1 / 28

Two Dimensional Arrays

Two Dimensional Arrays. Two-dimensional Arrays. A two-dimensional array consists of both rows and columns of elements. It is essentially a matrix. To declare a two-dimensional array, we merely use two sets of square brackets. The first contains the number of rows

amma
Télécharger la présentation

Two Dimensional 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. Two Dimensional Arrays The Ohio State University

  2. Two-dimensional Arrays • A two-dimensional array consists of both rows and columns of elements. It is essentially a matrix. • To declare a two-dimensional array, we merely use two sets of square brackets. • The first contains the number of rows • The second contains the number of columns //Creates a 2D array with 3 rows and 4 columns int vals[3][4]; The Ohio State University

  3. Indices in 2D arrays • Assume that the two dimensional array called val is declared and looks like the following: • To access the cell containing 6, we reference val[1][3],that is, row 1, column 3. The Ohio State University

  4. Using 2D arrays • Just like 1D arrays, once you have specified the index, you are just working with a single variable of the given data type. • Assignments and usage is still the same: sumRow0 = val[0][0] + val[0][1] + val[0][2] + val[0][3]; //assigns 72 to cell at row 2, column 3 val[2][3] = 72; The Ohio State University

  5. Initializing 2D arrays • You can use additional braces to indicate when rows start and end, but you don’t have to do that. int val[3][4] = { {8,16,9,52}, {3,15,27,6}, {14,25,2,10} }; • Or int val[3][4] = {8,16,9,52, 3,15,27,6, 14,25,2,10}; • Or (correct, but not as clear as the first two): int val[3][4] = {8,16,9,52,3,15,27,6,14,25,2,10}; The Ohio State University

  6. More on 2D arrays • Initialization of 2D arrays is done in row order. • 2D arrays work well with (for) loops like 1D arrays. However, to access all elements, typically you will need nested loops for 2D arrays. Can you see why? The Ohio State University

  7. Example Program ... int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the array for (int row = 0; row < NUM_ROW; row++) { for (int col = 0; col < NUM_COL; col++) { cout << vals[row][col] << " "; } cout << endl; } ... The Ohio State University

  8. Example Program ... int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the array for (int row = 0; row < NUM_ROW; row++) { for (int col = 0; col < NUM_COL; col++) { cout << vals[row][col] << " "; } cout << endl; } ... > array2DExample.exe 11 12 13 14 21 22 23 24 31 32 33 34 The Ohio State University

  9. Example Program (2) ... int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the transpose of the array for (int col = 0; col < NUM_COL; col++) { for (int row = 0; row < NUM_ROW; row++) { cout << vals[row][col] << " "; } cout << endl; } ... The Ohio State University

  10. Matrix Addition Algorithm • Add two n x m matrices A, B; • for each row i of A do • for each column j of A do • C[i][j] = A[i][j] + B[i][j]; • Output matrices A, B and C. The Ohio State University

  11. matrixAdd.cpp ... int main() { const int NUM_ROWS(3); // number of matrix rows const int NUM_COLS(2); // number of matrix columns // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; // C = A + B for (int i=0; i<NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { C[i][j] = A[i][j] + B[i][j]; } } ... The Ohio State University

  12. matrixAdd.cpp (2) ... // display A cout << "A =" << endl; for (int i=0; i<NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { cout << " " << A[i][j] << " "; } cout << endl; } cout << endl; ... The Ohio State University

  13. Distance Matrix Algorithm • Compute a matrix D storing distances to a point (x,y); • Input: x, y • for each row i of D do • for each column j of D do • D[i][j] = distance from (x,y) to (i,j) • Output matrix D. The Ohio State University

  14. distanceMatrix.cpp ... // dimensions of distance matrix const int N_ROWS(8); const int N_COLS(12); double A[N_ROWS][N_COLS]; double x(0.0), y(0.0); cout << "Enter (x,y) coordinates of center: "; cin >> x >> y; for (int i=0; i<N_ROWS; i++) { for (int j=0; j<N_COLS; j++) { double dx = j-x; // Note: x represents columns double dy = i-y; // Note: y represents rows double dist = sqrt(dx*dx+dy*dy); A[i][j] = dist; } } ... The Ohio State University

  15. X Matrix Algorithm • Compute a character matrix A representing an X; • Input: h (height of the X) • Initialize the first h columns in the first h rows of A to ‘-’; • Set all elements of A on the diagonal from A[0][0] to A[h-1][h-1] to ‘X’; • Set all elements of A on the diagonal from A[0][h-1] to A[h-1][0] to ‘X’. The Ohio State University

  16. X.cpp ... // dimensions of matrix A const int N_ROWS(50); const int N_COLS(N_ROWS); char A[N_ROWS][N_COLS]; int height(0); cout << "Enter height of X: "; cin >> height; while (height > N_ROWS) { cout << "Input error. Input must be less than or equal to " << N_ROWS << ". " << endl; cout << "Enter height of X: "; cin >> height; } ... The Ohio State University

  17. X.cpp (2) // initialize elements of A to '-' for (int i=0; i<height; i++) { for (int j=0; j<height; j++) { A[i][j] = '-'; } } // draw diagonal \ in A for (int k=0; k<height; k++) { A[k][k] = 'X'; } // draw diagonal / in A for (int k=0; k<height; k++) { A[k][height-k-1] = 'X'; } The Ohio State University

  18. X.cpp ... // dimensions of matrix A const int N_ROWS(50); const int N_COLS(N_ROWS); char A[N_ROWS][N_COLS]; int height(0.0); cout << "Enter height of X: "; cin >> height; while (height > N_ROWS) { cout << "Input error. Input must be less than or equal to " << N_ROWS << ". " << endl; cout << "Enter height of X: "; cin >> height; } ... The Ohio State University

  19. Two Dimensional Arrays as Parameters The Ohio State University

  20. Matrix Addition Algorithm • Add two n x m matrices A, B; • for each row i of A do • for each column j of A do • C[i][j] = A[i][j] + B[i][j]; • Output matrices A, B and C. The Ohio State University

  21. matrixAdd2.cpp ... int main() { // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; add(A, B, C); cout << "A = " << endl; display(A); cout << "B = " << endl; display(B); cout << "C = " << endl; display(C); return 0; } ... The Ohio State University

  22. matrixAdd2.cpp ... // GLOBAL CONSTANTS const int NUM_ROWS(3); // number of matrix rows const int NUM_COLS(2); // number of matrix columns // Function prototypes void add(const double M1[NUM_ROWS][NUM_COLS], const double M2[NUM_ROWS][NUM_COLS], double M3[NUM_ROWS][NUM_COLS]); void display(const double M[NUM_ROWS][NUM_COLS]); int main() { // Note: A, B and C have the same dimensions // Matrices double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; ... The Ohio State University

  23. Function add() // Add two matrices // M1[][] = First matrix // M2[][] = Second matrix // M3[][] = M1[][] + M2[][] // Note: M1, M2 and M3 must have the same dimensions void add(const double M1[NUM_ROWS][NUM_COLS], const double M2[NUM_ROWS][NUM_COLS], double M3[NUM_ROWS][NUM_COLS]) { // M3 = M1 + M2 for (int i=0; i < NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { M3[i][j] = M1[i][j] + M2[i][j]; } } } The Ohio State University

  24. Function display() // Output matrix // M[][] = Matrix void display(const double M[NUM_ROWS][NUM_COLS]) { for (int i=0; i < NUM_ROWS; i++) { for (int j=0; j < NUM_COLS; j++) { cout << " " << setw(3) << M[i][j]; } cout << endl; } cout << endl; } The Ohio State University

  25. matrixAdd2.cpp ... int main() { // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; add(A, B, C); cout << "A = " << endl; display(A); cout << "B = " << endl; display(B); cout << "C = " << endl; display(C); return 0; } ... The Ohio State University

  26. Passing 2D Arrays into Functions • 2D arrays are always passed by reference. • To declare a 2D array parameter, • Define global constants NUM_ROWS and NUM_COLS; • Parameter is: type array2D[NUM_ROWS][NUM_COLS] For instance, void f(int array2D[NUM_ROWS][NUM_COLS]) • No way to declare a variable length 2D array parameter. For instance, void f(int array2D[][]) is illegal and will generate a syntax error. The Ohio State University

  27. Multi-Dimensional Arrays • Arrays can have higher dimensions. • Definitions, intialization, indexing, function parameters are similar to 2D arrays. • Note multidimensional arrays can have quite a few elements so you must be mindful of your memory capacity: int big[1000][1000][1000];//a billion ints The Ohio State University

  28. Common Programming Errors • Addressing indices that are out of bounds of the array range. This will run-time crash or at the very least a logical error. Be careful especially when using expressions to compute the indices. • Remember, indexing starts with 0, not 1!!! • Forgetting to declare the array (either altogether or forgetting the []) • Assuming the array is initialized (to zero.) The Ohio State University

More Related