9536. Sum of matrices
Given two matrices A and B.
Find their sum C = A + B.
Input. First line contains the size of matrices n and m (1 ≤
n, m ≤ 100). Each of the next n lines contains m
integers and describe matrix A. Then empty line is given, after which the
description of matrix B is given in the same format.
Output. Print the matrix Ñ: n rows,
each with m integers.
Sample
input |
Sample
output |
3 4 3 4 5 6 1 2 3 4 7 6 5 4 0 0 -3 -2 -1 3 4 5 5 6 1 2 |
3 4 2 4 0 5 7 9 12 12 6 6 |
algebra
By the given
matrices À and  find their sum in
matrix Ñ, where
Ñij = Aij + Bij
Declare matrices a, b and c.
#define MAX 101
int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];
Read the input data – the matrices À and Â.
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &a[i][j]);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &b[i][j]);
Find the sum of matrices C = A + B.
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
c[i][j] = a[i][j] + b[i][j];
Print the resulting matrix.
for (i = 0; i < n; i++)
{
for (j = 0; j < m;
j++)
printf("%d ", c[i][j]);
printf("\n");
}
#include <stdio.h>
int i, j, n, m;
int** a, ** b, ** c;
void Read(int**& matr, int n, int m)
{
matr = new int* [n];
for (i
= 0; i < n; i++)
{
matr[i] = new int[m];
for (j
= 0; j < m; j++)
scanf("%d", &matr[i][j]);
}
}
void Sum(int** a, int**
b, int**& c, int n, int m)
{
c = new int* [n];
for (i
= 0; i < n; i++)
{
c[i] = new int[m];
for (j
= 0; j < m; j++)
c[i][j] = a[i][j] + b[i][j];
}
}
void Print(int** a)
{
for (i
= 0; i < n; i++)
{
for (j
= 0; j < m; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
int main(void)
{
scanf("%d %d", &n, &m);
Read(a, n, m);
Read(b, n, m);
Sum(a, b, c, n, m);
Print(c);
return 0;
}