Prepared by
MURLI MANOHARPGT (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
• The syntax for declaring a two-dimensional array is:dataType arrayName[rowsize][colsize];
where rowsize and colsize are expressions yielding positive integer values
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
A First Book of C++: From Here To There, Third Edition
6
Two-Dimensional Arrays (continued)
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:
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
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 (int i = 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 07 1 5 3 12 5 7 8 1 if A is0 1 5 0 13 4 9 1 5
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] <<“ “;elsecout << “ “;
}cout<< “ \n “;
}}
• The output will be27 12 5 70 1 5 03 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.
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)]<< “ “
}
• 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;}
THANK YOU
Top Related