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.

2
  • 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 Commented Aug 20, 2014 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 Commented Aug 20, 2014 at 21:57

2 Answers 2

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;
      }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@AndyG And I did not do it.:)
Fair enough :-) I wouldn't exactly call this a complete program either.
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().

5 Comments

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?
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.
@Jason int (*a)[3][2][4] = malloc(sizeof(*a)); or like this.
I thought about something like <code> 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; } }</code>
@andrew_m it's not linear.

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.