Write a program in C++ to add two matrices with provision of exceptions handling.
Answers
#include<iostream>
using namespace std;
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "Enter the elements of second matrix\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
return 0;
Exemption dealing with is a procedure of taking care of outstanding circumstances that may happen in a program because of the above expressed reasons so that:
The program will end smoothly i.e. it will give a legitimate message and afterward will end the program.
In the wake of giving the best possible message expressing the reason of the special case the program keeps on executing in the wake of amending the mistake.
In the C++ dialect, exemption taking care of is performed utilizing the accompanying catchphrases:
attempt
get
throw
C++ programming language is a strong andextensively used object-oriented programming language. Presented below below are some C++ codes and programme :
Following program is C ++ displaying the addition of two matrices :
#include<iostream>using namespace std;int main() { int m1[3][3], m2[3][3], i, j, m3[3][3]; cout<<"\n Enter First Matrix Elements : \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cout<<" "; cin>>m1[i][j], }, cout<<"\n Enter Second Matrix Elements : \n";for(i=0; i<3; i++ {;for(j=0; j<3; j++); {; cout<<" "; cin>>m2[i][j]; } } cout<<"\n Sum of Two Matrices : \n\n";for(i=0; i<3; i++) {for(j=0; j<3; j++) { m3[i][j]=m1[i][j]+m2[i][j]; } } for(i=0; i<3; i++) { cout<<" "; for(j=0; j<3; j++) { cout<<m3[i][j]<<" "; } cout<<"\n"; } return 0;}Output :