Monday, May 24, 2010

Guidance on C++ question?

I've been sick for the last 2 weeks so I'm not fully caught up in my class. I only have 5 hours to do this assignment as thats all the time they give us so I have until mid-night. I'm not asking anyone to "do" this, im just asking for help but code examples would help me understand how to do this.





Write a function that adds the elements of two 3-D arrays of size [n] [2] [2]. Create and initialize a 3-D array with n=2 where all of the elements are initialized to 2. Test the function by using it to add the array to itself. Next create a 3-D array with n= 3 where all of the elements are initialized to 3. Again test the function by using it to add the array to itself.





2: Write a function to print out the elements of arrays of size [n] [2] [2]. Use the function to print the results from the above two additions.





3: Write another function to add the elements of 3-D arrays of size [n] [2] [2], except use pointer arithmetic inside the function to implement the addition. Test the function.

Guidance on C++ question?
an array is a contiguous section of memory used to store data, and it can be accessed only by index.





to declare a 3-d array, do so like this:





int array[2][2][2];





and the first element in that array is then array[0][0][0]





you can think of a 3d array like a cube - having a width, height, and depth.





so you'll need three nested for loops to iterate through your 3d arrays, like





void iterate(int n, int[][][] array) {


int i1, i2, i3 = 0;





for(i1 = 0; i1 %26lt; n; i1++) {


for(i2=0;i2%26lt; n; i2++) {


for(i3=0;i3%26lt; n; i3++) {


printf("this is the element: %d\n", array[i1][i2][i3]);


}


}


}





hope this helps a little!


}


No comments:

Post a Comment