Write a program to exchange the values in two different ...



CSCI 515 C/C++ Programming Fall 2002

Dr. Creider

CLab 9 Assignment (In lab class)

Comparison of Dynamic Allocated Two Dimensional Array

in row major and column major storage models

The purpose of this program is to write a series of functions to manipulate a dynamically allocated block of memory as a two dimension array stored in row major representation or column major representation. For each of these functions you must write the algorithm as efficiently as possible. “Efficiently as possible” means that you must use a simplified addressing method in the functions.

Write a main program with a loop that inputs a storage model code, input order code, and two dimension array sizes. The storage model codes are: 1 for row major, 2 for column major. If the user enters a storage model code of 0, then terminate the program. The input order codes are: 1 for input by rows, 2 for input by columns. Enter the dimension sizes for the array and the call the functions as described below.

Write a function to dynamically allocate the array and enter the data for the array stored in row major. The same function will input the data by rows or by columns depending on the input order code and store the data in row major order. Return the address of the block of memory using a return statement. Pass to this function the input order code and the dimension sizes of the array that is to be allocated. This code will require two separate segments of code to enter the values in either input order. Do not use the row major random addressing method of

*(ptr + row_subscript * number of columns + column_subscript).

Write a function to dynamically allocate the array and enter the data for the array stored in column major. The same function will input the data by rows or by columns depending on the input order code and store the data in row major order. Return the address of the block of memory using a return statement. Pass to this function the input order code and the dimension sizes of the array that is to be allocated. This code will require two separate segments of code to enter the values in either input order. Do not use the column major random addressing method of

*(ptr + row_subscript + column_subscript * number of rows).

Write a function to print a two dimension array (in a two dimension graphical representation) that is stored in row major order. Use an efficient addressing method of *ptr++ to print the value of each element. After you have printed the graphical representation, print the memory image of the array by printing the values separated by one space on the same line. If there is a large number of elements the value will wrap around the screen.

Write a function to print a two dimension array (in a two dimension graphical representation) that is stored in column major order. Use an efficient addressing method of *ptr where ptr is incremented by the number of rows in the array as you print the values in each row of the array. After you have printed the graphical representation, print the memory image of the array by printing the values separated by one space on the same line. If there is a large number of elements the value will wrap around the screen.

In main after the user enters the storage model code, the input order code, and the dimension sizes, call the appropriate functions for the storage model type to enter the data and then print the array. If you enter the same data for both storage models then the graphical representation of the two dimensional array displayed should look the same for both models. After displaying these results, ask the user for another storage model code to determine if the program will continue.

Name the file you create, CLAB9. Do Not Use Any Global Variables. Save both the source (cpp file) and object code (exe) on your disk which is turned in at the end of the lab.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download