3.b) An array Array[20][15] is stored in the memory along the column with each element occupying 8 bytes. Find out the base address of the element Array[2][3] if the element Array[4][5] is stored at the address 1000.
Solution:
Given Data: Aray [20][15] W=8 B=?
R=20 C=15 Lr = 0 Lc = 0
Address of Array [2][3] =?Address of Array[4][5] =1000.
Address of an element (I,J) in
column major =B + W ( (I-Lr) + R(J-Lc ) )
Therefore 1000=B+8*((4-0)+20(5-0))
1000=B+8*(4+20*5)
1000 =B+8*104
1000=B+832
B =1000-832
B =168
Therefore Address of
Array[2][3]=168+8*((2-0)+20(3-0))
=168+8*(2+20*3)
=168+8*62
=168+496
=664
3.d) 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. [Assuming the 2D Array to be a square matrix with odd dimension i.e., 3x3, 5x5 ,7x7 etc…]
Example : if the array content is
5 4 3
6 7 8
1 2 9
Out put through the function should be :
Diagonal One : 5 7 9
Diagonal Two : 3 7 1
Solution:
void accept(int a[ ][ ],int size)
{ cout<<"Diagonal One:";
for (int i=0;i<size;i++)
for(int j=0;j<size;j++)
if (i= = j)
cout<<a[i][j]<<’\t’;
cout<<"\n Diagonal Two:";
for (i=0;i<size;i++)
for(j=0;j<size;j++)
if((i+j)= =(size-1))
cout<<a[i][j]<<’\t’;
}
CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )