Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two Dimensional Arrays Rohit Khokher. Two dimensional Arrays A vector can be represented as an one dimensional array A matrix can be represented as a.

Similar presentations


Presentation on theme: "Two Dimensional Arrays Rohit Khokher. Two dimensional Arrays A vector can be represented as an one dimensional array A matrix can be represented as a."— Presentation transcript:

1 Two Dimensional Arrays Rohit Khokher

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

3 The value of sales of three items by four sales person 123 1A[0][0]A[0][1]A[0][2] 2A[1][0]A[1][1]A[1][2] 3A[2][0]A[2][1]A[2][2] 4A[3][0]A[3][1]A[3][2] Items Sales Persons

4 2-D array 2-Day Array Declaration #define ROWS2 #define COLS3 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

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

8 Compute row sum A[0][0]A[0][1]A[0][2]A[0][3] A[1][0]A[1][1]A[1][2]A[1][3] A[2][0]A[2][1]A[2][2]A[2][3] A[3][0]A[3][1]A[3][2]A[3][3] R[0]=A[0][0] +A[0][1] +A[0][2] +A[0][3] R[1]=A[1][0] +A[1][1] +A[1][2] +A[1][3] R[2]=A[2][0] +A[2][1] +A[2][2] +A[2][3] R[3]=A[3][0] +A[3][1] +A[3][2] +A[3][3] for (i=0; i

9 Compute Column sum A[0][0]A[0][1]A[0][2]A[0][3] A[1][0]A[1][1]A[1][2]A[1][3] A[2][0]A[2][1]A[2][2]A[2][3] A[3][0]A[3][1]A[3][2]A[3][3] C[0]=A[0][0] +A[1][0] +A[2][0] +A[3][0] for (i=0; i

10 Compute diagonal sum A[0][0]A[0][1]A[0][2]A[0][3] A[1][0]A[1][1]A[1][2]A[1][3] A[2][0]A[2][1]A[2][2]A[2][3] A[3][0]A[3][1]A[3][2]A[3][3] D=A[0][0] +A[1][1] +A[2][2] +A[3][3] for (i=0; i

11 Compute second diagonal sum A[0][0]A[0][1]A[0][2]A[0][3] A[1][0]A[1][1]A[1][2]A[1][3] A[2][0]A[2][1]A[2][2]A[2][3] A[3][0]A[3][1]A[3][2]A[3][3] D=A[0][3] +A[1][2] +A[2][1] +A[3][0] for (i=0; 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: i-1,j-1i-1,ji-1, j+1 I,j-1i,ji,j+1 i+1,j-1i+1,ji+1,j+1

13 Add two matrices for (i=0; i

14 Multiplication of two matrices for (i=0; i

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 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 Find the simplest algorithm that can be implemented in C using 1D and 2D arrays.


Download ppt "Two Dimensional Arrays Rohit Khokher. Two dimensional Arrays A vector can be represented as an one dimensional array A matrix can be represented as a."

Similar presentations


Ads by Google