

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 modify the program written in Lab 8 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! Most importantly, this information gives the amount of memory required to store the matrices. We will use malloc() to request memory dynamically and free() to return memory back to the system. Your program will read integers from files and initialize two chunks of memory in row-major order 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. For simplicity, assume that the files contains two-dimensional matrices. 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. Because the elements are stored linearly, you will have to write a function to convert from two indices to the offset in which the element is stored in the chunk of memory. 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:
lab10 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 dimensions and allocate memory for the first matrix step 3: read the first matrix from file and store it in memory step 4: read dimensions and allocate memory for the second matrix step 5: read the second matrix from file and store it in memory step 6: check if the two matrices are multiplicable step 7: allocate memory for the product step 8: multiply the two matrices and store the product in memory step 9: print out the product in readable format } / main() */
Implement a function call for each step and place function prototypes in the header file util.h. You may define macros or prototypes in the header util.h.
Compile and run your program until there are no errors.