Prefer C++; Need some help. There\'s ample examples of how to create a program w
ID: 3861075 • Letter: P
Question
Prefer C++;
Need some help. There's ample examples of how to create a program where the user enters the matrix values. But how do I start a program where I create the matrices myself and determine the values?
Matrix multiplication/addition programming problem.
Write a program that displays 4 matrices to the user, then asks the user to select 2 matrices. Then ask if they want to add or multiply them. Print out the two matrices the user selected along with the results of the users choice (if possible).
Create 4 matrices: a 2x2, a 2x3, a 3x2, and a 3x3 matrix.
Hard code the values in each of the matrices
. Do NOT ask the user for the values in each matrix.
The user can choose the same matrix twice. Do no use binary (1,0 only) matrices.
Do not hardcode the ANSWERS (just the matrices).
Explanation / Answer
c++ code:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int m1[2][2] = {
{1,2},
{3,4}
};
cout << "Matrix 1: " << endl;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
cout << m1[i][j] << " ";
}
cout << endl;
}
int m2[2][3]= {
{1,2,3},
{4,5,6}
};
cout << "Matrix 2: " << endl;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
cout << m2[i][j] << " ";
}
cout << endl;
}
int m3[3][2]= {
{1,2},
{3,4},
{5,6}
};
cout << "Matrix 3: " << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 2; ++j)
{
cout << m3[i][j] << " ";
}
cout << endl;
}
int m4[3][3]= {
{1,2,3},
{4,5,6},
{7,8,9}
};
cout << "Matrix 4: " << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
cout << m4[i][j] << " ";
}
cout << endl;
}
int c;
cout <<"Enter 1 for Adding two matrices and Enter 2 for multiplying two matrices!";
cin >> c;
if(c == 1)
{
int c1, c2;
cout << "Select first matrix(Enter a number between 1-4)" << endl;
cin >> c1;
cout << "Select second matrix(Enter a number between 1-4)" << endl;
cin >> c2;
if(c1 != c2)
{
cout << "Error, you can not add two matrices with different dimentions!";
}
else
{
if(c1 == 1)
{
cout << "m1 + m1 = " << endl;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
cout << m1[i][j] + m1[i][j] << " ";
}
cout << endl;
}
}
else if(c1 == 2)
{
cout << "m2 + m2 = " << endl;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
cout << m2[i][j] + m2[i][j] << " ";
}
cout << endl;
}
}
else if(c1 == 3)
{
cout << "m3 + m3 = " << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 2; ++j)
{
cout << m3[i][j] + m3[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "m4 + m4 = " << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
cout << m4[i][j] + m4[i][j] << " ";
}
cout << endl;
}
}
}
}
else if(c == 2)
{
int c1, c2;
cout << "Select first matrix(Enter a number between 1-4)" << endl;
cin >> c1;
cout << "Select second matrix(Enter a number between 1-4)" << endl;
cin >> c2;
if(c1 != c2)
{
if(c1 == 1 and c2 ==2)
{
cout << "m1 * m2 = " << endl;
int r1 = 2; int c1 = 2; int r2 = 2; int c2 = 3;
for(int i = 0; i < r1; ++i)
{
for(int j = 0; j < c2; ++j)
{
int sum = 0;
for(int k = 0; k < c1; ++k)
{
sum += m1[i][k] * m2[k][j];
}
cout << sum << " ";
}
cout << " ";
}
}
else if(c1 == 2 and c2 == 3)
{
cout << "m2 * m3 = " << endl;
int r1 = 2; int c1 = 3; int r2 = 3; int c2 = 2;
for(int i = 0; i < r1; ++i)
{
for(int j = 0; j < c2; ++j)
{
int sum = 0;
for(int k = 0; k < c1; ++k)
{
sum += m2[i][k] * m3[k][j];
}
cout << sum << " ";
}
cout << " ";
}
}
else if(c1 == 3 and c2 == 2)
{
cout << "m3 * m2 = " << endl;
int r1 = 3; int c1 = 2; int r2 = 2; int c2 = 3;
for(int i = 0; i < r1; ++i)
{
for(int j = 0; j < c2; ++j)
{
int sum = 0;
for(int k = 0; k < c1; ++k)
{
sum += m3[i][k] * m2[k][j];
}
cout << sum << " ";
}
cout << " ";
}
}
else if(c1 == 2 and c2 == 4)
{
cout << "m2 * m4 = " << endl;
int r1 = 2; int c1 = 3; int r2 = 3; int c2 = 3;
for(int i = 0; i < r1; ++i)
{
for(int j = 0; j < c2; ++j)
{
int sum = 0;
for(int k = 0; k < c1; ++k)
{
sum += m2[i][k] * m4[k][j];
}
cout << sum << " ";
}
cout << " ";
}
}
else if(c1 == 4 and c2 == 3)
{
cout << "m4 * m3 = " << endl;
int r1 = 3; int c1 = 3; int r2 = 3; int c2 = 2;
for(int i = 0; i < r1; ++i)
{
for(int j = 0; j < c2; ++j)
{
int sum = 0;
for(int k = 0; k < c1; ++k)
{
sum += m4[i][k] * m3[k][j];
}
cout << sum << " ";
}
cout << " ";
}
}
else if(c1 == 3 and c2 == 1)
{
cout << "m3 * m1 = " << endl;
int r1 = 3; int c1 = 2; int r2 = 2; int c2 = 2;
for(int i = 0; i < r1; ++i)
{
for(int j = 0; j < c2; ++j)
{
int sum = 0;
for(int k = 0; k < c1; ++k)
{
sum += m3[i][k] * m1[k][j];
}
cout << sum << " ";
}
cout << " ";
}
}
else
{
cout << "Can not multiply these matrices as dimentions are not valid for multiplication! ";
}
}
else
{
if(c1 == 1)
{
cout << "m1 * m1 = " << endl;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
int sum=0;
for(int k=0; k<2; k++)
{
sum = sum + m1[i][k] * m1[k][j];
}
cout << sum << " ";
}
cout << endl;
}
}
else if(c1 == 2)
{
cout << "Can not multiply two matrices with dimentions 2*3 and 2*3! ";
}
else if(c1 == 3)
{
cout << "Can not multiply two matrices with dimentions 3*2 and 3*2! ";
}
else if( c1 == 4)
{
cout << "m4 * m4 = " << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
int sum=0;
for(int k=0; k<3; k++)
{
sum = sum + m4[i][k] * m4[k][j];
}
cout << sum << " ";
}
cout << endl;
}
}
}
}
else
{
cout << "Invalid choice!" << endl;
}
return 0;
}
Sample Output:
C:UsersAkashDesktopchegg>a
Matrix 1:
1 2
3 4
Matrix 2:
1 2 3
4 5 6
Matrix 3:
1 2
3 4
5 6
Matrix 4:
1 2 3
4 5 6
7 8 9
Enter 1 for Adding two matrices and Enter 2 for multiplying two matrices!2
Select first matrix(Enter a number between 1-4)
3
Select second matrix(Enter a number between 1-4)
2
m3 * m2 =
9 12 15
19 26 33
29 40 51
C:UsersAkashDesktopchegg>a
Matrix 1:
1 2
3 4
Matrix 2:
1 2 3
4 5 6
Matrix 3:
1 2
3 4
5 6
Matrix 4:
1 2 3
4 5 6
7 8 9
Enter 1 for Adding two matrices and Enter 2 for multiplying two matrices!1
Select first matrix(Enter a number between 1-4)
2
Select second matrix(Enter a number between 1-4)
2
m2 + m2 =
2 4 6
8 10 12