• edited by
68,172 views
144 144 votes

For a C program accessing $\mathbf{X[i] [j] [k]}$, the following intermediate code is generated by a compiler. Assume that the size of an integer is $32$ bits and the size of a 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?

  1. $\mathbf{X}$ is declared as "int $\mathbf{X[32] [32] [8]}$”.
  2. $\mathbf{X}$ is declared as "int $\mathbf{X[4] [1024] [32]}$”.
  3. $\mathbf{X}$ is declared as "char $\mathbf{X[4] [32] [8]}$”.
  4. $\mathbf{X}$ is declared as "char $\mathbf{X[32] [16] [2]}$”.

10 Answers

8 8 votes

Prerequisite: The visualisation of multidimensional arrays.


How do we get to $A[i][j]$ ? (in RMO)

From the base address, we skip $i$ rows and $j$ columns. This is quite well known.

See this or this for example.

 

Actually, a better perspective is to see it as skipping $i$ arrays and $j$ elements.

An even better perspective is to look at it as skipping $i$ $1D$ arrays, and $j$ elements.

 

By extending this, $A[i][j][k]$ is nothing but skipping $i$ $2D$ arrays, $j$ $1D$ arrays and $k$ elements. You can extend this generalised perspective to any number of dimensions.

 

Now,

t0 = i ∗ 1024 
t1 = j ∗ 32
t2 = k ∗ 4 

This is nothing but "skipping". Start from the innermost index. We're skipping $k$ by multiples of $4$. So, single-elements in the array occupy space of $4$ (Bytes). Hence, we're dealing with ints.

 

We're skipping $j$ by multiples of $32$. This means the $1D$ "sub-arrays" are of size $32$ (Bytes).

//Visualisation of multidimensional arrays is needed. If you don't know how to picturise it, I'll draw that in the comments.

 

Size of the $1D$ array = $32$ .

Number of elements = $\frac{32}{4}=8$

So, the last array subscript has a size 8.

 

With this knowledge, Option A is the answer.

But let's continue.

 

We're skipping $i$ by multiples of $1024$. This means the $2D$ "sub-arrays" are of size $1024$ (Bytes).

Number of elements in $2D$ subarrays = $\frac{1024}{32}=32$

Hence, there are $32$ $1D$ subarrays.

So the middle subscript is of size 32.

 

Nothing can be concluded about the first subscript. (Why? Hint: we don't know the total number of 2D subarrays)

 

So, we conclude the array must have been declared like: $int X[?][32][8]$

6 6 votes

Size of an integer = 32 bits = 4 Bytes
Size of a character = 8 bits = 1 Byte
Let array be

            type x[A] [B] [C]
type  may be integer / character

we want  t5=x[t0+t1+t2]=x[i*1024 + j*32 + k*4 ] element,

From t0 = i *1024,
we can conclude that

          B * C * (size of type) = 1024
From t1 = j *32, we can conclude that

         C *(size of type) = 32
From t2 = k *4, we can conclude that   

         (size of type) = 4
therefore  type = int
                    C *4 = 32    => C = 8
                    B *8 * 4 = 1024 => B = 32

Choice (A)

0 0 votes

A 3d array defined as a[2][2][2] means that there are 2 2d arrays each of dimension 2x2, i.e., both of them have 2 rows and 2 columns. 

Suppose the array given in the question is A[x][y][z]. This means that there are x 2d arrays each having y rows and z columns.

In the questions, the equations for finding A[i][j][k] can be re written in the form of y and z as follows if the array is assumed to be of type 'int':-

t0 = i ∗ 1024 = i * (y*z) * 4
t1 = j ∗ 32 = j * z * 4
t2 = k ∗ 4 = k * 4

So from the above equations we get,

z*4=32

=> z=8

and y*z*4=1024

=> y=32

So we know that dimensions are A[x][32][8]. Since we assumed int, so A is the answer since it is of this form.

If you assume array to be char then,

t0 = i ∗ 1024 = i * (y*z) * 1 

t1 = j ∗ 32 = j * z * 1 

t2 = k ∗ 4 = k * 4

So from the above equations we get,

z*1=32

=> z=32

and y*z*1=1024

=> y=32

So none of the char options,i.e, options B and C are of the form A[x][32][32]. So A has to be the answer.

Answer:
Position:
Show:

Related questions

66 66 votes
10 answers 10 answers
17.7k
17.7k views
go_editor asked Sep 28, 2014
17,665 views
Consider the expression tree shown. Each leaf represents a numerical value, which can either be $0$ or $1$. Over all possible choices of the values at the leaves, the max...
31 31 votes
3 answers 3 answers
11.7k
11.7k views
go_editor asked Sep 28, 2014
11,682 views
Consider the grammar defined by the following production rules, with two operators $∗$ and $+$$S\:\to\:T∗P$$T\:\to\:U\mid T∗U$$P\:\to\:Q+P\mid Q$$Q\:\to Id$$U\:\to Id$Whi...
224 224 votes
5 answers 5 answers
29.9k
29.9k views
go_editor asked Sep 28, 2014
29,945 views
Suppose $n$ and $p$ are unsigned int variables in a C program. We wish to set $p$ to $^nC_3$. If $n$ is large, which one of the following statements is most likely to set...
40 40 votes
4 answers 4 answers
13.5k
13.5k views
go_editor asked Sep 28, 2014
13,464 views
Which one of the following is NOT performed during compilation?Dynamic memory allocationType checkingSymbol table managementInline expansion