Presentation is loading. Please wait.

Presentation is loading. Please wait.

2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.

Similar presentations


Presentation on theme: "2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006."— Presentation transcript:

1 2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006

2 Declarations datatype array_name [num_of_rows] [num_of_col]; int array_1[10][10]; // indexed from (0,0) to (9,9) char array_2[3][4]; //indexed from (0,0) to (2,3) float array_3[2][10]; //indexed from (0,0) to (1,9) The first index always indicates the row, the second the column.

3 Declarations char letters[3][4]; char letters[3][4]; or or const int row = 3; const int col = 4; char letters[row][col]; //preferred way const int row = 3; const int col = 4; char letters[row][col]; //preferred way

4 1-D vs. 2-D Arrays 0 1 2 3 4 0,00,10,20,30,41,01,11,21,31,4 2,02,12,22,32,4 3,03,13,23,33,4 4,04,14,24,34,4

5 int table[4][3]; 72 45 15 62 10 0 19 99 27 38 24 89 72 45 15 62 10 0 19 99 27 38 24 89 table[2][2]

6 Examples float array_A[12][2]; char array_B[7][3]; int array_C[4][10];

7 Row-Major Order 72 45 15 62 10 0 19 99 27 38 24 89 72 45 15 62 10 0 19 99 27 38 24 89 |72|45|15|62|10|0|19|99|27|38|24|89| ? | ? | |72|45|15|62|10|0|19|99|27|38|24|89| ? | ? |

8 Initialization int nums[3][4] = {2,4,6,8,1,3,5,7,5,2,7,10}; int nums[3][4] = {2,4,6,8,1,3,5,7,5,2,7,10}; int nums[3][4] = {2,4,6,8, int nums[3][4] = {2,4,6,8, 1,3,5,7, 1,3,5,7, 5,2,7,10}; 5,2,7,10}; int nums[3][4] = { {2,4,6,8}, {1,3,5,7}, {1,3,5,7}, {5,2,7,10} }; {5,2,7,10} };

9 Accessing All Elements of an Array for ( i = 0; i < row; i++ ) { for( j=0; j<column; j++ ) cout << bunch_of_nums[i][j] << ‘\t’; cout << endl; cout << endl;}

10 Accessing Individual Elements int num; int bunch_of_nums[10][10]; bunch_of_nums[0][0] = 25; num = bunch_of_nums[3][5]; int num; int bunch_of_nums[10][10]; bunch_of_nums[0][0] = 25; num = bunch_of_nums[3][5];

11 Using Subscript Expressions result = result + value[ i + 2][ i * num] ; result = result + value[ i + 2][ i * num] ; cout << num[ i % row ][col-6]; cout << num[ i % row ][col-6];

12 Worksheet  Declare an array to store 5x5 matrix of integers.  List all of the elements on the major diagonal.  List all of the elements on the minor diagonal.  Write a code segment that would display all of the elements on the major diagonal.  Write a code segment that would display all of the elements on the minor diagonal.

13 const int size = 5; int square[size][size]; // display major diagonal in one loop for (int i = 0; i < size; i++) cout << square[i][i] << endl; // display minor diagonal in one loop for (int i = 0; i < size; i++) cout << square[i][size-1-i] << endl;

14 const int size = 5; int square[size][size]; // display major diagonal in two loops for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (i == j) cout << square[i][j] << endl; // display minor diagonal in two loops for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (i + j == size-1) cout << square[i][j] << endl;

15 Worksheet Write a code segment that declares Write a code segment that declares 45 x 33 array of integers and initializes all elements to 0. The program should then prompt the user for a location in that array ( row number, column number) and tell the user whether it’s a valid location ( i.e. within bounds). For example (4, 16) is a valid, (-3, 5) is invalid, (13, 56) is invalid. If location is valid, display the contents of that location. 45 x 33 array of integers and initializes all elements to 0. The program should then prompt the user for a location in that array ( row number, column number) and tell the user whether it’s a valid location ( i.e. within bounds). For example (4, 16) is a valid, (-3, 5) is invalid, (13, 56) is invalid. If location is valid, display the contents of that location.

16 const int nrow = 45; const int ncol = 33; int arr[nrow][ncol] = { 0 }; do { cout << "Enter row and column: "; cout << r << c; } while (r = nrow || c = ncol); c = ncol); cout << "The element in (" << r << ", " << c << ") is " << arr[r][c];

17 2-D Arrays in Functions void ProcessValues (int [ ][5], int, int); //works with ALL 2-d arrays that have //works with ALL 2-d arrays that have // exactly 5 columns: i.e. 10x5, 2x5 …. // exactly 5 columns: i.e. 10x5, 2x5 ….

18 int max(int[ ][2], int, int); void main() { const int nrow = 4, ncol = 2; int nums[nrow][ncol] = {3,2,5,13,1,7,9,4}; cout << "The max value of the array is " cout << "The max value of the array is " << max(nums, nrow, ncol); << max(nums, nrow, ncol);} int max(int vals[ ][2], int nr, int nc) { int i, j, max = vals[0][0]; for (i = 0; i < nr; i++) for (j = 0; j < nc; j++) if (max < vals[i][j]) max = vals[i][j]; for (i = 0; i < nr; i++) for (j = 0; j < nc; j++) if (max < vals[i][j]) max = vals[i][j]; return max; return max;}

19 Worksheet Write a function that receives an array of floating point values, number of rows and number of columns (known to be 7) in that array, and a “special” number between 0 and 6. The function should return the sum of all elements in the column that corresponds to the “special” number. Write a function that receives an array of floating point values, number of rows and number of columns (known to be 7) in that array, and a “special” number between 0 and 6. The function should return the sum of all elements in the column that corresponds to the “special” number.


Download ppt "2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006."

Similar presentations


Ads by Google