Você está na página 1de 10

ENGR 1200U Introduction to Programming Lecture 22 Two-Dimensional Arrays

Dr. Eyhab Al-Masri

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

Many of these collections commonly occur in financial and scientific applications

What is a two-dimensional array? Itanarrangementoftabulardata:consistingofrows andcolumnsofvalues


also called a matrix

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

row/column form: col 0 row 0 row 1


ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

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 }

ENGR 1200U Winter 2013 - UOIT

2D Arrays as Function Parameters The column dimension must be specified. The leftmost dimension (row) may be empty [] Function prototype example

int someFunction(int Arr[][COLSIZE], int someParameter);


Array declaration in main:

int Arr[ROWSIZE][COLSIZE];

ENGR 1200U Winter 2013 - UOIT

Example Consider this data from the 2006 Olympic skating competitions:

Canada China Germany Korea Japan Russia United States

Gold Silver Bronze 1 0 1 1 1 0 0 0 1 1 0 0 0 1 1 0 1 1 1 1 0

const int COUNTRIES = 7; const int MEDALS = 3; int counts[COUNTRIES][MEDALS];


You cannot change the size of a 2D array once it is defined

ENGR 1200U Winter 2013 - UOIT

How to Initialize a 2D Array?


int counts[COUNTRIES][MEDALS]= { {1,0,1}, {1,1,0}, {0,0,1}, {1,0,0}, {0,1,1}, {0,1,1}, {1,1,0} };

ENGR 1200U Winter 2013 - UOIT

Accessing Elements: The Olympic array looks like this

counts Accesstothe secondelement inthefourth rowis: counts[3][1]

ENGR 1200U Winter 2013 - UOIT

Accessing Elements (contd)

counts

ENGR 1200U Winter 2013 - UOIT

//setvaluetowhatiscurrently //storedinthearrayat[3][1] int value=counts[3][1];

Accessing Elements (contd)

counts

//setthatpositioninthearrayto8 counts[3][1]=8;
ENGR 1200U Winter 2013 - UOIT

Outputting Olympic Array Values

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; }

ENGR 1200U Winter 2013 - UOIT

It is a common task to compute the row or column totals

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]; }

ENGR 1200U Winter 2013 - UOIT

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:

a. 12345678910 b. 024681012141618 c. 0123401234


ENGR 1200U Winter 2013 - UOIT

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; }

ENGR 1200U Winter 2013 - UOIT

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; }

ENGR 1200U Winter 2013 - UOIT

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};

int total=0; for (int i=0;i<10;i++) { total=total+a[i]; } Total equals 25

int total=0; for (int i=9;i>=0;i) { total=total+a[i]; } Total equals 25

ENGR 1200U Winter 2013 - UOIT

10

Você também pode gostar