Question

This is C programming assignment. The objective of this homework is to give you practice using...

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<rdim; 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<rdim; i++)

    result.element[i] = (T*)malloc(cdim * sizeof(T));

/* initialize the matrix elements */

for (i=0; i<rdim; i++)

    for (j=0; j<cdim; 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<rdim; i++)

    result.element[i] = (T*)malloc(cdim * sizeof(T));

/* initialize the matrix elements */

for (i=0; i<rdim; i++)

    for (j=0; j<cdim; j++)

      result.element[i][j] = *(initval + (cdim * i + j));

return result;

}

void destroy(matrix m)

{

int i;

for (i=0; i<m.row_dim; 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("\n");

for (i=0; i<m.row_dim; i++) {

    printf("\n");

    for (j=0; j<m.col_dim; j++)

       printf(FORMAT, m.element[i][j]);

}

printf("\n");

}

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<m1.row_dim; i++)

    for (j=0; j<m1.col_dim; 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<m1.row_dim; i++)

    for (j=0; j<m1.col_dim; 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<m.row_dim; i++)

    for (j=0; j<m.col_dim; 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<m1.row_dim; i++)

    for (j=0; j<m2.col_dim; j++) {

      result.element[i][j] = 0;

      for (k=0; k<m1.col_dim; 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<m.row_dim; i++)

    for (j=0; j<m.col_dim; 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<m1->row_dim; i++)

    for (j=0; j<m1->col_dim; j++)

      m2->element[i][j] = m1->element[i][j];

}

/* File: matrix_dynamic.h */

#ifndef MATRIXh

#define MATRIXh

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

#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("\n Matrix a:");

matrix_print(a);

printf("\n Matrix b:");

matrix_print(b);

printf("\n 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<rdim; i++)

    for (j=0; j<cdim; 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<rdim; i++)

    for (j=0; j<cdim; 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("\n");

for (i=0; i<m.row_dim; i++) {

    printf("\n");

    for (j=0; j<m.col_dim; j++)

       printf(FORMAT, m.element[i][j]);

}

printf("\n");

}

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<m1.row_dim; i++)

    for (j=0; j<m1.col_dim; 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<m1.row_dim; i++)

    for (j=0; j<m1.col_dim; 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<m.row_dim; i++)

    for (j=0; j<m.col_dim; 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<m1.row_dim; i++)

    for (j=0; j<m2.col_dim; j++) {

      result.element[i][j] = 0;

      for (k=0; k<m1.col_dim; 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<m.row_dim; i++)

    for (j=0; j<m.col_dim; 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<m1->row_dim; i++)

    for (j=0; j<m1->col_dim; j++)

      m2->element[i][j] = m1->element[i][j];

}

/* File: matrix_static.h */

#ifndef MATRIXh

#define MATRIXh

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

#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("\n Matrix a:");

matrix_print(a);

printf("\n Matrix b:");

matrix_print(b);

printf("\n a+b:");

matrix_print(add(a,b));

}

Homework Answers

Answer #1

//dynamic

test_dynamic.c

/* File: test_dynamic.c */

#include "matrix_dynamic.h"
#include "transpose.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("\n Matrix a:");
    matrix_print(a);
    printf("\n Matrix b:");
    matrix_print(b);
    printf("\n a+b:");
    matrix_print(add(a,b));
    printf("\n transposing a");
    matrix_print(transpose(a));
    printf("\n transposing a+b");
    matrix_print((transpose(add(a,b))));
}

matrix_dynamic.h

/* File: matrix_dynamic.h */
#ifndef MATRIXh
#define MATRIXh
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int T;
#define FORMAT "%i " // This has to be changed if T is changed.

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


matrix_dynamic.c

/* 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<rdim; 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<rdim; i++)
        result.element[i] = (T*)malloc(cdim * sizeof(T));
    /* initialize the matrix elements */
    for (i=0; i<rdim; i++)
        for (j=0; j<cdim; 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<rdim; i++)
        result.element[i] = (T*)malloc(cdim * sizeof(T));
    /* initialize the matrix elements */
    for (i=0; i<rdim; i++)
        for (j=0; j<cdim; j++)
            result.element[i][j] = *(initval + (cdim * i + j));
    return result;
}

void destroy(matrix m)
{
    int i;
    for (i=0; i<m.row_dim; 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("\n");
    for (i=0; i<m.row_dim; i++) {
        printf("\n");
        for (j=0; j<m.col_dim; j++)
            printf(FORMAT, m.element[i][j]);
    }
    printf("\n");
}

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<m1.row_dim; i++)
        for (j=0; j<m1.col_dim; 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<m1.row_dim; i++)
        for (j=0; j<m1.col_dim; 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<m.row_dim; i++)
        for (j=0; j<m.col_dim; 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<m1.row_dim; i++)
        for (j=0; j<m2.col_dim; j++) {
            result.element[i][j] = 0;
            for (k=0; k<m1.col_dim; 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<m.row_dim; i++)
        for (j=0; j<m.col_dim; 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<m1->row_dim; i++)
        for (j=0; j<m1->col_dim; j++)
            m2->element[i][j] = m1->element[i][j];
}


transpose.h

// select which module to use for matrix data type
// This has to be done when testing each modules
//#include "matrix_static.h"
#include "matrix_dynamic.h"

// function prototypes

// matrix -> matrix
// returns a matrix with the rows and columbs fliped
// A_out[i,j] = A_in[j,i]
matrix transpose(matrix m);


transpose.c

#include "transpose.h"

// matrix -> matrix
// returns a matrix with the rows and columbs fliped
// A_out[i,j] = A_in[j,i]

matrix transpose(matrix m) {
    matrix result = create_empty(m.col_dim, m.row_dim);

    for (int i = 0, x = m.col_dim; i < x; i++) {
        for (int j = 0, y = m.row_dim; j < y; j++) {
            result.element[i][j] = m.element[j][i];
        }
    }

    return result;
}


test_dynamic_output.txt


Matrix a:

1 2
3 4

Matrix b:

1 2
3 4

a+b:

2 4
6 8

transposing a

1 3
2 4

transposing a+b

2 6
4 8

//static

test_static.c

/* File: test_static.c */

#include "matrix_static.h"
#include "transpose.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("\n Matrix a:");
    matrix_print(a);
    printf("\n Matrix b:");
    matrix_print(b);
    printf("\n a+b:");
    matrix_print(add(a,b));
    printf("\n transposing a");
    matrix_print(transpose(a));
    printf("\n transposing a+b");
    matrix_print((transpose(add(a,b))));
}


matrix_static.h

/* File: matrix_static.h */
#ifndef MATRIXh
#define MATRIXh
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MAX_ROW 10
#define MAX_COL 10

typedef int T;
#define FORMAT "%d " // This has to be changed if T is changed

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

matrix_static.c

/* File: matrix_static.c */
#include "matrix_static.h"

matrix create_empty(int rdim, int cdim)
{
    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<rdim; i++)
        for (j=0; j<cdim; 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<rdim; i++)
        for (j=0; j<cdim; 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("\n");
    for (i=0; i<m.row_dim; i++) {
        printf("\n");
        for (j=0; j<m.col_dim; j++)
            printf(FORMAT, m.element[i][j]);
    }
    printf("\n");
}

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<m1.row_dim; i++)
        for (j=0; j<m1.col_dim; 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<m1.row_dim; i++)
        for (j=0; j<m1.col_dim; 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<m.row_dim; i++)
        for (j=0; j<m.col_dim; 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<m1.row_dim; i++)
        for (j=0; j<m2.col_dim; j++) {
            result.element[i][j] = 0;
            for (k=0; k<m1.col_dim; 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<m.row_dim; i++)
        for (j=0; j<m.col_dim; 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<m1->row_dim; i++)
        for (j=0; j<m1->col_dim; j++)
            m2->element[i][j] = m1->element[i][j];
}


test_static_output.txt

Matrix a:

1 2
3 4

Matrix b:

1 2
3 4

a+b:

2 4
6 8

transposing a

1 3
2 4

transposing a+b

2 6
4 8

makefile

test_static: test_static.o matrix_static.o transpose.o matrix_static.h matrix_static.c transpose.h transpose.c
   gcc -o test_static test_static.o transpose.o matrix_static.o

test_dynamic: test_dynamic.o matrix_dynamic.o transpose.o matrix_dynamic.h matrix_dynamic.c transpose.h transpose.c
   gcc -o test_dynamic test_dynamic.o transpose.o matrix_dynamic.o

test_static.o: test_static.c matrix_static.h transpose.c transpose.h
   gcc -Wall -g -c test_static.c

test_dynamic.o: test_dynamic.c matrix_dynamic.h transpose.c transpose.h
   gcc -Wall -g -c test_dynamic.c

transpose.o: matrix_dynamic.h matrix_static.h transpose.c transpose.h
   gcc -Wall -g -c transpose.c

clean:
   rm *.o -f

  
   

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
We see that this computes the product of two matrices. Add a new kernel code, called...
We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices. #include <stdio.h> #include <math.h> #include <sys/time.h> #define TILE_WIDTH 2 #define WIDTH 6 // Kernel function execute by the device (GPU) __global__ void product (float *d_a, float *d_b, float *d_c, const int n) {    int col = blockIdx.x * blockDim.x + threadIdx.x ;    int row = blockIdx.y * blockDim.y + threadIdx.y ;    float...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
This is my code and can you please tell me why it's not working? By the...
This is my code and can you please tell me why it's not working? By the way, it will work if I reduce 10,000,000 to 1,000,000. #include <iostream> using namespace std; void radixSort(int*a, int n) { int intBitSize = sizeof(int)<<3; int radix = 256; int mask = radix-1; int maskBitLength = 8;    int *result = new int[n](); int *buckets = new int[radix](); int *startIndex = new int[radix]();    int flag = 0; int key = 0; bool hasNeg =...
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....