Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.

Similar presentations


Presentation on theme: "Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE."— Presentation transcript:

1 Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE

2 LOGICAL PROBLEM BASED ON TWO DIMENSIONAL ARRAY

3 Two-Dimensional Arrays Two-dimensional Array: a collection of a fixed number of components arranged in two dimensions – All components are of the same type The syntax for declaring a two-dimensional array is: dataType arrayName[rowsize][colsize]; where rowsize and colsize are expressions yielding positive integer values

4 Two-Dimensional Arrays (continued) The two expressions rowsize and colsize specify the number of rows and the number of columns, respectively, in the array Two-dimensional arrays are sometimes called matrices or tables

5

6 A First Book of C++: From Here To There, Third Edition 6 Two-Dimensional Arrays (continued)

7 Accessing Array Components The syntax to access a component of a two- dimensional array is: arrayName[indexexp1][indexexp2] where indexexp1 and indexexp2 are expressions yielding nonnegative integer values indexexp1 specifies the row position and indexexp2 specifies the column position

8

9 Processing Two-Dimensional Arrays A two-dimensional array can be processed in three different ways: 1.Process the entire array 2.Process a particular row of the array, called row processing 3.Process a particular column of the array, called column processing

10 Processing Two-Dimensional Arrays (continued) Each row and each column of a two- dimensional array is a one-dimensional array When processing a particular row or column of a two-dimensional array – we use algorithms similar to processing one- dimensional arrays

11 Two-Dimensional Arrays Two-dimensional arrays are stored in row order – The first row is stored first, followed by the second row, followed by the third row and so on When declaring a two-dimensional array as a formal parameter – can omit size of first dimension, but not the second Number of columns must be specified

12

13

14

15

16

17

18 Initialisation of 2D Array #include int main() { int _2DArray[5][6] = { { 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 0, 1, 2}, { 3, 4, 5} }; for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { cout << _2DArray [i][j]; } cout << endl; } cout << endl; return 0; }

19 OUTPUT 123456789012345000000000000000123456789012345000000000000000

20 Write a user function named Lower_half() which takes a two dimensional array A, with size N rows and N columns as argument and pronts the lower half of the array. 23150 71531 25781if A is 01501 34915

21 void Upper_half(int b[ ][10 ], int N) { int i, j; for (i = 0 ; i<N; i++) {for (j =0 ; j < N; j++) {if (I > = j) cout<< b[i][j] <<“ “; else cout << “ “; } cout<< “ \n “; }

22 The output will be 2 71 257 0150 34915

23 Write a function int ALTERSUM ( int B[][5], int N, int M) in c++ to find and return the sum of elements from all alternate elements of a two-dimensional array starting from B[0][0]. Sol. int ALTERSUM(int B[ ][3], int N, int M) {int sum = 0; for (int I = 0; I<N; I++) for (int J = 0; J < M; J++) {if( I + J ) %2 = = 0) sum = sum + B[I][J]; } return sum; }

24 Write a function in c++ which accepts a 2D array of integers and its size as arguments and displays the elements which lie on diagonals. const int n = 5; void Diagonals( int A[n][n], int size) { int i, j; cout << “ Diagonal One”; for (i=0 ; i<n; i++) cout << A[i][i]<< “ “; cout<< “Diagonal Two”; for (i = 0; i<n; i++) cout<<A[i][n-(i+1)]<< “ “ }

25 Write a function in c++ which accepts a 2D array of integers and its size as arguments and displays the elements of middle row and the elements of middle column. const int S = 5; void DisplayMidle( int A[S][S], int S) { int mid = S/2; int i; cout << “ \n Middle row”; for (i=0 ; i<S; i++) cout << A[ mid ][ i ]<< “ “; cout<< “ \n Middle Column ”; for (i = 0; i<S; i++) cout<<A[i][ mid ]<< “ “ cout << endl; }

26 THANK YOU


Download ppt "Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE."

Similar presentations


Ads by Google