95.5k views
4 votes
NOTE :

1. PLEASE WRITE THE CODE IN C

2. PLEASE MAKE SURE TO DO IT WITH THE LOADER CODE

3. PLEASE MAKE SURE THE INPUT AND OUTPUT MATCHES WITH BELOW

CASE :

NOTE :

Matrix Addition and Multiplication

Write a program that implements matrix addition and multiplication of two matrices.

Input

The input contains three parts. The first and second part represent the information of the first matrix and the second matrix respectively. The first line of each part contains row and column number of the matrix, and the following line is an array of integers which represent the matrix itself. The second part is a character which represents the operation of the matrix.

Output

The content of the result matrix. Each element is followed by a space character.

Loader Code

Your code will be judge using this program:

#include
#define SIZE 50

struct mat
int row;
int col;
int value[SIZE][SIZE];
;

void scan_mat(struct mat *);
void print_mat(const struct mat *);
void add_mat(const struct mat *, const struct mat *, struct mat *);
void mul_mat(const struct mat *, const struct mat *, struct mat *);

int main(void)
struct mat m1, m2, result;
char op;
scan_mat(&m1);
scanf(" %c", &op);
scan_mat(&m2);
switch (op)
case '+':
add_mat(&m1, &m2, &result);
break;
case '*':
mul_mat(&m1, &m2, &result);
break;

print_mat(&result);
return 0;


void scan_mat(struct mat *m_p)
scanf("%d %d", &m_p->row, &m_p->col);
for (int i = 0; i < m_p->row; ++i)
for (int j = 0; j < m_p->col; ++j)
scanf("%d", &m_p->value[i][j]);




void print_mat(const struct mat *m_p)
for (int i = 0; i < m_p->row; ++i)
for (int j = 0; j < m_p->col; ++j)
printf("%d ", m_p->value[i][j]);
if (j == m_p->col - 1)
printf("\\");




Example 1

Input

2 3
3 2 6
2 4 5
+
2 3
4 5 6
3 1 4

Output

7 7 12
5 5 9

Example 2

Input

2 2
1 2
3 4
*
2 2
5 6
7 8

Output

19 22
43 50

User NickSlash
by
8.5k points

1 Answer

0 votes

Here's the C code that implements matrix addition and multiplication:

```c

#include <stdio.h>

#define SIZE 50

struct mat {

int row;

int col;

int value[SIZE][SIZE];

};

void scan_mat(struct mat *);

void print_mat(const struct mat *);

void add_mat(const struct mat *, const struct mat *, struct mat *);

void mul_mat(const struct mat *, const struct mat *, struct mat *);

int main(void) {

struct mat m1, m2, result;

char op;

scan_mat(&m1);

scanf(" %c", &op);

scan_mat(&m2);

switch (op) {

case '+':

add_mat(&m1, &m2, &result);

break;

case '*':

mul_mat(&m1, &m2, &result);

break;

}

print_mat(&result);

return 0;

}

void scan_mat(struct mat *m_p) {

scanf("%d %d", &m_p->row, &m_p->col);

for (int i = 0; i < m_p->row; ++i) {

for (int j = 0; j < m_p->col; ++j) {

scanf("%d", &m_p->value[i][j]);

}

}

}

void print_mat(const struct mat *m_p) {

for (int i = 0; i < m_p->row; ++i) {

for (int j = 0; j < m_p->col; ++j) {

printf("%d ", m_p->value[i][j]);

}

printf("\\");

}

}

void add_mat(const struct mat *m1_p, const struct mat *m2_p, struct mat *result_p) {

result_p->row = m1_p->row;

result_p->col = m1_p->col;

for (int i = 0; i < m1_p->row; ++i) {

for (int j = 0; j < m1_p->col; ++j) {

result_p->value[i][j] = m1_p->value[i][j] + m2_p->value[i][j];

}

}

}

void mul_mat(const struct mat *m1_p, const struct mat *m2_p, struct mat *result_p) {

result_p->row = m1_p->row;

result_p->col = m2_p->col;

for (int i = 0; i < m1_p->row; ++i) {

for (int j = 0; j < m2_p->col; ++j) {

result_p->value[i][j] = 0;

for (int k = 0; k < m1_p->col; ++k) {

result_p->value[i][j] += m1_p->value[i][k] * m2_p->value[k][j];

}

}

}

}

```

In this code, the functions `scan_mat`, `print_mat`, `add_mat`, and `mul_mat` are defined as described in the loader code.

The `scan_mat` function scans the row and column numbers of a matrix, and then scans the matrix elements.

The `print_mat` function prints the elements of a matrix.

The `add_mat` function adds two matrices element-wise and stores the result in the `result` matrix.

The `mul_mat` function multiplies two matrices using matrix multiplication rules and stores the result in the `result` matrix.

In the `main` function, the matrices `m1`, `m2`, and `result` are declared and scanned using the `scan_mat` function. The operation character is scanned, and based on the operation, either matrix addition or multiplication is performed using the respective functions. Finally, the result matrix is printed using the `print_mat` function.

User Emmanuel Mendoza
by
7.9k points