Lecture 22 – Nov 1st, 2019
Setup
- Log in to clyde.
$ cp -r ~steve/ex/mda .
Task
Implement the function
read_array
inarray.c
. It should read in a rectangular (2D) array of integers from the specified file into an array and return the array along with the number of rows and columns. Seearray.h
andarray.c
for details.The first line of the file contains two integers: the number of rows and the number of columns. Following this are number-of-rows lines each of which contains number-of-columns integers. For example,
5 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
represents a 2D array with 5 rows and 3 columns (this file is in
small.txt
)- Implement the function
write_array
inarray.c
. It should first write out the number of rows and then columns on the line followed by the data of the array to the file. - Test that your code works by making
copyarray
($ make copyarray
) and then trying to copy the array fromsmall.txt
intocopy.txt
.$ ./copyarray small.txt copy.txt
Check that the output matches what you expect.
Implement the missing functionality in
transpose.c
. It should take in an array from one file, compute the transpose, and write it out to the second file.The transpose of the small array above is the array
Create a new program
matrixmult
(and add it to theMakefile
by addingmatrixmult
to thebins
variable) with a correspondingmatrixmult.c
This program should take three arguments. The first two should be the paths to files containing arrays and the third is the output file. Compute the matrix multiplication of the two input arrays and write the result to the output file.Note that if you have a matrix and a matrix, then the result of multiplication is a matrix. Otherwise, the multiplication isn’t defined.