matmult/Homework3.pdf
Names of Files to Submit: matmult.s, knapsack.s, combs.s ReadMe.txt
• If you are working in a group ALL members must submit the assignment
• All programs should compile with no warnings when compiled with the -Wall option
• All prompts for input and all output must match my prompts/output. We use a program to grade
your work and tiny differences can cause your work to be marked as a 0.
◦ The best way to avoid being deducted points is to copy the prompt/unchanging portion of
the outputs into your code. Make sure to get the spaces as well.
• You must also submit a file called ReadMe.txt. Include the names of all partners and any
trouble you had on the assignment
• An example ReadMe.txt has been included with this assignment
• The input in the examples has been underlined to help you figure out what is input and what is
output
• Submit your work on Smartsite by uploading each file separately. Do not upload any folders or
compressed files such as .rar, .tar, .targz, .zip etc.
• If you have any questions please post them to Piazza
• RESTRICTIONS:
◦ For all programs in this homework you
▪ May not have a data section. If you need extra space you must use the stack.
▪ Not call any C functions that you write. You may make use of any of the library
functions.
1. (Time: 3.5 hours. Lines of Code 257) Write a program called matmult.s that implements matrix
multiplication in assembly. If you don't know how to do matrix multiplication your can find a
tutorial here.
1. This program should be callable from C and have the following signature:
1. int** matMult(int **a, int num_rows_a, int num_cols_a,
int** b, int num_rows_b, int num_cols_b);
2. This function should multiply matrices a and b together and return the result
3. You must allocate space for this new matrix by calling malloc
2. You have been given a C file called main.c that accepts as command line arguments the
names of two files that contain the matrices to be multiplied. main.c will read in these
matrices, call your function, and then display the result. After it has displayed the result it
will then free the space that has been malloced.
1. Your function must be callable by this file
3. You have also been given a makefile that should compile your program. Your program
MUST be able to be compiled by this makefile. For those of you running 64 bit versions of
Linux you may need to install the 32 bit binaries. I was able to do this on my machine by
installing multilib for gcc. To do this I clicked on the Ubuntu Software App and then
searched for multilib. I selected the one for gcc, installed it, and everything was good to go.
If that doesn't work for you, you might find this this post helpful. If neither solution works
for you please Google and see what you can find. If you find a solution that works for you
please post it to Piazza.
Example:
cat mata/0-test.txt
3
3
470 -192 -539
235 -814 -538
-503 -418 541
cat matb/0-test.txt
3
3
31 ...
matmultHomework3.pdfNames of Files to Submit matmult..docx
1. matmult/Homework3.pdf
Names of Files to Submit: matmult.s, knapsack.s, combs.s
ReadMe.txt
• If you are working in a group ALL members must submit the
assignment
• All programs should compile with no warnings when compiled
with the -Wall option
• All prompts for input and all output must match my
prompts/output. We use a program to grade
your work and tiny differences can cause your work to be
marked as a 0.
◦ The best way to avoid being deducted points is to copy the
prompt/unchanging portion of
the outputs into your code. Make sure to get the spaces as well.
• You must also submit a file called ReadMe.txt. Include the
names of all partners and any
trouble you had on the assignment
• An example ReadMe.txt has been included with this
assignment
• The input in the examples has been underlined to help you
figure out what is input and what is
output
• Submit your work on Smartsite by uploading each file
separately. Do not upload any folders or
compressed files such as .rar, .tar, .targz, .zip etc.
2. • If you have any questions please post them to Piazza
• RESTRICTIONS:
◦ For all programs in this homework you
▪ May not have a data section. If you need extra space you must
use the stack.
▪ Not call any C functions that you write. You may make use of
any of the library
functions.
1. (Time: 3.5 hours. Lines of Code 257) Write a program called
matmult.s that implements matrix
multiplication in assembly. If you don't know how to do matrix
multiplication your can find a
tutorial here.
1. This program should be callable from C and have the
following signature:
1. int** matMult(int **a, int num_rows_a, int num_cols_a,
int** b, int num_rows_b, int num_cols_b);
2. This function should multiply matrices a and b together and
return the result
3. You must allocate space for this new matrix by calling
malloc
2. You have been given a C file called main.c that accepts as
command line arguments the
names of two files that contain the matrices to be multiplied.
main.c will read in these
matrices, call your function, and then display the result. After it
has displayed the result it
will then free the space that has been malloced.
3. 1. Your function must be callable by this file
3. You have also been given a makefile that should compile
your program. Your program
MUST be able to be compiled by this makefile. For those of you
running 64 bit versions of
Linux you may need to install the 32 bit binaries. I was able to
do this on my machine by
installing multilib for gcc. To do this I clicked on the Ubuntu
Software App and then
searched for multilib. I selected the one for gcc, installed it, and
everything was good to go.
If that doesn't work for you, you might find this this post
helpful. If neither solution works
for you please Google and see what you can find. If you find a
solution that works for you
please post it to Piazza.
Example:
cat mata/0-test.txt
3
3
470 -192 -539
235 -814 -538
-503 -418 541
cat matb/0-test.txt
3
3
313 531 802
26 860 -767
543 870 822
./matmult.out mata/0-test.txt matb/0-test.txt
-150559 -384480 81146
4. -239743 -1043315 370572
125456 -155903 361902
http://www.purplemath.com/modules/mtrxmult.htm
http://askubuntu.com/questions/454253/how-to-run-32-bit-app-
in-ubuntu-64-bit
2. (Time 1.5 hours) Write a program called knapsack.s that
solves the 0-1 knapsack problem
recursively. In the knapsack problem you have a knapsack that
can hold W weight. You also
have a collection of items that each have their own weight w i
and value vi . The goal is
find the set of items that maximizes the amount of value in the
knapsack but whose weight does
not exceed W.
1. This program should be callable from C and have the
following signature
1. unsigned int knapsack(int* weights, unsigned int* values,
unsigned int num_items, int capacity, unsigned int
cur_value)
2. This function should calculate and return the maximum value
knapsack
3. This function must be implemented recursively
4. Pay very careful attention to the types in this function as it
will affect which machine
instructions you should use. Hint: it will affect the jump
instructions you use
5. You have been provided with a C file called knapsack.c that
implements this function
and should give you a good starting point
5. 1. If you want an extra challenge try solving the problem
without looking at knapsack.c
as this problem boils down to just finding the optimal
combination of items
6. You will find the leal instruction very helpful for this
problem
2. You have also been given a file called main.c that will take
as a command line argument the
name of a file containing a knapsack problem.
1. Please see the comments in main.c to see how these files are
structured
2. Your function must be callable from this file
3. You have also been given a makefile that will compile the
assignment for you as well as
create a version of the executable using only the given C files.
1. Your program must be able to be complied by this makefile
4. Example:
1. cat Tests/0-test.txt
100
4
43 43
3 38
5 17
18 25
./knapsack.out Tests/0-test.txt
123
3. (Time 1.5 Hours) Write a program called combs.s that
6. generates all the possible combinations
of a set of items of a given size.
1. Your program should be callable from C and have the
following signature
1. int** get_combs(int* items, int k, int len)
2. This function should generate all possible combinations of
items taken k at a time and return
a 2-D array where each row contains one combination
1. The combinations should be added to the 2-D array in their
natural order
2. This 2-D array should be dynamically allocated
3. As a hint you will probably need to develop a helper function
that actually computes the
combinations
3. You have been given a file called main.c that will get the
inputs and call your function
1. Your function must be callable from this file
2. You will also find some helpful functions in main.c that you
can call from your program
4. You have also been given a makefile to compile your
program.
1. Your program must be compilable by this makefile.
4. Examples
1. How many items do you have: 5
Enter your items: 1 2 3 4 5
Enter k: 3
1 2 3
1 2 4
1 2 5
7. 1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
2. How many items do you have: 5
Enter your items: 1 2 3 4 5
Enter k: 4
1 2 3 4
1 2 3 5
1 2 4 5
1 3 4 5
2 3 4 5
matmult/main.c
#include <stdio.h>
#include <stdlib.h>
int** matMult(int **a, int num_rows_a, int num_cols_a, int**
b, int num_rows_b, int num_cols_b);
void displayMat(int** mat, int num_rows, int num_cols);
void readMat(const char* file_name, int*** mat, int*
num_rows, int* num_cols);
void displayMat(int** mat, int num_rows, int num_cols){
//display the contents of the 2D matrix to the scren
//@mat: the matrix to be displayed
//@num_rows: the number of rows in the matrix
//@num_cols: the number of columns in the matrix
int i,j;
8. for(i = 0; i < num_rows; i++){//for each row
for(j = 0; j < num_cols; j++){ //for each column
printf("%d ", mat[i][j]); //display the value
}
printf("n");
}
}//displayMat
void readMat(const char* file_name, int*** mat, int*
num_rows, int* num_cols){
//read in a file that contains a matrix
//format of file is
//number of rows
//number of columns
//values separated by white space
//@file_name: the name of the file containing the matrix
//@mat: the matrix to be intialized from the file. space will be
allocated for it inside the function
//@num_rows: the number of rows in the matrix. intialized by
this function
//@num_cols: the number of columns in the matrix. intialized
by this function
int i,j;
FILE *fptr;
fptr = fopen(file_name, "r"); //open the file
//read in the dimensions
fscanf(fptr, "%d", num_rows);
fscanf(fptr, "%d", num_cols);
//make space for mat
*mat = (int**)malloc(*num_rows * sizeof(int*));
9. for(i = 0; i < *num_rows; i++){
(*mat)[i] = (int*)malloc(*num_cols * sizeof(int));
}
//initialize mat
for(i = 0; i < *num_rows; i++){
for(j = 0; j < *num_cols; j++){
fscanf(fptr, "%d", &((*mat)[i][j]));
}//for each row
}//for each column
fclose(fptr);// close the file
}//readMat
int main(int argc, char **argv){
int** mat_a;
int** mat_b;
int** mat_c;
int rows_mat_a, cols_mat_a, rows_mat_b, cols_mat_b;
int i;
if(argc < 3){
printf("matmult.out matrix_A_File matrix_B_Filen");
printf("Not enough arguments given.n");
return(1);
}
//read in matrices
readMat(argv[1], &mat_a, &rows_mat_a, &cols_mat_a);
readMat(argv[2], &mat_b, &rows_mat_b, &cols_mat_b);
//do the multiplication
mat_c = matMult(mat_a, rows_mat_a, cols_mat_a, mat_b,
rows_mat_b, cols_mat_b);
//display solution
18. the number of seconds it took the program to run
'Timed Out' if the program took too long to complete
'Crashed' if the program encountered some fatal error
"""
def __init__(self, testName, correct, timeTaken):
"""
@testName: the name of the test run
@correct: True if the output of matched the solution; False
otherwise
@timeTaken is either
the number of seconds it took the program to run
'Timed Out' if the program took too long to complete
'Crashed' if the program encountered some fatal error
"""
self.testName = testName
self.correct = correct
self.timeTaken = timeTaken
#end init
19. def __repr__(self):
if type(self.timeTaken) == str:
format_str = 'Test: {!s} | Correct: {!s} | Time Taken: {!s}'
else:
format_str = 'Test: {!s} | Correct: {!s} | Time Taken: {:.3f}'
s = format_str.format(self.testName, self.correct,
self.timeTaken)
return s
def __str__(self):
return self.__repr__()
matmult/result.pyc
matmult/
Solution