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

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

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

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

Prepared by

MURLI MANOHARPGT (COMPUTER SCIENCE)

KV,B.E.G., PUNE

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

•LOGICAL PROBLEM BASED ON TWO

DIMENSIONAL ARRAY

Page 3: Prepared by MURLI MANOHAR PGT (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:dataType arrayName[rowsize][colsize];

where rowsize and colsize are expressions yielding positive integer values

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

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

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

A First Book of C++: From Here To There, Third Edition

6

Two-Dimensional Arrays (continued)

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

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

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

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

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

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

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

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

Page 12: Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Page 13: Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Page 14: Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Page 15: Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Page 16: Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Page 17: Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Page 18: Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.

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

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

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

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

• 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

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

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 “;

}}

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

• The output will be27 12 5 70 1 5 03 4 9 1 5

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

• 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;

}

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

• 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)]<< “ “

}

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

• 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;}

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

THANK YOU