Many mathematical problems require the addition, subtraction, and multiplication
ID: 3547853 • Letter: M
Question
Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Implement an ADT Matrix. You must use the following declaration. You will turn in matrix.h file with Matrix declaration and prototypes and matrix.c with the definition of the prototypes found in matrix.h.
typedef struct{
int rows, cols; // matrix dimensions
int **element; // element array
}Matrix;
void matrixInit (Matrix *m, int r, int c);
initializes m with values r row and c column
void matrixCopy (Matrix *m, Matrix *n )
stores an exact copy of matrix n in matrix m
int matrixGetElement (Matrix*, int r, int c);
return the value of the element in the r row and c column
int matrixGetRows (Matrix *)
returns the value stored in rows
int matrixGetCols (Matrix *)
return the value stored in cols
void matrixSetElement (Matrix*, int r, int c, int v);
store the value (v) in the matrix at location r, c
Matrix* matrixAdd (Matrix*, Matrix*);
return the sum of two matrices
Matrix* matrixSubtract (Matrix*, Matrix*);
return the difference of two matrices
Matrix* matrixMultiply (Matrix*, Matrix*);
return the product of two matrices
void matrixDestruct (Matrix* m)
frees the memory allocated within matrix m
Explanation / Answer
/************** matrix.h ******************/
typedef struct {
int rows, cols; // matrix dimensions
int **element; // element array
} Matrix;
void matrixInit (Matrix *m, int r, int c); //initializes m with values r row and c column
void matrixCopy (Matrix *m, Matrix *n ); //stores an exact copy of matrix n in matrix m
int matrixGetElement (Matrix*, int r, int c); //return the value of the element in the r row and c column
int matrixGetRows (Matrix *); //returns the value stored in rows
int matrixGetCols (Matrix *); //return the value stored in cols
void matrixSetElement (Matrix*, int r, int c, int v); //store the value (v) in the matrix at location r, c
Matrix* matrixAdd (Matrix*, Matrix*); //return the sum of two matrices
Matrix* matrixSubtract (Matrix*, Matrix*); //return the difference of two matrices
Matrix* matrixMultiply (Matrix*, Matrix*); //return the product of two matrices
//Returns NULL if multiply can not be performed
void matrixDestruct (Matrix* m); //frees the memory allocated within matrix m
/*************** matrix.c *****************/
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
/***************************************************
*
*
****************************************************/
void matrixInit (Matrix *m, int r, int c) //initializes m with values r row and c column
{
int i, j;
m->rows = r;
m->cols = c;
m->element = (int **) malloc(sizeof(int) * m->rows);
for(i = 0; i < m->rows; i++)
m->element[i] = (int *) malloc(sizeof(int) * m->cols);
for (i = 0; i < m->rows; i++)
for (j = 0; j < m->cols; j++)
m->element[i][j] = 0;
}
void matrixCopy (Matrix *m, Matrix *n) //stores an exact copy of matrix n in matrix m
{
int i, j;
for (i = 0; i < n->rows; i++)
for (j = 0; j < n->cols; j++)
m->element[i][j] = n->element[i][j];
}
int matrixGetElement (Matrix*m, int r, int c) //return the value of the element in the r row and c column
{
return m->element[r][c];
}
int matrixGetRows (Matrix *m) //returns the value stored in rows
{
return m->rows;
}
int matrixGetCols (Matrix *m) //return the value stored in cols
{
return m->cols;
}
void matrixSetElement (Matrix *m, int r, int c, int v) //store the value (v) in the matrix at location r, c
{
m->element[r][c] = v;
}
Matrix* matrixAdd (Matrix *m, Matrix *n) //return the sum of two matrices
{
int i, j;
Matrix *ret;
if(matrixGetRows(m) != matrixGetRows(n) || matrixGetCols(m) != matrixGetCols(n)) {
printf("Cannot add matrices ");
return NULL;
}
ret = (Matrix *) malloc(sizeof(Matrix));
matrixInit(ret, m->rows, m->cols);
for (i = 0; i < ret->rows; i++)
for (j = 0; j < ret->cols; j++)
ret->element[i][j] = m->element[i][j] + n->element[i][j];
return ret;
}
Matrix* matrixSubtract (Matrix *m, Matrix *n) //return the difference of two matrices
{
int i, j;
Matrix *ret;
if(matrixGetRows(m) != matrixGetRows(n) || matrixGetCols(m) != matrixGetCols(n)) {
printf("Cannot subtract matrices ");
return NULL;
}
ret = (Matrix *) malloc(sizeof(Matrix));
matrixInit(ret, m->rows, m->cols);
for (i = 0; i < ret->rows; i++)
for (j = 0; j < ret->cols; j++)
ret->element[i][j] = m->element[i][j] - n->element[i][j];
return ret;
}
Matrix* matrixMultiply (Matrix *m, Matrix *n) //return the product of two matrices
//Returns NULL if multiply can not be performed
{
int i, j, k;
Matrix *ret;
if(matrixGetCols(m) != matrixGetRows(n)) {
printf("Cannot multiply matrices ");
return NULL;
}
ret = (Matrix *) malloc(sizeof(Matrix));
matrixInit(ret, m->rows, n->cols);
for (i = 0; i < ret->rows; i++)
for (j = 0; j < ret->cols; j++)
for(k = 0; k < m->cols; k++)
ret->element[i][j] += m->element[i][k] * n->element[k][j];
return ret;
}
void matrixDestruct (Matrix* m) //frees the memory allocated within matrix m
{
int i;
for(i = 0; i < m->rows; i++)
free(m->element[i]);
free(m->element);
}
void matrixPrint (Matrix *m)
{
int i, j;
for (i = 0; i < m->rows; i++) {
for (j = 0; j < m->cols; j++) {
printf("%d ", m->element[i][j]);
}
printf(" ");
}
printf(" ");
}
/***************************************************
*
*
****************************************************/
int main()
{
int i, j;
Matrix m, n;
matrixInit(&m, 3, 3);
for (i = 0; i < m.rows; i++)
for (j = 0; j < m.cols; j++)
scanf("%d", &m.element[i][j]);
printf("M: ");
matrixPrint(&m);
matrixInit(&n, 3, 3);
for (i = 0; i < n.rows; i++)
for (j = 0; j < n.cols; j++)
scanf("%d", &n.element[i][j]);
printf("N: ");
matrixPrint(&n);
printf("Add: ");
matrixPrint(matrixAdd(&m, &n));
printf("Subtract: ");
matrixPrint(matrixSubtract(&m, &n));
printf("Multiply: ");
matrixPrint(matrixMultiply(&m, &n));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.