COMPUTER SCIENCE ARRAYS

3.b) An array VAL[1…15][1…10] is stored in the memory with each element requiring 4 bytes of storage. If the base address of the array VAL is 1500, determine the location of VAL[12][9] when the array VAL is stored (i) Row wise (ii) Column wise.

Solution:

Given Data:
VAL[1…15][1…10]
Word Length (W) = 4 Bytes
Base Address of VAL(B) = 1500
VAL[12][9] = ?
C = Total No of Columns
R = Total No of Rows
Lr = Least Row=1
Lc = Least Column=1

( i ) Row Major:
Address of an element (I,J) in row major = B + W ( C (I-Lr) + (J – Lc))

VAL [12][9] = 1500 + 4 (10 * (12-1) + (9-1))
= 1500 + 4 (10 * 11+8)
= 1500 + 4 (118)
= 1500 + 472
= 1972.

( i ) Column Major:
Address of an element (I,J) in column major = B + W ( (I-Lr) + R(J – Lc))

VAL [12][9] = 1500 + 4 ((12-1) +15 * (9-1))
= 1500 + 4 (11 + 15 * 8)
= 1500 + 4 ( 11+ 120)
= 1500 + 4 * 131
= 1500 + 524
= 2024.

3.c) Write a user-defined function in C++ to find and display the sum of diagonal elements from a 2D array MATRIX[6][6] containing integers.

void displaysum( )
{ int i,j,D1=0,D2=0,MATRIX[6][6];
cout<<”\nEnter any 36 values….”;
for(i=0;i<6;i++)
for(j=0;j<6;j++){ cin>>MATRIX[i][j];
if(i= = j)
D1=D1+MATRIX[i][j];
else if ((i+j)= =(size-1))
D2=D2+MATRIX[i][j];
}
cout<<”\nThe sum of the elements of the Main Diagonal = “<<D1;
cout<<”\nThe sum of the elements of the Other Diagonal = “<<D2;
}

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )