Matrix Addition

/* Matrix Addition with class */
#include<iostream>
using namespace std;

class matrix {

	/* Declaration of two dimensional array */
	int a[3][3], b[3][3], ans[3][3];

	public:
		matrix() {
			cout << "Enter data for first array:" << endl;
			for(int i = 0; i < 3; i++)
				for(int j = 0; j < 3; j++)
					cin >> a[i][j];
			cout << "Enter data for second array:" << endl;
			for(int i = 0; i < 3; i++)
				for(int j = 0; j < 3; j++)
					cin >> b[i][j];
		}

		void addition() {
			cout << "After matrix addition" << endl;
			for(int i = 0; i < 3; i++) {
				for (int j = 0; j < 3; j++) {
					ans[i][j]= a[i][j] + b[i][j];
					cout << ans[i][j] << "\t";
				}
				cout << endl;
			}
		}
};

int main() {
	cout << "Program for calculation of Matrix Addition" << endl;
	matrix mtAdd = matrix();
	mtAdd.addition();
	return 0;
}


/* Output */
Enter data for first array:
1
2
3
4
5
6
7
8
9

Enter data for second array:
4
4
4
4
4
4
4
4
4

After matrix addition
5 6 7
8 9 10
11 12 13

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!