Question
This is C programming assignment. The objective of this homework is to give you practice using make files to compose an executable file from a
This is C programming assignment. The objective of this homework is to give you practice using make files to compose an executable file from a set of source files and adding additional functions to an existing set of code. This assignment will give you an appreciation for the ease with which well designed software can be extended. For this assignment, you will use both the static and dynamic assignment versions of the matrix software. Using each version, do the following:
1.Develop a module which calculates the transpose of a matrix. Your module should return a matrix with the elements interchanged so that A_out[i][j] = A_in[j][i]. (You should be able to use the same module for both the static and dynamic versions.)
2.Generate a makefile that you can use to compile the set of modules to generate the executable file.
3.Modify the set of software so that the matrix elements are now integers, rather than a double precision float value.
4.Execute your transpose file with both the static and dynamic implementation and the version with integer elements. Copy the outputs to a text file to demonstrate proper execution.
What to submit: Please submit a copy of the modified source files, makefiles, and a text file(s) that includes execution output that demonstrates proper operation for all cases.
Here is all the codes they gave to us:
This is C programming assignment.
The objective of this homework is to give you practice using make files to compose an executable file from a set of source files and adding additional functions to an existing set of code. This assignment will give you an appreciation for the ease with which well designed software can be extended. For this assignment, you will use both the static and dynamic assignment versions of the matrix software. Using each version, do the following:
1.Develop a module which calculates the transpose of a matrix. Your module should return a matrix with the elements interchanged so that A_out[i][j] = A_in[j][i]. (You should be able to use the same module for both the static and dynamic versions.)
2.Generate a makefile that you can use to compile the set of modules to generate the executable file.
3.Modify the set of software so that the matrix elements are now integers, rather than a double precision float value.
4.Execute your transpose file with both the static and dynamic implementation and the version with integer elements. Copy the outputs to a text file to demonstrate proper execution.
What to submit: Please submit a copy of the modified source files, makefiles, and a text file(s) that includes execution output that demonstrates proper operation for all cases.
Here is all the codes they gave to us:
/* File: matrix_dynamic.c */
#include "matrix_dynamic.h"
matrix create_empty(int rdim, int cdim)
{
int i;
matrix result;
/* assign values to the row_dim and col_dim structure members */
result.row_dim = rdim;
result.col_dim = cdim;
result.element = (T**)malloc(rdim * sizeof(T*));
for (i=0; i result.element[i] = (T*)malloc(cdim * sizeof(T)); return result; } matrix create_initval(int rdim, int cdim, T val) { int i,j; matrix result; /* assign values to the structure members */ result.row_dim = rdim; result.col_dim = cdim; result.element = (T**)malloc(rdim * sizeof(T*)); for (i=0; i result.element[i] = (T*)malloc(cdim * sizeof(T)); /* initialize the matrix elements */ for (i=0; i for (j=0; j result.element[i][j] = val; return result; } matrix create_initvals(int rdim, int cdim, T* initval) { int i,j; matrix result; /* assign values to the structure members */ result.row_dim = rdim; result.col_dim = cdim; result.element = (T**)malloc(rdim * sizeof(T*)); for (i=0; i result.element[i] = (T*)malloc(cdim * sizeof(T)); /* initialize the matrix elements */ for (i=0; i for (j=0; j result.element[i][j] = *(initval + (cdim * i + j)); return result; } void destroy(matrix m) { int i; for (i=0; i free(m.element[i]); free(m.element); free(&m); } T retrieve(int row, int col, matrix m) { return(m.element[row][col]); } void assign(int row, int col, matrix* m, T elm) { m->element[row][col] = elm; } void matrix_print(matrix m) { int i,j; printf(" "); for (i=0; i printf(" "); for (j=0; j printf(FORMAT, m.element[i][j]); } printf(" "); } matrix add(matrix m1, matrix m2) { int i,j; matrix result; assert((m1.row_dim == m2.row_dim) && (m1.col_dim == m2.col_dim)); result = create_empty(m1.row_dim, m2.col_dim); for (i=0; i for (j=0; j result.element[i][j] = m1.element[i][j] + m2.element[i][j]; return result; } matrix subtract(matrix m1, matrix m2) { int i,j; matrix result; assert((m1.row_dim == m2.row_dim) && (m1.col_dim == m2.col_dim)); result = create_empty(m1.row_dim, m2.col_dim); for (i=0; i for (j=0; j result.element[i][j] = m1.element[i][j] - m2.element[i][j]; return result; } matrix negate(matrix m) { int i,j; matrix result; result = create_empty(m.row_dim, m.col_dim); for (i=0; i for (j=0; j result.element[i][j] = -m.element[i][j]; return result; } matrix multiply(matrix m1, matrix m2) { int i,j,k; matrix result; assert(m1.col_dim == m2.row_dim); result = create_empty(m1.row_dim, m2.col_dim); for (i=0; i for (j=0; j result.element[i][j] = 0; for (k=0; k result.element[i][j] += m1.element[i][k] * m2.element[k][j]; } return result; } matrix scalar_multiply(T scalar, matrix m) { int i,j; matrix result; result = create_empty(m.row_dim, m.col_dim); for (i=0; i for (j=0; j result.element[i][j] = scalar * m.element[i][j]; return result; } void equate(matrix* m1, matrix* m2) { int i,j; assert((m1->row_dim == m2->row_dim) && (m1->col_dim == m2->col_dim)); for (i=0; i for (j=0; j m2->element[i][j] = m1->element[i][j]; } /* File: matrix_dynamic.h */ #ifndef MATRIXh #define MATRIXh #include #include #include #define FORMAT "%8.3lf" typedef double T; typedef struct { int row_dim, col_dim; T** element; } matrix; /* function prototypes */ matrix create_empty(int rdim, int cdim); matrix create_initval(int rdim, int cdim, T val); matrix create_initvals(int rdim, int cdim, T* initval); void destroy(matrix); void matrix_print(matrix); T retrieve(int row, int col, matrix m); /* retrieve an element from m */ void assign(int row, int col, matrix*, T val); /* assign a value to an element of m */ void equate(matrix* m1, matrix* m2); /* m1 = m2 */ matrix add(matrix, matrix); matrix subtract(matrix, matrix); matrix negate(matrix); matrix multiply(matrix, matrix); matrix scalar_multiply(T scalar, matrix); /* remaining function prototypes not shown */ #endif /* File: test_dynamic.c */ #include "matrix_dynamic.h" int main() { static T data[] = {1,2,3,4}; matrix a,b; a = create_initvals(2,2,data); b = create_empty(2,2); equate(&a,&b); printf(" Matrix a:"); matrix_print(a); printf(" Matrix b:"); matrix_print(b); printf(" a+b:"); matrix_print(add(a,b)); } /* File: matrix_static.c */ #include "matrix.h" matrix create_empty(int rdim, int cdim) { int i; matrix result; /* assign values to the row_dim and col_dim structure members */ result.row_dim = rdim; result.col_dim = cdim; return result; } matrix create_initval(int rdim, int cdim, T val) { int i,j; matrix result; /* assign values to the structure members */ result.row_dim = rdim; result.col_dim = cdim; /* initialize the matrix elements */ for (i=0; i for (j=0; j result.element[i][j] = val; return result; } matrix create_initvals(int rdim, int cdim, T* initval) { int i,j; matrix result; /* assign values to the structure members */ result.row_dim = rdim; result.col_dim = cdim; /* initialize the matrix elements */ for (i=0; i for (j=0; j result.element[i][j] = *(initval + (cdim * i + j)); return result; } T retrieve(int row, int col, matrix m) { return(m.element[row][col]); } void assign(int row, int col, matrix* m, T elm) { m->element[row][col] = elm; } void matrix_print(matrix m) { int i,j; printf(" "); for (i=0; i printf(" "); for (j=0; j printf(FORMAT, m.element[i][j]); } printf(" "); } matrix add(matrix m1, matrix m2) { int i,j; matrix result; assert((m1.row_dim == m2.row_dim) && (m1.col_dim == m2.col_dim)); result = create_empty(m1.row_dim, m2.col_dim); for (i=0; i for (j=0; j result.element[i][j] = m1.element[i][j] + m2.element[i][j]; return result; } matrix subtract(matrix m1, matrix m2) { int i,j; matrix result; assert((m1.row_dim == m2.row_dim) && (m1.col_dim == m2.col_dim)); result = create_empty(m1.row_dim, m2.col_dim); for (i=0; i for (j=0; j result.element[i][j] = m1.element[i][j] - m2.element[i][j]; return result; } matrix negate(matrix m) { int i,j; matrix result; result = create_empty(m.row_dim, m.col_dim); for (i=0; i for (j=0; j result.element[i][j] = -m.element[i][j]; return result; } matrix multiply(matrix m1, matrix m2) { int i,j,k; matrix result; assert(m1.col_dim == m2.row_dim); result = create_empty(m1.row_dim, m2.col_dim); for (i=0; i for (j=0; j result.element[i][j] = 0; for (k=0; k result.element[i][j] += m1.element[i][k] * m2.element[k][j]; } return result; } matrix scalar_multiply(T scalar, matrix m) { int i,j; matrix result; result = create_empty(m.row_dim, m.col_dim); for (i=0; i for (j=0; j result.element[i][j] = scalar * m.element[i][j]; return result; } void equate(matrix* m1, matrix* m2) { int i,j; assert((m1->row_dim == m2->row_dim) && (m1->col_dim == m2->col_dim)); for (i=0; i for (j=0; j m2->element[i][j] = m1->element[i][j]; } /* File: matrix_static.h */ #ifndef MATRIXh #define MATRIXh #include #include #include #define FORMAT "%8.3lf" #define MAX_ROW 10 #define MAX_COL 10 typedef double T; typedef struct { int row_dim, col_dim; T element[MAX_ROW][MAX_COL]; } matrix; /* function prototypes */ matrix create_empty(int rdim, int cdim); matrix create_initval(int rdim, int cdim, T val); matrix create_initvals(int rdim, int cdim, T* initval); void destroy(matrix); void matrix_print(matrix); T retrieve(int row, int col, matrix m); /* retrieve an element from m */ void assign(int row, int col, matrix*, T val); /* assign a value to an element of m */ void equate(matrix* m1, matrix* m2); /* m1 = m2 */ matrix add(matrix, matrix); matrix subtract(matrix, matrix); matrix negate(matrix); matrix multiply(matrix, matrix); matrix scalar_multiply(T scalar, matrix); /* remaining function prototypes not shown */ #endif /* File: test_static.c */ #include "matrix.h" int main() { static T data[] = {1,2,3,4}; matrix a,b; a = create_initvals(2,2,data); b = create_empty(2,2); equate(&a,&b); printf(" Matrix a:"); matrix_print(a); printf(" Matrix b:"); matrix_print(b); printf(" a+b:"); matrix_print(add(a,b)); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started