Deprecated: Implicit conversion from float-string "1710034768.853" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1710034768.853" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1710034768.853" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1710034768.853" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1710034768.853" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1608605121.494" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1608605121.494" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1608605121.494" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1608605121.494" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1608605121.494" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1610982163.864" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1610982163.864" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1610982163.864" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1610982163.864" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1610982163.864" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1684765331.615" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1684765331.615" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1684765331.615" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1684765331.615" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1684765331.615" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1534472597.783" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1534472597.783" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1534472597.783" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1534472597.783" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1534472597.783" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1547358893.935" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1547358893.935" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1547358893.935" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1547358893.935" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1547358893.935" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594
GATE CSE 2015 Set 3 | Question: 30 / GATE Overflow for GATE CSE
edited by
8,810 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
edited by

3 Answers

Best answer
46 votes
46 votes

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
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.

Answer:

Related questions

16.0k
views
8 answers
46 votes
go_editor asked Feb 14, 2015
16,036 views
Consider the following C program segment.# include <stdio.h int main() { char s1[7] = "1234", *p; p = s1 + 2; *p = '0'; printf("%s", s1); }What will be printed by the pro...
11.9k
views
4 answers
41 votes
go_editor asked Feb 16, 2015
11,900 views
Consider the following C program:#include<stdio.h int f1(void); int f2(void); int f3(void); int x=10; int main() { int x=1; x += f1() + f2 () + f3() + f2(); printf("%d", ...
22.4k
views
1 answers
77 votes
go_editor asked Feb 16, 2015
22,386 views
Consider the following C program:#include<stdio.h int main() { int i, j, k = 0; j=2 * 3 / 4 + 2.0 / 5 + 8 / 5; k-= j; for (i=0; i<5; i++) { switch(i+k) { case 1: case 2: ...
22.5k
views
11 answers
61 votes
go_editor asked Feb 15, 2015
22,477 views
Consider the following C program#include<stdio.h int main() { static int a[] = {10, 20, 30, 40, 50}; static int *p[] = {a, a+3, a+4, a+1, a+2}; int ptr = p; ptr++; print...
Total PHP MySQL Other RAM
Time (ms) % Time (ms) % File count Time (ms) % Query count Time (ms) % Amount %
Setup 3.9 2% 2.4 1% 72 1.5 1% 2 0.0 0% 569k 39%
Control 13.9 10% 1.6 1% 5 12.5 9% 12 0.0 0% 346k 23%
View 3.0 2% 3.0 2% 12 0.0 0% 0 0.0 0% 175k 12%
Theme 104.9 79% 4.8 3% 15 100.1 75% 3 0.0 0% 363k 24%
Stats 7.1 5% 0.1 0% 0 7.0 5% 1 0.0 0% 0k 0%
Total 132.8 100% 12.0 9% 104 121.2 91% 18 0.0 0% 1455k 100%