1 / 17

Two Dimensional Arrays Rohit Khokher

Two Dimensional Arrays Rohit Khokher. Two dimensional Arrays. A[8][10]. A vector can be represented as an one dimensional array A matrix can be represented as a two dimensional array. Columns. Rows. A[5][6]. Array element. The value of sales of three items by four sales person. Items.

didier
Télécharger la présentation

Two Dimensional Arrays Rohit Khokher

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 ArraysRohitKhokher

  2. Two dimensional Arrays A[8][10] • A vector can be represented as an one dimensional array • A matrix can be represented as a two dimensional array Columns Rows A[5][6] Array element

  3. The value of sales of three items by four sales person Items Sales Persons

  4. 2-D array 2-Day Array Declaration #define ROWS 2 #define COLS 3 float A[ROWS ][COLS]; Compile time Initialization int A[ROWS ][COLS]= {0,0,0,1,1,1}; or int A[ROWS ][COLS]= ({0,0,0},{1,1,1});

  5. Compile time initialization int A[ROWS ][COLS]= {{0,0,0}, {1,1,1}}; int A[ ][COLS]= { {0,0,0}, {1,1,1} }; int A[ROWS][COLS]= { {0,0}, {2} }; The missing values are initialized to zero automatically

  6. Runtime Initialization for (i=0; i<ROWS; i++) for (j=0; i<COLS; j++) a[i][j]=0; Reading the elements of 2-D array Read data row-wise for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++) scanf (“%d”, &a[i][j]); Priniting the elements of 2-D array for (i=0; i<ROWS; i++) { Print a row for (j=0; j<COLS; j++) printf (“%d”, &a[i][j]); Change line printf (“\n”); }

  7. Matrix manipulation • Read a matrix A (4,4) of real numbers and compute: • sum of each row entries • sum of each column entries • sum of the main diagonal entries • sum of the secondary diagonal entries

  8. Compute row sum for (i=0; i<ROWS; i++) { R[i]=0; Initialize R[i] for (j=0; j<COLS; j++) R[i]=R[i]+A[i][j];}

  9. Compute Column sum for (j=0; j<COLS; j++) { C[j]=0; for (i=0; i<ROWS; i++) C[j]=C[j]+A[i][j]; } Initialize C[j]

  10. Compute diagonal sum D =0; for (j=0; j<COLS; j++) D= D+ A[j][j]; OR D =0; for (i=0; i<ROWS; i++) D=D+A[i][i];

  11. Compute second diagonal sum D =0; for (i=0; i<ROWS; i++) D= D+ A[i][COLS-i];

  12. Practice question • Write a C program to find the largest element of each row of a 2D array. • Write a C program to find the row and column numbers of the smallest element of a 2D array and count the sum of its neighbors. In a 2D array an element A[i][j] may have up to eight neighbors defined as:

  13. Add two matrices for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++) C[i][j]=A[i][j]+B[i][j];

  14. Multiplication of two matrices c11=a11 x b11 + a12 x b21 c12=a11 x b12 + a12 x b22 c13=a11 x b13 + a12 x b23 for (i=0; i<ROWSA; i++) c21=a21 x b11 + a22 x b21 for (j=0; j<COLSB; j++) c22=a21 x b12 + a22 x b22 c23=a21 x b13 + a22 x b23 { C[i][j]=0; c31=a31 x b11 + a32 x b21 for (k=0; j<COLSA; j++) C[i][j]= C[i][j]+A[i][k]+B[k][j]; } c22=a31 x b12 + a32 x b22 c33=a31 x b13 + a32 x b23

  15. Practice problem To verify the correctness the matrix multiplication algorithm described before compute:

  16. Practice problem • A square 2D array is called a magic square if sums of each row, each column, and both the diagonals of the array are equal. A 3 x 3 magic square is shown below. Read more about magic square at http://en.wikipedia.org/wiki/Magic_square Write a program that reads a 2D array and determines whether the array is a magic square or not.

  17. Project • Read about the tic-tac-toe game at http://en.wikipedia.org/wiki/Tic-tac-toe . Find the simplest algorithm that can be implemented in C using 1D and 2D arrays.

More Related