in Programming in C edited by
8,762 views
43 votes
43 votes

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

  1. Only S2 is correct
  2. Only S3 is correct
  3. Only S1 and S2 are correct
  4. Only S1 and S3 are correct
in Programming in C edited by
8.8k views

3 Comments

As it's not specified ,can we consider the 2-D array is stored as column major order ?
4
4
No. In C, default storage mechanism is row major order.
10
10
what's the point of last line in the question .

" further assume that when stored in main memory all the elements of X are in same main memory page frame "
0
0

3 Answers

46 votes
46 votes
Best answer

Option is C. Only $S1$ and $S2$ are correct because $Y$ have same element in both code and in code $1.$

Y[i] += X[0][i];

This row major order (In C, arrays are stored in row-major order)  which gives address of each element in sequential order$(1,2,3,\ldots,n)$ means we cross single element each time to move next shows  contiguous in main memory but in code $2$ for:

Y[i] += X[i][0];
We are crossing n element (row crossing with n element )to move next.
edited by

4 Comments

@, Here from the question, All elements of  X stored in single frame

means, can we think that,  All the elements of X from X[0][0] to X[n-1][n-1] are stored in single frame, but X[0][0] & X[1][0]  is not continuously stored as like X[0][0] & X[0][1] is  continuously stored in Memory due to the Row major order of C .

 

Is my understanding correct ?

2
2
Yes
1
1
yess correct
0
0
7 votes
7 votes
answer will be c

as s1 is right(final contents are same ) and s2 as we are getting

consider size of 4

y[0]=0(same as x[0][0])

y[1]=2(as x[0][1]=0+1=1 and y[1]=0+1)

y[2]=1(x[0][2]=0+2=2 and y[2]=0+2)

thus following row major order as in c
6 votes
6 votes

Actually Answer depends upon storage scheme of array(Row major or Column major). However if it is not mentioned in the Question that which Scheme is used then assume it is stored in Row major because by default it is stored in Row major so here answer restrict to option C only.

1 comment

In s3 Elements of array X accessed inside the for loop shown in code segment 2 are not contiguous in main memory  because they stored in column major order 
0
0
Answer:

Related questions