Multiple-Subscripted%20Array - PowerPoint PPT Presentation

About This Presentation
Title:

Multiple-Subscripted%20Array

Description:

must tell compiler name, size (2 sizes) and type. all elements of the array must be the same type ... 1) Define constant variables numRows and numCols to be ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 17
Provided by: CCC
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: Multiple-Subscripted%20Array


1
Multiple-Subscripted Array
  • What it is
  • How to use it
  • How to declare it
  • How to initialize it

2
Multiple-Subscripted Arrays
  • Use more than one subscript (can have up to 12)
  • Double-subscripted array (2 subscripts)

3
Double-subscripted array
  • good for representing tables in memory
  • one subscript is the row
  • second subscript is the column

4
Double-subscripted array
column 0
column 1
column 2
column 3
row 0
a00
a01
a02
a03
row 1
a10
a11
a12
a13
a20
a21
a22
a23
row 2
column subscript
row subscript
array name a
a double-subscripted array with 3 rows and 4
columns 3 by 4
array
5
Referencing elements in a double-subscripted array
To set the element in row 2 and column 3 to the
value 4 use a 2 3 4
column subscript
row subscript
name of the array
6
Declaring a 3 by 4 array
int a 3 4
must tell compiler name, size (2 sizes) and
type all elements of the array must be the same
type any data type is fine (int, float, char,
etc.)
7
Declaring a double-subscripted array
Must use constants to declare row and column
sizes. int a 3 4 char letters 2 26
define ROW 3 define COLUMN 4 int a ROW
COLUMN const int row 3 const int column
4 int a row column
8
Initializing a double-subscripted array
const int row 3 const int column 4 int a
row column for (int i 0 i lt row i)
for (int j 0 j lt column j) a
ij 0
9
Initializing a double-subscripted array with
declaration
const int row 3 const int column 4 int
arow column 1,2,3,4,
1,2,3,4,
1,2,3,4
2
3
4
1
3
1
2
4
3
4
1
2
10
Initializing a double-subscripted array with
declaration
const int row 3 const int column 4 int
arow column 1,2,3,
1,2,
1,2,3,4
2
3
0
1
0
1
2
0
3
4
1
2
11
Setting entire array to 0
const int row 3 const int column 4 int a
row column 0
0
0
0
0
0
0
0
0
0
0
0
0
12
Exercises
Answer the following questions regarding an array
called table that is a double-subscripted array
with 2 rows and 3 columns. 1) Define constant
variables numRows and numCols to be used to
declare the array. 2) Declare the array to be
an integer array. 3) Use a for loop to
initialize each element of the array to be the
sum of its subscripts 4) Write a code segment
to print the array as a table.
13
How double-subscripted arrays are stored in memory
2
3
4
1
table
5
6
7
8
9
10
11
12
table
1
2
3
4
5
6
7
8
9
10
11
12
14
passing double-subscripted array to a function
Must tell function size of second subscript so it
can find the beginning of each row in memory.
void PrintTable (int table 4, int
numRows, int numCols) for (int i 0 i lt
numRows i) for (int j 0 j lt
numCols j) cout ltlt setw(4) ltlt table
i j cout ltlt endl
15
passing double-subscripted array to a function
  • passed as reference parameter
  • Use const to prevent function changing the array.

void PrintTable (const int table 4, int
numRows, int numCols)
16
Exercises
Write a main program as follows Use the results
of the last set of exercises to declare an array
of type integers with 2 rows and 3 columns and
initialize each element to the sum of its
subscripts. Call the PrintTable function to print
the array. Create a function called PrintTable
to print each element of the array in tabular
form.
Write a Comment
User Comments (0)
About PowerShow.com