Sei sulla pagina 1di 5

#include<iostream>

#include<iomanip>

#include<conio.h>

#include<math.h>

#include<windows.h>

#include<stdlib.h>

using namespace std;

char choice;

const int Ra=2;

const int Ca=3;

const int Rb=3;

const int Cb=4;

void inputa(int a[][Ca], int Ra);

void inputb(int b[][Cb], int Rb);

void prca(int a[][Ca],int Ra);

void prcb(int a[][Cb],int Rb);

int main()

{int x=0;

int a[2][3],b[3][4];

cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\tThis program is prompt to solve a system of equations in 2 or 3


unkowns using inverse method.\n\tThe porgram will display the system of equations the determinant of
the coefficient matrix,\n\tthe inverse of the coefficient matrix and the solution set \n";

getch();

do{system("cls");

cout<<"\n\n\n\t\t\t\t\t\t\tMAIN MENU\n\t\t\t\t\t\tWhat would you like to do? \n\t\t\t\t\t\tA -


Systems of 2 equations \n\t\t\t\t\t\tB- Systems of 3 equations\n\t\t\t\t\t\tC-Exit";

cin>>choice;

switch (choice)

case 'a':

case 'A':{ cout<<"\n\t\t\t\t\t\tThe form of equation


is\n\t\t\t\t\t\ta1x+b1y=c1\n\t\t\t\t\t\ta2x+b2y=c2\n";inputa(a,Ra);
system("cls");cout<<"\t\t\t\t\t\tThe equations are\n\t\t\t\t\t\t"<<a[0][0]<<"x
+"<<a[0][1]<<"y = "<<a[0][2]<<"\n";

cout<<"\n\t\t\t\t\t\t and \n\n\t\t\t\t\t\t"<<a[1][0]<<"x +"<<a[1][1]<<"y =


"<<a[1][2]<<"\n";getch();

prca(a,Ra);

break;}

case 'b':

case 'B': {cout<<"\n\t\t\t\t\t\tThe form of equation


is\n\t\t\t\t\t\ta1x+b1y+c1z=d1\n\t\t\t\t\t\ta2x+b2y+c2z=d2\n\t\t\t\t\t\ta3x+b3y+c3z=d3";inputb(b,Rb);

system("cls");cout<<"\t\t\t\t\t\tThe equations are\n\t\t\t\t\t\t"<<b[0][0]<<"x


+"<<b[0][1]<<"y +"<<b[0][2]<<"z ="<<b[0][3]<<"\n";

cout<<"\n\t\t\t\t\t\t and \n\n\t\t\t\t\t\t"<<b[1][0]<<"x +"<<b[1][1]<<"y


+"<<b[1][2]<<"z="<<b[1][3]<<"\n";

cout<<"\n\t\t\t\t\t\t and \n\n\t\t\t\t\t\t"<<b[2][0]<<"x +"<<b[2][1]<<"y


+"<<b[2][2]<<"z="<<b[2][3]<<"\n";

prcb(b,Rb);getch();

break;}

case 'c':

case 'C': x=2;break;

default:

cout<<"\t\t\t\t\t\tInvalid entry try again"; Sleep(800);continue;

while(x<1);

getch ();

return 0;

void inputa(int a[][Ca],int Ra)

{int h,i;

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

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

{ if (h==0)

{ cout<<"\n\t\t\t\t\t\ta"<<i+1<<"?\n";
cin>>a[i][h];

if (h==1)

{cout<<"\t\t\t\t\t\tb"<<i+1<<"?\n";

cin>>a[i][h];

if(h==2)

{cout<<"\t\t\t\t\t\tc"<<i+1<<"?\n";

cin>>a[i][h];

void inputb(int b[][Cb],int Rb)

{int e,l;

for(e=0;e<4;e++)

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

{ if (e==0)

{ cout<<"\n\t\t\t\t\t\ta"<<l+1<<"?\n";

cin>>b[l][e];

if (e==1)

{cout<<"\t\t\t\t\t\tb"<<l+1<<"?\n";

cin>>b[l][e];

if(e==2)

{cout<<"\t\t\t\t\t\tc"<<l+1<<"?\n";

cin>>b[l][e];

if(e==3)
{cout<<"\t\t\t\t\t\td"<<l+1<<"?\n";

cin>>b[l][e];

void prca(int a[][Ca],int Ra)

{double determinant,ans1,ans2,ans3,ans4;

cout<<setprecision(2)<<fixed;

determinant=(1.00/((a[0][0]*a[1][1])-(a[0][1])*a[1][0]));

ans1=a[1][1]*a[0][2]+(a[0][1]*(-1)*a[1][2]);

ans2=ans1*determinant;

ans3=a[0][0]*a[1][2]+(a[1][0]*(-1)*a[0][2]);

ans4=ans3*determinant;

cout<<"\t\t\t\t\tThe determinant of the matrix is="<<determinant<<endl;

cout<<"\t\t\t\t\tTHe inverse of the matrix is"<<endl;

cout<<"\t\t\t\t\t|"<<a[1][1]<<" "<<a[0][1]*(-1)<<"|";

cout<<"\n\t\t\t\t\t|"<<a[1][0]*(-1)<<" "<<a[0][0]<<"|\n\t\t\t\t\tx="<<ans2<<" y="<<ans4;

getch();

void prcb(int b[][Cb],int Rb)

{double eterminant;

int c[3][4];

cout<<setprecision(2)<<fixed;

eterminant=((b[0][0]*b[1][1]*b[2][2])+(b[0][1]*b[1][2]*b[2][0])+(b[0][2]*b[1][0]*b[2][1]))-((b[2][0]*b[1][1
]*b[0][2])+(b[0][0]*b[2][1]*b[1][2])+(b[0][1]*b[1][0]*b[2][2]));

cout<<"\t\t\t\t\tThe determinant of the matrix is="<<eterminant<<endl;

c[0][0]=((b[1][1]*b[2][2])-(b[1][2]*b[2][1]));

c[1][1]=((b[0][0]*b[2][2])-(b[0][2]*b[2][0]));

c[2][2]=((b[0][0]*b[1][1])-(b[0][1]*b[1][0]));

c[0][1]=((b[0][1]*b[2][2])-(b[0][2]*b[2][1]))*(-1);

c[0][2]=((b[0][1]*b[1][2])-(b[0][2]*b[1][1]));
c[1][0]=((b[1][0]*b[2][2])-(b[1][2]*b[2][0]))*(-1);

c[1][2]=((b[0][0]*b[1][2])-(b[2][0]*b[1][0]))*(-1);

c[2][0]=((b[1][0]*b[2][1])-(b[1][1]*b[2][0]));

c[2][1]=((b[0][0]*b[2][1])-(b[0][1]*b[2][0]))*(-1);

cout<<"\t\t\t\t\t The inverse matrix is \n\t\t\t\t\t|"<<c[0][0]/eterminant<<" "<<c[0][1]/eterminant<<"


"<<c[0][2]/eterminant<<"|\n\t\t\t\t\t|";

cout<<c[1][0]/eterminant<<" "<<c[1][1]/eterminant<<" "<<c[1][2]/eterminant<<"|\n\t\t\t\t\t|";

cout<<c[2][0]/eterminant<<" "<<c[2][1]/eterminant<<" "<<c[2][2]/eterminant<<"|\n";

cout<<"\t\t\t\t\tx="<<((c[0][0]*b[0][3])+(c[0][1]*b[1][3])+(c[0][2]*b[2][3]))/eterminant<<endl;

cout<<"\t\t\t\t\ty="<<((c[1][0]*b[0][3])+(c[1][1]*b[1][3])+(c[1][2]*b[2][3]))/eterminant<<endl;

cout<<"\t\t\t\t\tz="<<((c[2][0]*b[0][3])+(c[2][1]*b[1][3])+(c[2][2]*b[2][3]))/eterminant<<endl;