0

I am looking at past exams for a first year computer science course and I am confused about one question. I have no idea what it's asking. I am not asking someone to do it for me, but I would appreciate if someone could help me understand what the question wants me to do.

Write a complete C program to allocate, initialize, print, and de-allocate a three-dimensional array of int type variables, according to the specifications below. The sizes of the three array dimensions x, y, and z, are 3, 2, and 4, respectively.

The array elements should be initialized according to the following function

f(x,y,z) = 5x + 6y + 7z

Which means your initialization code will look like this:

myArray[x][y][z] = 5 * x + 6 * y + 7 * z;

Here are some sample outputs:

0 7 14 21
6 13 20 27

5 12 19 26
11 18 25 32

10 17 24 31
16 23 30 37

Firstly, I do not understand what the question is asking. The only pattern I see is that each value is the prior value + 7.

EDIT: Facepalm. Thanks Andy. I thought it was totally something else.

Jason
  • 1,297
  • 12
  • 24
  • 2
    x,y, and z are the indexes into each dimension of the array. The formula dictates the value that goes at that precise cell in 3D array. e.g. myArray[0][0][0] = 5(0) + 6(0) + 7(0) = 0. myArray[0][0][1] = 5(0) + 6(0) + 7(1) = 7. myArray[1][1][1] = 5(1)+6(1)+7(1) = 18 – AndyG Aug 20 '14 at 21:50
  • 1
    allocate:`malloc`, initialize:3 X for a[x][y][z]=f(x,y,z), print: for and `printf`, and de-allocate:`free` – BLUEPIXY Aug 20 '14 at 21:57

2 Answers2

2

There is no any question in the assignment. There is the following request

Write a complete C program to allocate, initialize, print, and de-allocate a three-dimensional array of int type variables, according to the specifications below

What is not clear in this statement?

And there is an example how each element of the array shall be initialized

myArray[x][y][z] = 5 * x + 6 * y + 7 * z;

So you need to write three nested loops each loop for each dimension of the array.

For example

for ( int x = 0; x < 3; x++ )
{
   for ( int y = 0; y < 2; y++ )
   {
      for ( int z = 0; z < 4; z++ )
      {
         myArray[x][y][z] = 5 * x + 6 * y + 7 * z;
      }
   }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

They want you to allocate, fill in and deallocate an 3D array, so you probably need

  1. use malloc() to allocate your 3D array:
    • first create a 1-dimensional array which length is 3.
    • each element of that array should hold a pointer to another array of length 2 (so there will be 3 arrays of length 2)
    • and finally each element of that 2-element arrays should hold a pointer to a 4-element array (so there will be 3*2 arrays of length 4)
  2. use 3 nested loops to fill the arrays with numbers calculated with the formula myArray[x][y][z] = 5 * x + 6 * y + 7 * z;
  3. deallocate the arrays with free().
andrew_m
  • 121
  • 3
  • It was my understanding that 3D arrays do not contain pointers to elements, but rather they are flat? I.e. x[3][3] is the same as x[9] and is accessed through multiplication? – Jason Aug 20 '14 at 22:20
  • I agree. You can create the array as int x[3][2][4]. But if you create such a global array, you cannot deallocate it (if you create such an array in a function, the array will be deallocated when the function returns). The question wants you to allocate and deallocate the array, so I understand they want you to use malloc and free. – andrew_m Aug 20 '14 at 22:23
  • 1
    @Jason `int (*a)[3][2][4] = malloc(sizeof(*a));` or like this. – BLUEPIXY Aug 20 '14 at 22:29
  • I thought about something like int*** myArray = (int***)malloc(3*sizeof(int**)); for(int i=0; i<3; i++){ int** row = (int**) malloc(2*sizeof(int*)); myArray[i] = row; for(int j=0; j< 2; j++){ int* innerRow = malloc(4*sizeof(int)); row[j] = innerRow; // or myArray[i][j] = innerRow; } } – andrew_m Aug 20 '14 at 22:43
  • @andrew_m it's not linear. – BLUEPIXY Aug 20 '14 at 22:45