Write the following functions, void matAdd(double a[][3], double b[][3], double
ID: 3693356 • Letter: W
Question
Write the following functions, void matAdd(double a[][3], double b[][3], double c[][3], int rowsize) that calculates the result of a + b and stores it in matrix c. Matrix a and b must have the same number of rows and columns. void matSub(double a[][3], double b[][3], double c[][3], int rowsize) that calculates the result of a -b and store it in matrix c. Matrix A and B must have the same number of rows and columns. Write a main function which asks the user for the numbers to be stored in the 2 arrays and prints the resulting array in tabular format on the screen (rows/cols))Explanation / Answer
#include <iostream>
using namespace std;
// define Subtraction function here
void matSub(double a[][100],double b[][100],double c[][100],int rowsize,int colsize){
// define some varible here
int i,j;
// run multi array for subtraction and store in new array
for(i=0;i<rowsize;++i)
for(j=0;j<colsize;++j)
c[i][j]=a[i][j]-b[i][j];
cout << endl << "Subtraction of two matrix is: " << endl;
// run loop for print final value here
for(i=0;i<rowsize;++i)
for(j=0;j<colsize;++j)
{
cout << c[i][j] << " ";
if(j==colsize-1)
cout << endl;
}
}
// define addition function here
void matAdd(double a[][100],double b[][100],double c[][100],int rowsize,int colsize){
// define some varible here
int i,j;
// run multi array for addition value and store in new array
for(i=0;i<rowsize;++i)
for(j=0;j<colsize;++j)
c[i][j]=a[i][j]+b[i][j];
// print final value here
cout << endl << "Addition of two matrix is: " << endl;
for(i=0;i<rowsize;++i)
for(j=0;j<colsize;++j)
{
cout << c[i][j] << " ";
if(j==colsize-1)
cout << endl;
}
}
int main(){
// define some varible here
int r,c,i,j;
double a[100][100],b[100][100],sum[100][100],sub[100][100];
// get user input how many rows require
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
// get user input how many colums require
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter array a elements : " << endl;
//add elements of first array entered by user.
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
// get current posstion array alue and add in array
cout << "Enter element a[" << i <<"]["<< j << "] : ";
cin >> a[i][j];
}
//add elements of second array entered by user.
cout << endl << "Enter array b elements : " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
// get current posstion array alue and add in array
cout << "Enter element b[" << i <<"]["<< j << "] : ";
cin >> b[i][j];
}
// call addition function here
matAdd(a,b,sum,r,c);
// call Subtraction function here
matSub(a,b,sub,r,c);
std::cout<<"Press Enter To Continue..."<<std::endl;
std::cin.ignore();
std::cin.get();
main();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.