Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following code is written in C, where array elements within the same row are

ID: 3681300 • Letter: T

Question

The following code is written in C, where array elements within the same row are stored contiguously.

for (I=0; I<8; I++)
for (J=0; J<8000; J++)

A[I][J]=B[I][0]+A[J][I];

a) References to which variables exhibit temporal locality? References to which variables exhibit spatial locality? Please give a brief explanation.

b) Now let us switch the inner and outer loop order, as shown below. References to which variables exhibit temporal locality? References to which variables exhibit spatial locality? Please give a brief explanation.

for (J=0; J<8000; J++)

for (I=0; I<8; I++)

A[I][J]=B[I][0]+A[J][I];

Explanation / Answer

Is this explanation ok with bit a)

a)
During each iteration of the code "a[i][j] = b[i][0] + a[j][i]" The variables i and j are constantly accessed, and because of processors taking advantage of temporal locality, these will likely stay in the cache the entire time this code is executing because the processor will assume, correctly, that is a piece of data is accessed, it will likely be accessed again very soon

>Spacial locality is the tendency for data with addresses near the current piece of data we are working with to to be needed soon. This is why when one piece of data is needed, the processor will also load the entire block with data that is next to the data we just accessed. In this example, i and j again exhibit spacial locality. because they would be located near each other in the array.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote