CSCI 515 Introduction to C Programming Spring 2000



CSCI 515 C/C++ Programming Fall 2002

Dr. Creider

Lab 7 Assignment

Two Dimension Matrix Manipulation using Pointer Addressing

Write a C/C++ program that performs matrix multiplication and matrix transposition. Create three (3) two dimension short arrays in which to perform matrix manipulations: array_a has 10 rows and 8 columns, array_b has 7 rows and 9 columns, and array_c has 10 rows and 10 columns. Write the following functions using pointer addressing instead of subscripts in all functions.

Function 1 - Write a function to read the number of rows and columns of data the user wants to use in manipulating the two dimension array, array_a. The user must be permitted to enter separate row and column sizes for the amount of data to be entered in the array. The row and columns sizes do not have to be the same. Make sure that the values entered are in the correct range for array that has been passed to this function. This should be a generic function so you must pass the starting address of the array and the dimension sizes of the array to the function so that you do not use any constants in your code. Pass two other arguments by reference which will be assigned the number of rows and columns entered by the user. After you have entered the values for the number of rows and columns, assign values to the array based on the sizes entered so that all elements of row 0 are assigned a value of 1 and all elements of each row are assigned a value which is one greater than its subscript.

Function 2 - Write a function to copy all values from one two dimension array to a second two dimension array (array_a to array_b). Pass to the function the starting address for each of the two arrays, the number of rows and columns of data in the first array, and the dimension sizes for both arrays. Pass by reference two variables which represent the number of rows and columns of data to be stored in the second array. These values must be assigned in the function. You must check to make sure that there is enough room in the second array before you copy the values. Return a 0 if you could copy, otherwise return a 1using a return statement. If you could not copy, in main write an error message and skip the remaining functions.

Function 3 - Write a function to transpose one matrix (array_b). When a square matrix is transposed the values on the left diagonal do not move. All remaining elements of the array in the form of x[i][j] and x[j][i] are swapped. However, if the array is not square, such as an array with 5 rows and 3, you should not swap the non-square part of the matrix. Test to determine if the number of rows and columns of the array that is to be transposed is square. If the array is not square, move the non-square part (rows or columns) to the correct location and then transpose the square part. Be sure that you swap the values of the number of rows and columns before the function terminates. Pass to the function the starting address of the array, the number of rows and columns of data in the array, and the dimension sizes for the array. Check to determine if you can transpose the array based on the dimension sizes of the array. If you can transpose the array return a 0, otherwise return a 1 using a return statement. If you could not transpose the array, in main write an error message and skip the remaining functions.

Function 4 - Write a function to multiply matrices. Arrays can be multiplied if the number of columns in the first array matches the number of rows in the second array. The size of the product array is the number of rows of the first array and the number of columns of the second array. Pass the starting address of the three arrays to the function, the number of rows and columns of data in the arrays to be multiplied, and the dimension sizes for all arrays. The number of rows and columns for the third array (product array, array_c) that will be created by multiplication must be passed by reference. Check that multiplication can occur by checking that the number of columns in the first array equals the number of rows in the second array and that the result can be stored in the third array. If you can multiply return a 0 otherwise return a 1 using a return statement. If you could not multiply the arrays, in main write an error message and skip the remaining functions. The matrix multiplication code segment is as follows.

for (i = 0; i < arows; i++)

for(j = 0; j< bcols; j++) {

sum = 0;

for (k = 0; k ................
................

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

Google Online Preview   Download