120k views
5 votes
g For this project you are required to perform Matrix operations (Addition, Subtraction and Multiplication). For each of the operations mentioned above you have to make a function in addition to two other functions for ‘Inputting’ and ‘Displaying’ the Matrices in Row Order (rows are populated left to right, in sequence). In total there will be 5 functions: 3 for the operations and 2 functions for inputting and displaying the data.

1 Answer

1 vote

Answer:

C++ code is explained below

Step-by-step explanation:

#include<iostream>

using namespace std;

//Function Declarations

void add();

void sub();

void mul();

//Main Code Displays Menu And Take User Input

int main()

{

int choice;

cout << "\\Menu";

cout << "\\Choice 1:addition";

cout << "\\Choice 2:subtraction";

cout << "\\Choice 3:multiplication";

cout << "\\Choice 0:exit";

cout << "\\\\Enter your choice: ";

cin >> choice;

cout << "\\";

switch(choice)

{

case 1: add();

break;

case 2: sub();

break;

case 3: mul();

break;

case 0: cout << "Exited";

exit(1);

default: cout << "Invalid";

}

main();

}

//Addition Of Matrix

void add()

{

int rows1,cols1,i,j,rows2,cols2;

cout << "\\matrix1 # of rows: ";

cin >> rows1;

cout << "\\matrix1 # of columns: ";

cin >> cols1;

int m1[rows1][cols1];

//Taking First Matrix

for(i=0;i<rows1;i++)

for(j=0;j<cols1;j++)

{

cout << "\\Enter element (" << i << "," << j << "): ";

cin >> m1[i][j];

cout << "\\";

}

//Printing 1st Matrix

for(i=0;i<rows1;i++)

{

for(j=0;j<cols1;j++)

cout << m1[i][j] << " ";

cout << "\\";

}

cout << "\\matrix2 # of rows: ";

cin >> rows2;

cout << "\\matrix2 # of columns: ";

cin >> cols2;

int m2[rows2][cols2];

//Taking Second Matrix

for(i=0;i<rows2;i++)

for(j=0;j<cols2;j++)

{

cout << "\\Enter element (" << i << "," << j << "): ";

cin >> m2[i][j];

cout << "\\";

}

//Displaying second Matrix

cout << "\\";

for(i=0;i<rows2;i++)

{

for(j=0;j<cols2;j++)

cout << m2[i][j] << " ";

cout << "\\";

}

//Displaying Sum of m1 & m2

if(rows1 == rows2 && cols1 == cols2)

{

cout << "\\";

for(i=0;i<rows1;i++)

{

for(j=0;j<cols1;j++)

cout << m1[i][j]+m2[i][j] << " ";

cout << "\\";

}

}

else

cout << "operation is not supported";

main();

}

void sub()

{

int rows1,cols1,i,j,k,rows2,cols2;

cout << "\\matrix1 # of rows: ";

cin >> rows1;

cout << "\\matrix1 # of columns: ";

cin >> cols1;

int m1[rows1][cols1];

for(i=0;i<rows1;i++)

for(j=0;j<cols1;j++)

{

cout << "\\Enter element (" << i << "," << j << "): ";

cin >> m1[i][j];

cout << "\\";

}

for(i=0;i<rows1;i++)

{

for(j=0;j<cols1;j++)

cout << m1[i][j] << " ";

cout << "\\";

}

cout << "\\matrix2 # of rows: ";

cin >> rows2;

cout << "\\matrix2 # of columns: ";

cin >> cols2;

int m2[rows2][cols2];

for(i=0;i<rows2;i++)

for(j=0;j<cols2;j++)

{

cout << "\\Enter element (" << i << "," << j << "): ";

cin >> m2[i][j];

cout << "\\";

}

for(i=0;i<rows2;i++)

{

for(j=0;j<cols2;j++)

cout << m1[i][j] << " ";

cout << "\\";

}

cout << "\\";

//Displaying Subtraction of m1 & m2

if(rows1 == rows2 && cols1 == cols2)

{

for(i=0;i<rows1;i++)

{

for(j=0;j<cols1;j++)

cout << m1[i][j]-m2[i][j] << " ";

cout << "\\";

}

}

else

cout << "operation is not supported";

main();

}

void mul()

{

int rows1,cols1,i,j,k,rows2,cols2,mul[10][10];

cout << "\\matrix1 # of rows: ";

cin >> rows1;

cout << "\\matrix1 # of columns: ";

cin >> cols1;

int m1[rows1][cols1];

for(i=0;i<rows1;i++)

for(j=0;j<cols1;j++)

{

cout << "\\Enter element (" << i << "," << j << "): ";

cin >> m1[i][j];

cout << "\\";

}

cout << "\\";

for(i=0;i<rows1;i++)

{

for(j=0;j<cols1;j++)

cout << m1[i][j] << " ";

cout << "\\";

}

cout << "\\matrix2 # of rows: ";

cin >> rows2;

cout << "\\matrix2 # of columns: ";

cin >> cols2;

int m2[rows2][cols2];

for(i=0;i<rows2;i++)

for(j=0;j<cols2;j++)

{

cout << "\\Enter element (" << i << "," << j << "): ";

cin >> m2[i][j];

cout << "\\";

}

cout << "\\";

//Displaying Matrix 2

for(i=0;i<rows2;i++)

{

for(j=0;j<cols2;j++)

cout << m2[i][j] << " ";

cout << "\\";

}

if(cols1!=rows2)

cout << "operation is not supported";

else

{

//Initializing results as 0

for(i = 0; i < rows1; ++i)

for(j = 0; j < cols2; ++j)

mul[i][j]=0;

// Multiplying matrix m1 and m2 and storing in array mul.

for(i = 0; i < rows1; i++)

for(j = 0; j < cols2; j++)

for(k = 0; k < cols1; k++)

mul[i][j] += m1[i][k] * m2[k][j];

// Displaying the result.

cout << "\\";

for(i = 0; i < rows1; ++i)

for(j = 0; j < cols2; ++j)

{

cout << " " << mul[i][j];

if(j == cols2-1)

cout << endl;

}

}

main();

}

User Tong
by
5.1k points