1 / 19

2D Arrays

2D Arrays. Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006. Declarations. datatype array_name [num_of_rows] [num_of_col]; int array_1[10][10]; // indexed from (0,0) to (9,9)

bianca
Download Presentation

2D Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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]; or const int row = 3;const int col = 4;char letters[row][col]; //preferred way

  4. 1-D vs. 2-D Arrays

  5. int table[4][3]; 72 45 1562 10 019 99 2738 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 1562 10 019 99 2738 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}, {1,3,5,7}, {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; }

  10. Accessing Individual Elements 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] ; 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 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 < 0 || r >= nrow || c < 0 || 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 // 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 " << 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]; 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.

More Related