Question

2 dim arrays as Matrices (30 pts) Given matrices A and B as shown below: Write functions to do each of the following jobs for

Need help with this program which needs to be done in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using namespace std;
int main()
{
int i,j,a,b, first[3][3], second[3][3], sum[3][3], subtract[3][3], multiply[3][3],scalar,scalarf[3][3],scalars[3][3];
a=b=3;
cout<<"\nEnter the scalar miltiplier:";
cin>>scalar;
cout << "\nEnter the elements of first matrix : ";
for (i=0;i<a;i++ )
{
for (j=0;j<b;j++ )
{
cin >> first[i][j];
}
}
cout << "\nEnter the elements of second matrix:";
for (i= 0 ;i< a ;i++ )
{
for (j= 0;j<b;j++ )
{
cin >> second[i][j];
}
}
for (i=0;i< a;i++ )
{
for (j=0;j<b;j++ )
{
sum[i][j] = first[i][j] + second[i][j];
subtract[i][j] = first[i][j] - second[i][j];
scalarf[i][j]=first[i][j]*scalar;
scalars[i][j]=second[i][j]*scalar;
}
}
for(i = 0; i < a; ++i)
for(j = 0; j < b; ++j)
{
multiply[i][j]=0;
}
// Multiplying matrix a and b and storing in array mult.
for(i = 0; i < a; ++i)
for(j = 0; j < b; ++j)
for(int k = 0; k < a; ++k)
{
multiply[i][j] += first[i][k] * second[k][j];
}
cout << "\nFirst matrix : "<<endl;
for (i=0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << first[i][j] << "\t";
}
cout << endl;

}cout << "\nSecond matrix : "<<endl;
for (i=0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << second[i][j] << "\t";
}
cout << endl;

}

cout << "\nSum of entered matrices : "<<endl;
for (i=0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << sum[i][j] << "\t";
}
cout << endl;

}
cout << "\nSubtraction of entered matrices : "<<endl;
for (i=0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << subtract[i][j] << "\t";
}
cout << endl;
}
cout << "\nMultiplication of entered matrices : "<<endl;
for (i=0 ; i <a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << multiply[i][j] << "\t";
}
cout << endl;
}
cout << "\nScalar multiplication of first matrix : "<<endl;
for (i=0 ; i <a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << scalarf[i][j] << "\t";
}
cout << endl;
}
cout << "\nScalar multiplication of second matrix : "<<endl;
for (i=0 ; i <a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << scalars[i][j] << "\t";
}
cout << endl;
}

return 0;
}

A matrixfunction.cpp 1 using namespace std; 2 int main() 3 { 4 int i,j,a,b, first[3] [3], second [3][3], sum[3][3], subtract[A 34 Tur matrixfunction.cpp - U; T a; FFT) for(j = 0; j < b; ++i) multiply[i][j]=0; // Multiplying matrix a and b and storingmatrixfunction.cpp A 72 } 73 cout << endl; 74 75 } 76 cout << \nSubtraction of entered matrices : << endl; 77 for (i=0 ; i<matrixfunction.cpp A 85 cout << \nmultiplication of entered matrices : << endl; 86 for (i=0; i <a ; i++) 87 { 88 for (j = 0matrixfunction Enter the scalar miltiplier:-2 Enter the elements of first matrix : 2 3 -1 5 6 2 3 4 2 Enter the elements of smatrixfunction - 4 2 Enter the elements of second matrix:1 3 2 -2 -1 50 2 -2 First matrix : 2 3 -1 5 6 2 3 4 2 Second matrix

Please upvote... Thanks

Add a comment
Know the answer?
Add Answer to:
Need help with this program which needs to be done in C++ 2 dim arrays as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT