[ Last web update: September 13, 2010 ]  Contact: roabel@engineering.uiowa.edu

Department of Electrical & Computer Engineering
57:017 Computers in Engineering

Fall 2010

CIE Homework #2: Matrix Operations using Arrays and Functions

Due Date: 10:00 PM Thursday, Sept 16, 2010.

Submission: C code and pseudocode should be submitted using SVN.

Overview: Homework #2 is intended to give you practice using arrays with function.

Write subroutines to perform the following matrix operations: read, print, add, subtract, and transpose. Write a main program to test your subroutines. For this homework, a matrix will consist of an array of floating point numbers, a variable for the row dimension, and a variable for the column dimension.

Main Program: Matrices for this homework assignment will be stored in 1D floating point arrays (not in 2D arrays) and will have associated variables to hold the row and column dimensions. Each array will hold a maximum of 100 numbers. Use a global constant to define the maximum size of the 1D array. In the main program, define three 1D float arrays to hold the values of the matrices and define variables to hold the row and column dimension for each matrix. Remember to initialize all your variables before you use them.

Your main program should perform the following steps

  1. Prompt the user to enter matrix A. This means that the user will have to enter the row and column dimensions and the matrix values.
  2. Print matrix A.
  3. Transpose matrix A.
  4. Print matrix A.
  5. Prompt the user to enter matrix B.
  6. Print matrix B.
  7. Add matrix A and B together and store the result in matrix C. Note: The program should print out an error message if the dimensions of A and B are different and prompt the user to re-enter the matrices until both matrices have the same dimension. Note: The Add subroutine should return an error code to the main program if the Add operation could not be performed. If the Add operation could not be performed, the Read operations should be called from the main program to fix this problem. The Add matrix subroutine should not call the Read subroutine itself.
  8. Print matrix C.
  9. Subtract C from A and put the result in A .
  10. Print matrix A.

Read subroutine: This subroutine should prompt the user to enter the number of rows and columns for matrix being entered. The subroutine should check to make sure that the row and column dimensions are positive and their product is less than the maximum size of the array. The function should continue to prompt the user for input until valid input is entered. The function will then read the values of the matrix into the array. The output of the subroutine should be updated values for the row, column, and array values.

Print subroutine: Your program should have a subroutine for printing the elements of a matrix in a nice format.

Add subroutine: This subroutine will take as input two matrices, the row and column dimensions of the two input matrices, and return the sum of the two matrices as an array and the row and column dimensions of the result array. The add subroutine should check to see if the two input matrices are the same size or not. The add subroutine should return an error condition to the main program if the dimensions are different.

Subtract subroutine: The subtract subroutine is similar to the add routine except that the the two input matrices are subtracted.

Transpose subroutine: This subroutine will take as input a matrix, the row and column dimensions of the matrix, and return the transpose of the input matrix.

All of your subroutines should detect errors and print warnings when they occur. For example, your program should display warnings if the matrix dimensions are inconsistent when trying to perform an addition operation or if any of the input matrices contain invalid data.

1a. [30pts] Pseudocode. You should write your pseudocode before you write your program.

1b. [30pts] Write a C program that implements the above specifications. Your C program must be similar to your pseudocode. Use structured programming techniques to write your program. You should not use the "goto" command and should only use a "break" command inside a "switch" statement.

1c. [15pts] Properly indented code and code documentation. See Style Guide.