In: Computer Science
C++
For this assignment, you will write a C++ program to either add (A), subtract (S), multiply (M), and (N), or (O) two matrices if possible. You will read in the dimensions (rows, then columns) of the first matrix, then read the first matrix, then the dimensions (rows then columns) of the second matrix, then the second matrix, then a character (A, S, M, N, O) to determine which operation they want to do.
The program will then perform the operation, print the answer,
and quit. The program should have five functions in addition to
main - one for add, one for subtract, one for multiply, one for
add, and one for or, but main should do all the input and output.
Matrices will be no bigger than 10x10 and will be ints. You cannot
assume the matrices will be square.
SAMPLE:
$ ./a.out input the row size and col size of A 3 2 input matrix A 2 1 -1 0 3 4 input the row size and col size of B 3 2 input matrix B 12 -18 3 9 6 -3 Choose your operation: A for add, S for subtract, M for multiply N for and, O for or A The answer is: 14 -17 2 9 9 1
Program :
#include <iostream>
using namespace std;
int row1,column1,row2,column2;
void add_matrix(int **A,int **B){
int i,j=0;
if(row1 == row2 && column1 == column2){
cout << "The answer is:"
<< endl;
for(i=0;i<row1;i++){
for(j=0;j<column1;j++){
cout << A[i][j]+B[i][j]
<< " ";
}
cout << "\n";
}
}else{
cout << "not possible";
}
}
void sub_matrix(int **A,int **B){
int i,j=0;
if(row1 == row2 && column1 == column2){
cout << "The answer is:"
<< endl;
for(i=0;i<row1;i++){
for(j=0;j<column1;j++){
cout << A[i][j]-B[i][j]
<< " ";
}
cout << "\n";
}
}else{
cout << "not possible";
}
}
void mul_matrix(int **A,int **B){
int i,j,k=0;
int c[row1][column2];
if(column1 == row2){
cout << "The answer is:"
<< endl;
for
(i=0;i<row1;i++){
for(j=0;j<column2;j++){
c[i][j]=0;
for(k=0;k<column1;k++){
c[i][j]=c[i][j]+A[i][k]*B[k][j];
}
}
}
for(i=0;i<row1;i++){
for(j=0;j<column1;j++){
cout << c[i][j]
<< " ";
}
cout << "\n";
}
}else{
cout << "not possible";
}
}
void and_matrix(int **A,int **B){
int i,j=0;
if(row1 == row2 && column1 == column2){
cout << "The answer is:"
<< endl;
for(i=0;i<row1;i++){
for(j=0;j<column1;j++){
cout << (A[i][j]
&& B[i][j]) << " ";
}
cout << "\n";
}
}else{
cout << "not possible";
}
}
void or_matrix(int **A,int **B){
int i,j=0;
if(row1 == row2 && column1 == column2){
cout << "The answer is:"
<< endl;
for(i=0;i<row1;i++){
for(j=0;j<column1;j++){
cout << (A[i][j] ||
B[i][j]) << " ";
}
cout << "\n";
}
}else{
cout << "not possible";
}
}
int main(){
cout << "Input the row size and col size of A"
<< endl;
cin >> row1 >> column1;
if(row1 > 10 || column1 > 10){
return 0;
}
int i,j=0;
int **A;
A = new int *[row1];
for(int i = 0; i <row1; i++)
A[i] = new int[column1];
cout << "input matrix A " << endl;
for(i=0;i<row1;i++){
for(j=0;j<column1;j++){
cin >>
A[i][j];
}
}
cout << "Input the row size and col size of B"
<< endl;
cin >> row2 >> column2;
if(row2 > 10 || column2 > 10){
return 0;
}
int **B;
B = new int *[row1];
for(int i = 0; i <row1; i++)
B[i] = new int[column1];
cout << "input matrix B " << endl;
for(i=0;i<row1;i++){
for(j=0;j<column1;j++){
cin >>
B[i][j];
}
}
cout << "Choose your operation: A for add,S for
subtract,M for multiple,N for and,O for or"<<endl;
char op;
cin >> op;
switch(op){
case 'A':
add_matrix(A,B);
break;
case 'S':
sub_matrix(A,B);
break;
case 'M':
mul_matrix(A,B);
break;
case 'N':
and_matrix(A,B);
break;
case 'O':
or_matrix(A,B);
break;
default:
cout <<
"Invalid operation";
break;
}
return 0;
}
Output :