I have to create a program that finds determinants of 2x2 matrix or a 3x3 matrix
ID: 3623601 • Letter: I
Question
I have to create a program that finds determinants of 2x2 matrix or a 3x3 matrix
so the determinant of a 2x2 matrix is:
|a(1,1),a(1,2)|
|a(2,1),a(2,2)| is equal to a(1,1)*a(2,2)-a(2,1)*a(1,2)
and the determinant of a 3x3 matrix is:
|a(1,1),a(1,2),a(1,3)|
|a(2,1),a(2,2),a(2,3)|
|a(3,1),a(3,2),a(3,3)|
is equal to
a(1,1)*|a(2,2),a(2,3)|-a(2,1)*|a(1,2),a(1,3)|-a(3,1)*|a(1,2),a(1,3)|
______|a(3,2),a(3,3)|______ |a(3,2),a(3,3)|______ |a(2,2),a(2,3)|
so I have to have 2 functions det2() for the 2x2 matrix and det3() for the 3x3 matrix
and I have to call on det2() to calculate the required 2x2 determinant in order to solve for det3()
so far I haven't been able to get my det2() function working so I haven't made det3() yet so I have det3() set equal to 1, but when I try to output my determinant it always comes out 0 here's my code I just feel overwhelmed right now i have 2 more functions besides det2() and det3() they are for input and for displaying...
#include
#include
using namespace std;
double det2(double a11,double a12,double a21,double a22);
double det3(double a11,double a12,double a13,double a21,double a22,double a23,double a31,double a32,double a33);
void InputMatrix(double& a11,double& a12,double& a13,double& a21,double& a22,double& a23,double& a31,double& a32,double& a33);
void displaydet(double a11,double a12,double a13,double a21,double a22,double a23,double a31,double a32,double a33,double d);
int main()
{
double a11=0,a12=0,a13=0,a21=0,a22=0,a23=0,a31=0,a32=0,a33=0,d=0;
d= det2(a11,a12,a21,a22);
d= det3(a11,a12,a13,a21,a22,a23,a31,a32,a33);
InputMatrix(a11,a12,a13,a21,a22,a23,a31,a32,a33);
displaydet(a11,a12,a13,a21,a22,a23,a31,a32,a33,d);
return 0;
}
//Purpose: calculate 2x2 matrix
double det2(double a11,double a12,double a21,double a22)
{
double d;
d=(a11*a22)-(a21*a12);
cout << d << endl;
return d;
}
//Purpose: calculate 3x3 matrix
double det3(double a11,double a12,double a13,double a21,double a22,double a23,double a31,double a32,double a33)
{
double d=0;
d=1;
return d;
}
//Purpose: function to input matrix
void InputMatrix(double& a11,double& a12,double& a13,double& a21,double& a22,double& a23,double& a31,double& a32,double& a33)
{
int matsize;
cout << "Please select 1 or 2: " << endl;
cout << " 1 to find the determinant of a 2 by 2 matrix" << endl;
cout << " 2 to find the determinant of a 3 by 3 matrix" << endl;
cin >> matsize;
switch (matsize)
{
case 1:
cout << "Please Enter a11" << endl;
cin >> a11;
cout << "Please Enter a12" << endl;
cin >> a12;
cout << "Please Enter a21" << endl;
cin >> a21;
cout << "Please Enter a22" << endl;
cin >> a22;
break;
case 2:
cout << "Please Enter a11" << endl;
cin >> a11;
cout << "Please Enter a12" << endl;
cin >> a12;
cout << "Please Enter a13" << endl;
cin >> a13;
cout << "Please Enter a21" << endl;
cin >> a21;
cout << "Please Enter a22" << endl;
cin >> a22;
cout << "Please Enter a23" << endl;
cin >> a23;
cout << "Please Enter a31" << endl;
cin >> a31;
cout << "Please Enter a32" << endl;
cin >> a32;
cout << "Please Enter a33" << endl;
cin >> a33;
break;
}
}
//Purpose: displays matrix and determinant
void displaydet(double a11,double a12,double a13,double a21,double a22,double a23,double a31,double a32,double a33,double d)
{
cout << a11 << " " << a12 << " " << a13 << " " << endl;
cout << a21 << " " << a22 << " " << a23 << " " << endl;
cout << a31 << " " << a32 << " " << a33 << " " << endl;
cout << "Determinant is " << d << endl;
}
Explanation / Answer
please rate - thanks
how is this, sorry I scrapped yours
#include<iostream>
#include<cmath>
using namespace std;
int deter2(int[][3]);
int deter3(int[][3]);
void input(int[][3],int);
void print(int[][3],int);
int main()
{
int a[3][3];
int n;
int result;
cout<<"Enter dimension of matrix: ";
cin>>n;
input(a,n);
print(a,n);
if(n==2)
cout<<"The determinant is:"<<deter2(a)<<endl;
else
cout<<"The determinant is:"<<deter3(a)<<endl;
system("pause");
}
void input(int b[][3],int m) //FUNCTION FOR READING MATRIX
{int i,j;
for( i=0;i<m;i++)
for(j=0;j<m;j++)
{cout<<"Enter value at row "<<i<<" column "<<j<<": ";
cin>>b[i][j];
}
}
void print(int b[][3],int m)
{int i,j;
cout<<" Matrix ";
for( i=0;i<m;i++)
{for(int j=0;j<m;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
}
int deter2(int b[ ][3])
{
int sum ,c[5][5];
{
sum = b[0][0]*b[1][1] - b[0][1]*b[1][0];
return sum;
}
}
int deter3(int b[3][3])
{
int i,j,sum = 0,c[3][3];
for(int p=0;p<3;p++)
{int h = 0,k = 0;
for(i=1;i<3;i++)
{for( j=0;j<3;j++)
{if(j!=p)
{c[h][k]=b[i][j];
k++;
if(k==2)
{h++;
k=0;
}
}
}
}
sum = sum + b[0][p]*(int)pow(-1.,p)*deter2(c);
}
return sum;
}