Consider the following code to initialize an array in C++: double A[100][100]; .
ID: 674304 • Letter: C
Question
Consider the following code to initialize an array in C++:
double A[100][100];
...
for (int j = 0; j < 100; j++) {
for (int i = 0; i < 100; i++) {
A[i][j] = i * j;
}
}
double A[100][100];
...
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
A[i][j] = i * j;
}
}
1) Compare the execution speed between the code on the left and the code on the right and justify your answer.
2) Suppose the address for A[10][10] is 40000, double takes 16 bytes, and byte addressing is used, what are the addresses for A[40, 50] ?
3) Given the same array A[100][100] in C++, what will be the result of the following code:
double last;
last = A[100][100];
cout << “the last element is: “ << last << “ ”;
double A[100][100];
...
for (int j = 0; j < 100; j++) {
for (int i = 0; i < 100; i++) {
A[i][j] = i * j;
}
}
Explanation / Answer
1) The execution speed of both the programs is same because both wil be running for 10000 times and inside the loop they are doing only one operation of multiplication.
2) At A[40,50] there will be 39*100+51 = 3951th double number present. Till A[10,10] there are 911 elements are present so the address of A[40,50] will be (3951-911)*16+40000 = 88640
3) undefined as the last element of the array is A[99][99]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.