260 likes | 362 Views
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE. LOGICAL PROBLEM BASED ON TWO DIMENSIONAL ARRAY. 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
E N D
Prepared by MURLI MANOHARPGT (COMPUTER SCIENCE)KV,B.E.G., PUNE
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: dataTypearrayName[rowsize][colsize]; where rowsizeand colsizeare expressions yielding positive integer values
Two-Dimensional Arrays (continued) • The two expressions rowsizeand colsizespecify the number of rows and the number of columns, respectively, in the array • Two-dimensional arrays are sometimes called matrices or tables
Two-Dimensional Arrays (continued) A First Book of C++: From Here To There, Third Edition
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
Processing Two-Dimensional Arrays • A two-dimensional array can be processed in three different ways: • Process the entire array • Process a particular row of the array, called row processing • Process a particular column of the array, called column processing
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
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
Initialisation of 2D Array #include <iostream>int main(){int _2DArray[5][6] = { { 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 0, 1, 2}, { 3, 4, 5} }; for (inti = 0; i < 5; i++) { for (int j = 0; j < 6; j++) {cout << _2DArray [i][j]; }cout << endl; }cout << endl; return 0;}
OUTPUT 1 2 3 4 5 67 8 9 0 1 23 4 5 0 0 00 0 0 0 0 00 0 0 0 0 0
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. 2 3 1 5 0 7 1 5 3 1 2 5 7 8 1 if A is 0 1 5 0 1 3 4 9 1 5
void Upper_half(int b[ ][10 ], int N) { inti, j; for (i = 0 ; i<N; i++) { for (j =0 ; j < N; j++) { if (I > = j) cout<< b[i][j] <<“ “; else cout << “ “; } cout<< “ \n “; } }
The output will be 2 7 1 2 5 7 0 1 5 0 3 4 9 1 5
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; }
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. constint n = 5; void Diagonals( int A[n][n], int size) { inti, 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)]<< “ “ }
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. constintS = 5; void DisplayMidle( intA[S][S], intS) { int mid = S/2; inti; 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; }