Download Lab8 - KSU Web Home

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Determinant wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Matrix calculus wikipedia , lookup

Transcript
CS 2073 Lab 8: Matrix Multiplication Using Arrays
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 C arrays 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 be writing a program to
1. read two integer matrices from two files, one for each,
2. initialize two C 2-dimensional arrays to keep values read from files, and
3. multiply the two arrays and put the result in another array.
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!
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
X
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.
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:
1
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.
IV
Setup
1. Project name: Lab8
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 lab8 matrix a.txt matrix b.txt in the commandline.
3. Try lab8 matrix b.txt matrix a.txt in the commandline.
4. Try lab8 matrix a.txt in the commandline.
5. Try lab8 matrix c.txt matrix b.txt in the commandline.
2