C program of Gauss Jordan Method

# C program of Gauss Jordan Method

6482
2
SHARE

C program to solve 3 Linear using Gauss Jordan Method. This program takes 4X4 values as input and displays output in form of cool animated matrix.Let us see of Gauss Jordan Method below:

How C program of Gauss Jordan Method works:

C program of Gauss Jordan Method solves 3 linear equations using 4×4 matrix with animation, showing step by step on the screen , for example if you want to solve these equations :

2×1+x2+x3 = 27
x1+x2+x3 = 27
x1+5×2+7×3 = 62
you should enter :
2 1 1 27 1 2 2 27 1 5 7 62

Press Enter after each number , then the matrix will appear on the screen , then press ENTER and relax to see that matrix solve step by step with explain in the left of matrix until the cross 1’s appears and then the results appears on screen.

### C program of Gauss Jordan Method :

```#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>

void Rep(int x, int y ,float res)
{
gotoxy(x,y);
cout<<"    ";
delay(200);
gotoxy(x,y);
delay(200);
cout<<"Û";
gotoxy(x,y);
delay(200);
//getch();       ///if you want it step by step make getch() active
cout<<res;
}

void Fill(int tt ,char ch)
{
for (int ii=0 ;ii<tt ;ii++)
cout<<ch;
}

void main()
{
char *name= {"W""r""i""t""t""e""n"" ""b""y"" "":"" ""C"" ""C"
"o""d""e"" ""C""h""a""m""p"};

c:	clrscr();
cout<<"Enter the matrix row by row\n\n\n";
cout<<"\nYou should enter (12) numbers.. then press ENTER\n";
float arr[3][4];

for(int i=0; i<3 ;i++)
for(int j=0; j<4; j++)
cin>>arr[i][j];

clrscr();
int f=0,d=0;
for(i=3; i<10; i+=3)
for(j=25; j<56; j+=10)
{
gotoxy(j,i);
cout<<arr[f][d];
d++;
if(d == 4)
{
f++;
d=0;
}

}
getch();

float t=1;
gotoxy(1,3);
cout<<1/(arr[0][0]);
cout<<"R1 ===>";

if (arr[0][0] != 1)
{
t = (1/(arr[0][0]));
arr[0][0] = t * arr[0][0];
Rep(25,3,arr[0][0]);
}

arr[0][1] = t * arr[0][1];
Rep(35,3,arr[0][1]);

arr[0][2] = t * arr[0][2];
Rep(45,3,arr[0][2]);

arr[0][3] = t * arr[0][3];
Rep(55,3,arr[0][3]);

gotoxy(1,3);
Fill(24,' ');

float mt1=arr[1][0];
gotoxy(1,6);
cout<<"R2="<<-mt1<<"R1+R2 ===>";

arr[1][0] = -mt1*arr[0][0]+arr[1][0];     // mlt mt pls
// what's upper
Rep(25,6,arr[1][0]);

arr[1][1] = -mt1*arr[0][1]+arr[1][1];
Rep(35,6,arr[1][1]);

arr[1][2] = -mt1*arr[0][2]+arr[1][2];
Rep(45,6,arr[1][2]);

arr[1][3] = -mt1*arr[0][3]+arr[1][3];
Rep(55,6,arr[1][3]);

gotoxy(1,6);
Fill(24,' ');

float mt2=arr[2][0];

gotoxy(1,9);
cout<<"R3="<<-mt2<<"R1+R3 ===>";

arr[2][0] = -mt2*arr[0][0]+arr[2][0]; //last
Rep(25,9,arr[2][0]);

arr[2][1] = -mt2*arr[0][1]+arr[2][1]; // 1st two should equals
Rep(35,9,arr[2][1]);
// last is diffrent from one
arr[2][2] = -mt2*arr[0][2]+arr[2][2]; // to one.
Rep(45,9,arr[2][2]);

arr[2][3] = -mt2*arr[0][3]+arr[2][3];
Rep(55,9,arr[2][3]);

gotoxy(1,9);
Fill(24,' ');

gotoxy(1,6);
cout<<(1/(arr[1][1]));
cout<<"R2 ===>";
t=1;
if (arr[1][1] != 1)           // start second pivot
{
t = (1/(arr[1][1]));
arr[1][1] = t * arr[1][1];
Rep(35,6,arr[1][1]);
}

arr[1][2] = t * arr[1][2];
Rep(45,6,arr[1][2]);

arr[1][3] = t * arr[1][3];
Rep(55,6,arr[1][3]);

gotoxy(1,6);
Fill(24,' ');

float mt3=arr[0][1];

gotoxy(1,3);
cout<<"R1="<<-mt3<<"R1+R2 ===>";

arr[0][1] = -mt3*arr[1][1]+arr[0][1];
Rep(35,3,arr[0][1]);

arr[0][2] = -mt3*arr[1][2]+arr[0][2];
Rep(45,3,arr[0][2]);

arr[0][3] = -mt3*arr[1][3]+arr[0][3];
Rep(55,3,arr[0][3]);

gotoxy(1,3);
Fill(24,' ');

gotoxy(1,9);
cout<<"R3="<<-mt3<<"R2+R3 ===>";

float mt4 = arr[2][1];

arr[2][1] = -mt4*arr[1][1]+arr[2][1];
Rep(35,9,arr[2][1]);

arr[2][2] = -mt4*arr[1][2]+arr[2][2];
Rep(45,9,arr[2][2]);

arr[2][3] = -mt4*arr[1][3]+arr[2][3];
Rep(55,9,arr[2][3]);

gotoxy(1,9);
Fill(24,' ');

gotoxy(1,9);
cout<<(1/(arr[2][2]))<<"R3 ===>";
t=1;
if (arr[2][2] != 1)           // start third pivot
{
t = (1/(arr[2][2]));
arr[2][2] = t * arr[2][2];
Rep(45,9,arr[2][2]);
}

arr[2][3] = t * arr[2][3];
Rep(55,9,arr[2][3]);

gotoxy(1,9);
Fill(24,' ');

float mt5 = arr[1][2];

gotoxy(1,6);
cout<<"R2="<<-mt3<<"R3+R2 ===>";

arr[1][2] = -mt5*arr[2][2]+arr[1][2];
Rep(45,6,arr[1][2]);

arr[1][3] = -mt5*arr[2][3]+arr[1][3];
Rep(55,6,arr[1][3]);

gotoxy(1,6);
Fill(24,' ');

float mt6 = arr[0][2];

gotoxy(1,3);
cout<<"R1="<<-mt3<<"R3+R1 ===>";

arr[0][2] = -mt6*arr[2][2]+arr[0][2];
Rep(45,3,arr[0][2]);

arr[0][3] = -mt6*arr[2][3]+arr[0][3];
Rep(55,3,arr[0][3]);

gotoxy(1,3);
Fill(24,' ');

clrscr();
f=0,d=0;
for(i=3; i<10; i+=3)
for(j=25; j<56; j+=10)
{
gotoxy(j,i);
cout<<arr[f][d];
d++;
if(d == 4)
{
f++;
d=0;
}

}

gotoxy(25,3);        // flashing 1's
textcolor(191);      //
cprintf("1");        //
gotoxy(35,6);        //
cprintf("1");        //
gotoxy(45,9);        //
cprintf("1");        //

float x1 = arr[0][3];
float x2 = arr[1][3];
float x3 = arr[2][3];

delay(2000);
gotoxy(37,14);
textcolor(YELLOW);
cprintf("X1 = ");
cout<<x1;
delay(700);

gotoxy(37,16);
cprintf("X2 = ");
cout<<x2;
delay(700);

gotoxy(37,18);
cprintf("X3 = ");
cout<<x3;

delay(500);

for (int st=80 ;st>0 ;st--)
{
gotoxy(st,24);
//cout<<"Û";
delay(10);
}

int fs = 2;                // logo
int sc = 72;
//clrscr();
textcolor(YELLOW);
for( i=0 ;i<64 ;i++)
{
gotoxy(fs,25);
cprintf("ÛÛÛÛÛÛÛÛ");
gotoxy(fs,25);
cout<<"  ";
fs+=1;
delay(5);

gotoxy(sc,25);
cprintf("ÛÛÛÛÛÛÛÛ");
gotoxy(sc+6,25);
cout<<"  ";
sc-=1;
delay(5);

}

cout<<"    ";
for(int nm=0 ;nm<40 ;nm++)
{
cout<<name[nm];
delay(40);
}
textcolor(WHITE);
getch();
}```

I hope you all have enjoyed the code. If you face any issues with the code then let us know in form of comments..