0

After putting efforts i was unable to solve the following question. Question was asked in Graduate Aptitude Test in Engineering (GATE) 2014, India.

Question) For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and size of the character is 8 bits.

t0 = i * 1024

t1 = j * 32

t2 = k * 4

t3 = t1 + t0

t4 = t3 + t2

t5 = X[t4]

Which one of the following statements about the source code for the C program is CORRECT?

(a) X is declared as "int X[32][32][8]".

(b) X is declared as "int X[4][1024][32]".

(c) X is declared as "int X[4][32][8]".

(d) X is declared as "int X[32][16][2]".

One of the book which provide solutions to the previous papers says that the answer is option (a). How? Any explanation

Thanks in advance

4
  • Are you sure that all the arrays are of type int? Commented Oct 19, 2014 at 2:06
  • 1
    Edited the question with proper options as per official GATE 2014 question paper. See question no. 34 of this paper: gate.iitk.ac.in/GATE2015/docs/QP2014/CS02_2014.pdf Commented Jan 13, 2015 at 16:25
  • @ShantanuPaul That should really have been a comment as it deviates from the original intent of the question, and the matching answers. Commented Jan 14, 2015 at 11:20
  • @ShantanuPaul Although you may be fully correct with the edits, please refrain from code correction in questions in future editing action as soon as it may either answer the question, or make answers irrelevant. For more information read on When should I make edits to code?. Commented Jan 14, 2015 at 11:56

4 Answers 4

2

t1 is i * (inumInts * sizeof(int)).

So, inumInts * 32 = 1024.

Thus, inumInts = 32.

t1 is j * (jnumInts * (inumInts/sizeof(int)), becasue there is 1 j for every row of i.

So, jnumInts * 1 = 32.

Thus, jnumInts = 32.

t2 is k * (knumInts * (inumInts/sizeof(int) / ((inumInts*jnumInts)/sizeof(int)))). (because there is one i and i rows of j for every k)

So, knumInts * 1/2 = 4.

Thus, knumInts = 8.

Thus, int X[32][32][8].

Sign up to request clarification or add additional context in comments.

1 Comment

technically sound and very well explained. Hope you don't mind if i take some time
2

Not enough info. I'll try to prove it to you:

To make our life easier, let's divide all values by 4, since that's the size of an integer(considering a character size of 8 bits). That leaves us with:

multiplier of i: 256; multiplier of j: 8; multiplier of k: 1.

k must be 1, because it's the last index used, witch means it has to jump only 1 integer to get to the next one in the row.

j, on the other hand, has to jump 8 integers, so it can get to the same position on the next row. That means each row has 8 integers. And we have our value for k. Our array X now looks like: X[i][j][8]

i has to jump through 256 integers to get to the next column. Since a row has 8 integers, and 256/8 = 32, that means each column has 32 rows, leaving array X as: X[i][32][8]

finally, we need to know how many pages the array has. But there's no way to know that, since we would need the full size of the array in bytes, so we can divide it by 256 and then know the number of pages. That leads us back to the beginning of this answer: There's simply not enough info.

1 Comment

Thanks for nice explanation, i typed the question in its as is form. I will also edit the question to include exam papers in its original form as soon as posible.
2

Exp: It is given that Size of int is 4B and of char is 1B. The memory is byte addressable. Let the array be declared as Type X[A][B][C] (where Type = int/char and A,B,C are natural numbers).

From t0 = i*1024, we conclude that B*C*(size of Type) = 1024. 
From t1 = j*32, we conclude that C*(size of Type) = 32. 
From t2 = k*4, we conclude that size of Type = 4. 
Type = int, and 
C = 8, and 
B = 32.

Comments

1

The first dimension of the array has no effect on the address calculation. The sizeof(int) does have an effect on the address calculation. So it might help to rewrite answer a) as

X[][32][8][4]
     i  j  k

where the last [4] represents the sizeof(int). So the address calculation is

(k * 4) + (j * 8 * 4) + (i * 32 * 8 * 4) = i * 1024 + j * 32 + k * 4

From this, I would conclude that both a) and c) are correct answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.