1 / 26

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

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

leiko
Download Presentation

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

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. Prepared by MURLI MANOHARPGT (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: dataTypearrayName[rowsize][colsize]; where rowsizeand colsizeare expressions yielding positive integer values

  4. 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

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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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;}

  11. 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

  12. 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

  13. 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 “; } }

  14. The output will be 2 7 1 2 5 7 0 1 5 0 3 4 9 1 5

  15. 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; }

  16. 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)]<< “ “ }

  17. 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; }

  18. THANK YOU

More Related