Escolar Documentos
Profissional Documentos
Cultura Documentos
1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann
It is often the case that you wish to store collections of data values that have two dimensional layout
Eachelementinatwodimensionalarrayhasarowposition andacolumnposition
to access an element in a two dimensional array, you must specify the name of the array followed by a) a row offset, and b) a column offset
ENGR 1200U Winter 2013 - UOIT
Declaration Declaration of a two-dimensional array requires a row size and column size Aconsecutiveblockof(rowsize)*(columnsize)memory locationsareallocated Example
int data[2][3];
All array elements must of the same data type
col 1
col 2
? ?
? ?
? ?
Definition Syntax
data_type identifier[ [row_size] ][column_size] [= initialization_list ];
//row_size and column_size must be integer constants
Examples
int data[2][5]; //allocates consecutive memory for 10 integer values double t[2][2] = {{3.0,5.0},{2.1,7.2}}; //allocates and initializes
Valid References cout << data[1][3]; cout << t[1][0]; Invalid References cout << data[2][5]; //invalid offset. cout << t[-1][-1]; //invalid offset
Asigning and Inputting Values in 2D Arrays Nested for loops are often used when inputting and assigning values to a two dimensional array Example //Declaration const int RSIZE(3),CSIZE(2); double v[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col v[i][j] = i+j;
V
ENGR 1200U Winter 2013 - UOIT
0 1 2
1 2 3
Outputting 2D Arrays Two dimensional arrays are often printed in a row by row format, using nested for statements. Important When printing the row values of an array, be sure to print: whitespace between the values in a row. a newline character at the end of each row. Example for (int i=0; i<n; ++i) {//every row for (int j=0; j<m; ++j )//every col cout << array[i][j] << ; cout << endl; //add end-of-line each row }
2D Arrays as Function Parameters The column dimension must be specified. The leftmost dimension (row) may be empty [] Function prototype example
int Arr[ROWSIZE][COLSIZE];
Example Consider this data from the 2006 Olympic skating competitions:
counts
counts
//setthatpositioninthearrayto8 counts[3][1]=8;
ENGR 1200U Winter 2013 - UOIT
for (int i =0;i <COUNTRIES;i++) { //Processtheith row for (int j=0;j<MEDALS;j++) { //Processthejth columnintheith row cout <<setw(8)<<counts[i][j]; } //Startanewlineattheendoftherow cout <<endl; }
Example:rowtotalsgiveusthetotalnumberof medalswonbyaparticularcountry
How many of each kind of medal was won?
counts
That would be a column total. Let j be the silver column:
int total=0; for (int i=0;i<COUNTRIES;i++) { total=total+counts[i][j]; }
Practice Exercises
1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann
Write code that fills an array double value[10] with each set of the values below:
Write code that fills an array double value[10] with each set of the values below:
ANSWER
a. 12345678910
//Useaforloop: double values[10]; for (int i=0;i<10;i++) { values[i]=i +1; }
Write code that fills an array double value[10] with each set of the values below:
ANSWER
b. 024681012141618
//Useaforloop: double values[10]; for (int i=0;i<10;i++) { values[i]=i *2; }
Write code that fills an array double value[10] with each set of the values below:
ANSWER
c. 0123401234
double values[10]; int count=0; for (int i=0;i<10;i++) { if (count>=5) {count=0;} values[i]=count; count++; }
ENGR 1200U Winter 2013 - UOIT
Consider the following array: What is the value of total after the following loops complete?
int a[ ] = {1,2,3,4,5,4,3,2,1,0};
10