c 2b 2b program for matrix addition

Solutions on MaxInterview for c 2b 2b program for matrix addition by the best coders in the world

showing results for - "c 2b 2b program for matrix addition"
Olivia
11 Jan 2020
1#include <iostream>
2using namespace std;
3
4int main()
5{
6    int r, c, a[100][100], b[100][100], sum[100][100], i, j;
7
8    cout << "Enter number of rows (between 1 and 100): ";
9    cin >> r;
10
11    cout << "Enter number of columns (between 1 and 100): ";
12    cin >> c;
13
14    cout << endl << "Enter elements of 1st matrix: " << endl;
15
16    // Storing elements of first matrix entered by user.
17    for(i = 0; i < r; ++i)
18       for(j = 0; j < c; ++j)
19       {
20           cout << "Enter element a" << i + 1 << j + 1 << " : ";
21           cin >> a[i][j];
22       }
23
24    // Storing elements of second matrix entered by user.
25    cout << endl << "Enter elements of 2nd matrix: " << endl;
26    for(i = 0; i < r; ++i)
27       for(j = 0; j < c; ++j)
28       {
29           cout << "Enter element b" << i + 1 << j + 1 << " : ";
30           cin >> b[i][j];
31       }
32
33    // Adding Two matrices
34    for(i = 0; i < r; ++i)
35        for(j = 0; j < c; ++j)
36            sum[i][j] = a[i][j] + b[i][j];
37
38    // Displaying the resultant sum matrix.
39    cout << endl << "Sum of two matrix is: " << endl;
40    for(i = 0; i < r; ++i)
41        for(j = 0; j < c; ++j)
42        {
43            cout << sum[i][j] << "  ";
44            if(j == c - 1)
45                cout << endl;
46        }
47
48    return 0;
49}
50