wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Consider the following two C code segments. Y and X are one and two dimensional arrays of size n and n×n respectively, where 2n10. 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.

A
Only S1 and S3 are correct
No worries! We‘ve got your back. Try BYJU‘S free classes today!
B
Only S3 is correct
No worries! We‘ve got your back. Try BYJU‘S free classes today!
C
Only S1 and S2 are correct
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
D
Only S2 is correct
No worries! We‘ve got your back. Try BYJU‘S free classes today!
Open in App
Solution

The correct option is C Only S1 and S2 are correct
Y is one dimensional array

X is two dimensional array


0123n1

0123n1⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢0123n11234n2345n+13456n+2n1nn+1n+2n+n2⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥n×n

Code segment 1 :

for (i = 0; i < n; i++)
Y[i] + = X [0] [i];

Code segment 2:

for (i = 0; i < n; i++)
Y[i] + = X [i] [0];

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


Output of both segments.

S2 : X contents are stored row wise in C- program hence code segment row '0' accessed contiguous in memory.

S3 : In code segment 2, content of X are not accessed in contiguous.

flag
Suggest Corrections
thumbs-up
0
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Elements of Matrix
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon