Consider the following two C code segment. Y and X are one- and two-dimensional arrays of size n and n × n respectively, where 2 ≤ n ≤ 10. Assume that in both code segments, elements of y are initialized to 0 and each element X[i] [j] of array X is initialized to i + j. Further assume that when stored in main memory all elements of x are in same main memory page frame. Code segment 1:                 //initialize elements of y to 0                 //initialize elements X[i] [j] of X to i+j                       for (i = 0; i < n; i++)                                 Y[i] += X[0] [i]; Code Segment 2:                 //initialize elements of Y to 0                 //initialize elements X[i] [j] of X to i+j                        for (i = 0; i < n; i++)                                 Y[i] += X[i] [0]; Which of the following statements is/are correct? S1: Final contents of array Y will be same in both code segments S2: Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory S3: Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory

Consider the following two C code segment. Y and X are one- and two-dimensional arrays of size n and n × n respectively, where 2 ≤ n ≤ 10. Assume that in both code segments, elements of y are initialized to 0 and each element X[i] [j] of array X is initialized to i + j. Further assume that when stored in main memory all elements of x are in same main memory page frame. Code segment 1:                 //initialize elements of y to 0                 //initialize elements X[i] [j] of X to i+j                       for (i = 0; i < n; i++)                                 Y[i] += X[0] [i]; Code Segment 2:                 //initialize elements of Y to 0                 //initialize elements X[i] [j] of X to i+j                        for (i = 0; i < n; i++)                                 Y[i] += X[i] [0]; Which of the following statements is/are correct? S1: Final contents of array Y will be same in both code segments S2: Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory S3: Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory Correct Answer Only S1 and S2 are correct

Code segment 1:

                //initialize elements of y to 0

                //initialize elements X of X to i+j

                      for (i = 0; i < n; i++)

                                Y += X ;

As, it is given that y element are initially 0 and x elements are i+j

Consider n = 5

So, final contents in array y=

Code segment 2:

//initialize elements of Y to 0

                //initialize elements X of X to i+j

                       for (i = 0; i < n; i++)

                                Y += X ;

Consider n = 5

Final contents of array y in this code segment =

Statement S1: Final contents of array Y will be same in both code segments

As, it is clear that, final contents of array Y is same in both code segments. This is true.

Statement S2: Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory

In C programming, array are stored in row major order which gives address of each element in sequential order means we cross single element each time to move next shows, contiguous in memory. So, this is true.

Statement S3: Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory

In code segment 2, y += x

Means we are crossing n element to move next, so elements of array X accesses inside the for loop are not contiguous in memory in code segment 2.

Related Questions