

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


All laboratories will be submitted electronically through WebCT. Zip up your entire project folder to submit as the source. (Right click on the laboratory folder and follow the SentTo link.)
In this assignment, you will be writing a program to
The files used to store the matrices are of the following format: The first two integers represent the number of rows (m) and the number of columns (n) of a matrix, say Am×n. The rest of numbers in the file represent the element of Ai,j accordingly. Altogether, there are m × n + 2 integers in the file. Your program should check if there are enough integers in the file or there are too many numbers in it. If not, output an error message and terminate the program. Note that we use the first two integers to tell f scanf () to expect how many reads it should perform. Otherwise, it is hard to get all the inputs accordingly! Your program will read integers from files and initialize two C arrays accordingly. Then check if the two matrices are multiplicable (i.e., the number of columns of the first matrix must be equal to the number of rows of the second matrix). If not, output an error and terminate the program. Once matrices have been read, implement the following matrix multiplication algorithm:
Mi,j =
n∑− 1
k=
Ai,k ∗ Bk,j
where Am,n and Bn,r are two integer matrices, and Mm,r is the product of A and B. Print out the resultant matrix Mn,r in a readable format. In the exercise, we will pass two arguments from the command line to the main() function. The two arguments indicate the file names (matrix a.txt and matrix b.txt) for two matrices:
lab8 matrix_a.txt matrix_b.txt
The above files and another test file matrix c.txt are provided and will be downloaded from WebCT. Your program has to be able to pass for the sample files. Here is the pseudo code for this program:
int main(int argc, char *argv) { step 1: check if the command line arguments are passed correctly step 2: read the first matrix from file and store it in an array step 3: read the second matrix from file and store it in an array step 4: check if the two matrices are multiplicable step 5: multiply the two matrices and store the product in an array step 6: print out the product in readable format } / main() */
Implement a fuction call for each step and place function prototypes in the header file util.h (you may implement a function for both step 2 and step 3). Since we don’t know the dimension of a matrix, normally we will get dynamic memory for it. However, to simplify the design, you can define large arrays beforehand, and check if the inputs reach the array bounds. The dimensions can be defined as macros and placed in the header util.h.
Compile and run your program until there are no errors.