Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CS 2073 Lab 10: Matrix Multiplication Using Pointers

Chia-Tien Dan Lo
Department of Computer Science, UTSA

I Objectives
• Show how to manipulate commandline arguments in C
• Demonstrate your ability to read matrices from files

• Demonstrate your ability to use pointers for matrix multiplication

II Hand-in Requirements
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.)

III Details
In this assignment, you will modify the program written in Lab 8 to
1. read two integer matrices from two files, one for each,
2. initialize two chunks of memory to keep values read from files, and
3. multiply the two matrices and put the result in another chunk of memory.
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 A m×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:

X
n−1
Mi,j = Ai,k ∗ Bk,j
k=0

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

1
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.

IV Setup
1. Project name: Lab10
2. Solution name: MyCPrograms
3. Create new source with name: main.c util.c
4. Create a header with name: util.h
5. Save the source and header
6. Compile and run the program.

V Coding
1. Write a main function shell in main.c.
2. Write a header util.h.
3. Write down comments whenever applicable.
4. Implement all the functions.

VI Testing
Compile and run your program until there are no errors.
1. You may test your program at least once on each statement.
2. Try lab10 matrix a.txt matrix b.txt in the commandline.
3. Try lab10 matrix b.txt matrix a.txt in the commandline.
4. Try lab10 matrix a.txt in the commandline.
5. Try lab10 matrix c.txt matrix b.txt in the commandline.

You might also like