Write a C++ program to demonstrate exception handling by using matrix multiplication
operation. Matrix multiplication function should notify if the order of the matrix is invalid, using exception.
Answers
Answer:
#include<iostream>
#include<conio.h>
#include<process.h>
#include<exception>
using namespace std;
void matsum()
{
int m, n, i, j, first[10][10], second[10][10], sum[10][10];
try
{
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
if (m>10 || n>10)
//exit(0);
throw 1;
}
catch(int)
{
cout<<"subscript invalid";
}
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
cin >> first[i][j];
cout << "Enter the elements of second matrix\n";
for ( i = 0 ; i < m ;i++ )
for ( j = 0 ; j < n ; j++ )
cin >> second[i][j];
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
sum[i][j] = first[i][j] + second[i][j];
cout << "Sum of entered matrices:-\n";
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
cout << sum[i][j] << "\t";
cout << endl;
}
}
int main(void)
{
matsum();
getch();
}