According to the question , one frame can hold 512 elements.
The array D[128][128] is stored in row-major order. i.e the array is stored in memory in the following manner:
Each page frame can hold 512 elements. Now as elements in memory are stored in row-major order, number of rows stored in one page frame will be
number of rows of array in one page frame = $\frac{512}{128} = 4$
therefore , elements in 1st page frame frame will look like the following
Now,
for(i=0; i<128; i++){
for(j = 0; j < 128; j++){
D[j][i] *= 10
}
}
According to the for loop, elements accessed in the sequence:
D[0][0] , D[1][0] , D[2][0] , D[3][0] , ….. D[127][0] -------------- for i = 0
D[0][1] , D[1][1] , D[2][1] , D[3][1] , ….. D[127][1] -------------- for i = 1
.
.
D[0][127] , D[1][127] , D[2][127] , D[3][127] , ….. D[127][127] -------------- for i = 127
Now as one page frame can store only 4 rows, so for D[0][0] ….. D[3][127], there will be 1 page
fault and that also only for D[0][0] ( when it tries to access D[0][0] , it will the fetch 512
elements and store it in the page frame ) .
D[4][0] …. D[7][127], will be stored in the 2nd page frame and there will be also 1 page fault
and that also for D[4][0].
Now, as we are only having 30 frames and each frame can store upto 4 rows, so the elements
that will be present in page frame 30 will be
Now, we are having 30 frames, and each frame stores 4 rows, for 1 page frame we are having 1
page fault so for 30 page frame we will have 30 page faults.
we are still left with 8 rows, for D[120][0] …. D[123][127] , there will be one page fault, that also
only for D[120][0]. ( Remember that we will be using LRU replacement policy, so 1st page frame
will be replaced ) .
and then for D[124][0] … D[127][127], we will have one page fault and that also only for D[124][0].
So, in total for one iteration of outer loop we are having 32 page faults. In total we have 128
iterations of outer loop , so we will have
$128 * 32 = 4096$ page faults