Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program should prompt the user with the following menu: Matrix Algebra Packa

ID: 3541341 • Letter: T

Question

The program should prompt the user with the following menu:

Matrix Algebra Package:

a.) VECTOR ADDITION

b.) VECTOR PRODUCT

c.) VECTOR INNER PRODUCT

d.) PRODUCT MATRIX VECTOR

e.) PRODUCT MATRIX MATRIX

f.) TRANSPOSE MATRIX

g.) EXIT

Enter the index for the task you want to perform:

Then it should ask the user for the matrices and/or vectors needed for the task. The output should show the input matrices/vectors and the result matrix/vector. After given the output the program should present the menu again. You shuld implement functions and use pointers.

Explanation / Answer

#include<iostream.h>

#include<conio.h>

#include <cstdlib>

#include <math.h>

class vector

{   

public:

int x,y,z;

void read()

{

cout<<" Enter the magnitude of i : ";

cin>>x;

cout<<" Enter the magnitude of j : ";

cin>>y;

cout<<" Enter the magnitude of k : ";

cin>>z;

}

vector operator -()

{

x=-x;

y=-y;

z=-z;

}

vector operator +(vector b)

{

vector c;

c.x=x+b.x;

c.y=y+b.y;

c.z=z+b.z;

return c;

}

vector operator *(vector b)

{

int prod;

vector c;

c.x=x*b.x;

c.y=y*b.y;

c.z=z*b.z;

prod=c.x+c.y+c.z;

cout<<" Inner Product = ";

cout<<prod;

vector d;

d.x=(y*b.z)-(z*b.y);

d.y=(x*b.z)-(z*b.x);

d.z=(x*b.y)-(y*b.x);

return d;

}

void display()

{

cout<<x<<"i"<<"+"<<y<<"j"<<"+"<<z<<"k";

}

};


using namespace std;


void vecmat_mult()

{

int mat[10][10];

int vec[3];

int out[10];

int c,d,m,n,i,j;

cout<<"Enter the number of rows and columns of matrix"<<endl;

cin>>m;

cin>>n;

cout<<"Enter the elements of matrix"<<endl;

for ( c = 0 ; c < m ; c++ )

{

for ( d = 0 ; d < n ; d++ )

{

cout<<"Enter elements mat"<<c+1<<d+1<<" ";

cin>>mat[c][d];

}

}


cout<<"Enter Vector Component"<<endl;

  

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

{

cin>>vec[i];

}


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

{

out[i] = 0;

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

{

out[i] += mat[i][j] * vec[j];

}

}

cout<<"Result Product is ";

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

{

cout<<out[i]<<" "<<endl;

}

}


void matrixmult()

{

int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;

cout<<"Enter number of rows and columns of first matrix ";

cin>>r1>>c1;

cout<<"Enter number of rows and columns of second matrix ";

cin>>r2>>c2;

if(r2==c1)

{

cout<<"Enter rows and columns of First matrix ";

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

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

cin>>m1[i][j];

cout<<"First Matrix is : ";

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

{

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

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

cout<<" ";

}

cout<<"Enter rows and columns of Second matrix ";

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

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

cin>>m2[i][j];

cout<<"Second Matrix is: ";

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

{

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

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

cout<<" ";

}

cout<<"Multiplication of the Matrices: ";

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

{

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

{

mult[i][j]=0;

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

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

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

}

cout<<" ";

}

}

else

{

cout<<"Matrix multiplication cannot be done";

}

}

void matrixtranspose()

{

int a[10][10], trans[10][10], r, c, i, j;

cout<<"Enter rows and column of matrix: "<<endl;

cin>>r>>c;

cout<<" Enter elements of matrix: ";

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

{

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

{

cout<<"Enter elements a"<<i+1<<j+1<<" ";

cin>>a[i][j];

}

}

/*Displaying the matrix a[][] */

cout<<" Entered Matrix: ";

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

{

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

{

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

if(j==c-1)

cout<<" ";

}

}

/* Finding transpose of matrix a[]

[] and storing it in array trans[][]. */


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

{

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

{

trans[j][i]=a[i][j];

}

}

/* Displaying the transpose,i.e, Displaying array trans[][].*/


cout<<" Transpose of Matrix: ";

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

{

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

{

printf("%d ",trans[i][j]); if(j==r-1) printf(" ");

}

}

}


int main()

{

vector v1,v2,v3;

int choice,cont;

do

{

cout<<" VECTORS & MaTRIX Algebra 1.Vector Addition 2.Vector Product 3.Product Matrix-Vector 4.Product Matrix-Matrix 5.Transpose Matrix 6.Exit";

cout<<" Enter your choice : ";

cin>>choice;

switch(choice)

{

case 1 : cout<<" Enter the First Vector : ";

v1.read();

cout<<" Enter the Second Vector : ";

v2.read();

v3=v1+v2;

cout<<" The Sum of Vectors : ";

v3.display();

break;

case 2 : cout<<" Enter the First Vector : ";

v1.read();

cout<<" Enter the Second Vector : ";

v2.read();

v3=v1*v2;

cout<<" Cross Product = ";

v3.display();

break;

case 3 : vecmat_mult();

break;

case 4 : matrixmult();

break;

case 5 : matrixtranspose();

break;

case 6 : exit(0);

default : cout<<" Invalid Choice";

}

cout<<" Do you want to continue?(1-YES,0-NO) ";

cin>>cont;

}while(cont==1);

return 0;

}