Consider a 20 *5 two-dimensional array marks which has its base address = 1000 and the size of an element = 2. Now compute the address of the element, marks[18][ 4] assuming that the elements are stored in row major order.
Element A[18][0] is stored at
"Base\\ Address + 18 \\times 2\\times 5 = 1180". which is "1000 + 18 \\times 2\\times 5 = 1180".
Therefore, marks[18][ 4] is stored at "1180 + 4 \\times 2 = 1188"
Answer is: 1188
The following program will give 1188 as the answer
#include<iostream>
using namespace std;
int main(){
int sum = 1000;
int arr[20][5];
for(int i=0; i<20; i++){
for(int j=0; j<5; j++){
arr[i][j] = sum;
sum = sum + 2;
}
}
cout<<arr[18][4];
//Answer is 1188
}
Comments
Thanks
Leave a comment