CSCI 515                                                   C/C++ Programming                                                  Fall 2002

Dr. Creider

Lab 9 Assignment

Two Dimensional Array manipulation using Dynamically Allocated Memory and pointers

 

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 of type short.  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. Ask the user to enter the number of rows (m) and columns (n) of data that they want to manipulate.  The number of rows and columns that the user enters may or may not define a square matrix (when the number of rows equals the number of columns).  The array will have exactly rows times columns (m*n) elements.  It will not contain any extra or empty cells.

 

Function one will dynamically allocate a block of memory which represents a two dimension array based on the input values supplied by the user. Initialize the “matrix” by rows such that the first element 0 or [0][0] has the value of 1 and each successive element of the matrix has the next value in ascending order, e.g. [0][1] is 2, [0][2] is 3, [0][3] is 4, etc.  Pass to the function three arguments.  Pass the first argument by reference so that you can assign the address of the dynamically allocated block of memory to this argument.  The second and third arguments will be the number of rows and columns of the array.

 

Function two will rotate the data in the matrix in an up/down method such that the first row is exchanged with the last row (swap row 0 and row m-1, swap row 1 and row m-2, etc.).

 

Function three will rotate the data in the matrix in a left/right method such that the first column is exchanged with the last column (swap column 0 and column n-1, swap column1 and column n-2, etc.).

 

Function four will rotate the data in the matrix by the left diagonal.  This operation is usually referred to as a matrix transpose.  This is done by exchanging element [i][j] with element [j][i].  The values on the left diagonal do not move for a square matrix.  Do not forget to swap the values of the number of rows and columns.  Pass these two arguments by reference so that you can change their values.

 

Function five will rotate the data in the matrix by the right diagonal.  This operation will require that you exchange element [0][0] with element [m-1][n-1].  The values on the right diagonal do not move for a square matrix.  Do not forget to swap the values of the number of rows and columns.  Pass these two arguments by reference so that you can change their values.

 

Function six will print the dynamically allocated array 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.

 

Use type short for the dynamically allocated block of memory.  Call the first 5 functions in the order given.  After each function has been called you must print the matrix and pause the program so that the results of the operation can be viewed.  After the results of the operation of function five has been printed, free the block of memory.  Continue running the program until the user want to quit.  You cannot use subscripts in this program and you cannot use any structure which is an array of pointers.  Clearly label each matrix with the operation that was performed on the matrix.

 

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