Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

DATA STRUCTURE

Task 02

Muskan

023-21-0129

Section E

Faryal shamsi
/* 1. Take two matrices(3x3) as input from user

2. Ask from user to add, subtract or multiply option

3. Print relevant output*/

Code:

/* 1. Take two matrices(3x3) as input from user

2. Ask from user to add, subtract or multiply option

3. Print relevant output*/

#include <iostream>

using namespace std;

int main()

int array1 [3][3] , array2 [3][3];

cout<<"Enter the elements for first 3x3 matrix:\n";

for(int i = 0 ; i<3 ; i++)

for (int j = 0 ; j<3 ; j++)

cin>> array1[i][j];
}

cout<<"Enter the elements for second 3x3 matrix:\n";

for(int i = 0 ; i<3 ; i++)

for (int j = 0 ; j<3 ; j++)

cin>> array2[i][j];

int array3[3][3], sum;

char choice;

cout<<"What operation would you like to perform on the matrices(a/s/m):\t";

cin>>choice;

if( choice == 'a')

for(int i = 0 ; i<3 ; i++)

for(int j = 0 ; j<3 ; j++)

array3[i][j]=array1[i][j]+array2[i][j];

cout<<"Addition of two matrices is equal to :\n\t";


for(int i = 0 ; i<3 ; i++)

for (int j= 0; j<3 ; j++)

cout<< array3[i][j]<<"\t";

cout<<"\n"<<"\t";

else if ( choice == 's')

for(int i = 0 ; i<3 ; i++)

for(int j = 0 ; j<3 ; j++)

array3[i][j]=array1[i][j]-array2[i][j];

cout<<"Subtraction of two matrices is equal to :\n\t";

for(int i = 0 ; i<3 ; i++)

for (int j= 0; j<3 ; j++)

cout<< array3[i][j]<<"\t";

cout<<"\n"<<"\t";

}
}

else if (choice == 'm')

for (int i = 0 ; i<3 ; i++)

for (int j = 0 ; j<3 ; j++)

sum = 0 ;

for (int k = 0 ; k <3 ; k++)

sum = sum +(array1[i][k]*array2[k][j]);

array3[i][j]= sum;

cout<<"Product of two matrices is equal to :\n\t";

for(int i = 0 ; i<3 ; i++)

for (int j= 0; j<3 ; j++)

cout<< array3[i][j]<<"\t";

cout<<"\n"<<"\t";

return 0 ;
}

You might also like