CSCI 515                                                        C/C++ Programming                                                      Fall 2002

Dr. Creider

Lab 10 Assignment

Three Dimensional Array manipulation using Dynamically Allocated Memory

 

The purpose of this program is to write a series of functions to manipulate a dynamically allocated block of memory as a three dimension array.  For each of these functions you must write the algorithm as efficiently as possible.  “Efficiently as possible” means with the least amount of memory and the fastest algorithm.

 

Write a function to dynamically allocate a block of memory which represents a three dimension array based on the input values supplied by the user.  Ask the user to enter the number of pages (p), the number of rows (m) and columns (n) of data that they want to manipulate.  The number of pages, rows and columns that the user enters may or may not define a cube (where the number of pages, rows and columns are equal).  The array will have exactly pages times rows times columns (p*r*c) elements.  It will not contain any extra or empty cells.  Ask the user to select an option to initialize that array with input from the keyboard or have the program initialize the array (option 1 will initialize from the keyboard and option 2 will initialize in the program).  If the option is 2, initialize the structure by rows by pages such that the first element 0 or [0][0][0] has the value of 1 and each successive element of the matrix has the next value in ascending order, e.g. [0][0][1] is 2, [0][0][2] is 3, [0][0][3] is 4, for all elements of the array.  Pass four arguments by reference so that you can assign the address of the dynamically allocated block of memory to the first argument and assign the number of pages, rows and columns of data to the second, third and fourth arguments.

 

Write a function to find the row slice with the largest and smallest sums.  A row slice is the same row number for all pages of the three dimension array.  Assign the value of the largest and smallest sums and the corresponding row numbers to arguments using pass by reference.

 

Write a function to find the column slice with the largest and smallest sums.  A column slice is the same column number for all pages of the three dimension array.  Assign the value of the largest and smallest sums and the corresponding column numbers to arguments using pass by reference.

 

Write a function to rotate the pages of the array so that the last page becomes page 0, page 0 becomes page 1, page 1 becomes page 2, etc.  Use only one temporary memory location to move the data as described in the previous sentence.

 

Write a function to print the dynamically allocated array by pages in a two dimension format with rows and columns as you would normally print a compile time defined two dimension array.  Be sure that the columns are aligned correctly for display purposes.  Print the pages in order and label the output with the page number.

 

Use type short for the dynamically allocated block of memory.  After the array is initialized you must print the structure and pause the program after each page so that the results of the operation can be viewed.  Call the function to find the sums for the row slice and print the results in main.  Call the function to find the sums for the column slice and print the results in main.  Rotate the array, print the array and then free the block of memory.  Continue running the program until the user want to quit.  Use a “do while” loop so that the program runs at least one time.  You cannot use subscripts in this program and you cannot use any structure which is an array of pointers.  Clearly label each output with the operation that was performed on the array.

 

Name the file you create LAB10.  Do not use any global variables for this assignment.  Assignment due October 29, 2002.  Hand in a hard copy of the source code, and a floppy on which is saved the source file and executable file.