Practical Web Programming

Sunday, February 03, 2008

Gauss - Jordan Reduction Source Codes in C/C++

This program will process the matrix entered by the user to Reduced-Row Echelon Form. This method is known as Gauss-Jordan Reduction. I made this as a Midterm Project in Numerical Analysis.

NOTE: This is a complete and running C/C++ program.

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#define PAUSE 10000

/*++++++++++++++ FUNCTION DECLARATION ++++++++++++++++++++++++*/
void Main_Screen();
void Dimension();
void Enter_To_The_Matrix();
void About_The_Matrix();
void The_Programmer();
void Help_Topics();
void ProcessMatrix(float Matrix[10][10],int row,int col);
void SwapRow(float Matrix[10][10],int R1,int R2,int col);
void MultiplyRow(float Matrix[10][10],int R1,int R2,int col);
void MultiplyAddRow(float Matrix[10][10],int R1,int R2,int col);
void centerY(char *text,int y,int fore,int back);
void centerX(char text[],int x1,int y1,int fore,int back);
void box1(int x1,int y1,int x2,int y2,int fore);
void wall1(int x1,int y1,int x2,int y2,int fore,int back);
void shadow(int x1,int y1,int x2,int y2,int fore,int back);

/*++++++++++++++ GLOBAL VARIABLES ++++++++++++++++++++++++++++*/
char ans, buffer[10], rowBuf[10], colBuf[10];
int x, y, z, X, Y, Z, check1,check2;
float matrix[10][10], InputBuf, row, col, num;

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/*########### S T A R T of M A I N #########################*/
void main()
{
while(1 == 1)
{
Main_Screen();
ans=0;
while(ans!='1' && ans!='2' && ans!='3')
{
ans = getch();
}
switch(ans)
{
case 49: Dimension(); break;
case 50: About_The_Matrix(); break;
case 51: exit(0);
}
}
}

void Dimension()
{
shadow(20,10,66,20,8,0); wall1(18,9,64,19,3,3);
box1(18,10,63,17,8); box1(28,12,52,16,8);
centerY(" I n s i d e t h ‚ M … t r i x ",10,15,4);
centerY("Dimension",12,0,3);
while(1 == 1)
{
centerY(" x ",14,14,1);
gotoxy(37,14); gets(rowBuf); row = atof(rowBuf);
gotoxy(43,14); gets(colBuf); col = atof(colBuf);
gotoxy(44,14);
if(row > 0 && row < 6 && col > 1 && col < 7 )
{
break;
}
}
Enter_To_The_Matrix();
}

void Enter_To_The_Matrix()
{
char label[]="ABCDE";
ans=0;
while(ans != 'N')
{
shadow(10,4,76,24,8,0); wall1(8,3,74,23,7,7); box1(8,4,73,21,8);
box1(10,10,47,20,9); box1(48,6,71,20,1);
centerY(" I n s i d e t h ‚ M … t i x ",4,10,4);
centerX("Gauss-Jordan Reduction", 60,6,14,1);
centerX("Enter elements :", 19,9,0,7);
for(x=0 ; x<row ; x++)
{
for(y=0,z=0 ; y<col ; y++,z=z+6)
{
if(y < col-1)
{
textattr(14+(4<<4));
gotoxy(12+z,11); cprintf(" %c ",label[y]);
}
while(1 == 1)
{
centerX(" ",30,9,14,1);
gotoxy(29,9); gets(buffer);
InputBuf = atof(buffer);
if(InputBuf < 100 && InputBuf > -100)
{
break;
}
}
if(y == col-1)
{
label[y] = ' ';
}
matrix[x][y] = InputBuf; textattr(0+(7<<4));
gotoxy(12+z,13+x); cprintf("%.0f%c",matrix[x][y],label[y]);
}
}
ProcessMatrix(matrix,row,col);
centerX("Again? (Y/N)", 15,22,1,7);
ans=0;
while(ans != 'Y' && ans != 'N' && ans != 27)
{
ans = toupper(getch());
}
if(ans == 'N')
{
return;
}
if(ans == 'Y')
{
Dimension();
}
if(ans == 27)
{
return;
}
}
}

void ProcessMatrix(float Matrix[10][10],int row,int col)
{
char label[]="ABCDE";
X=0;Y=0;Z=1;
while(X<row)
{
for(x=X;x<row;x++)
{
if(Matrix[x][Y] == 1)
{
SwapRow(Matrix,x,Y,col);
}
}
if(Matrix[X][Y] == 0)
{
for(x=X;x<row-1;x++)
{
if(Matrix[x][Y] != 0)
{
SwapRow(Matrix,x,Y,col);
}
}
}
MultiplyRow(Matrix,X,Y,col);
for(y=Z;y<row;y++)
{
MultiplyAddRow(Matrix,y,Y,col);
}
X++;Y++;Z++;
}
for(Z=1;Z<6;Z++)
{
X=0;Y=Z;
while(X<row)
{
MultiplyAddRow(Matrix,X,Y,col);
X++;Y++;
}
}
for(y=0;y<col-1;y++)
{
check1=0;
if(y==col-2)
{
for(x=0;x<col;x++)
{
if(Matrix[row-1][x]==0)
{
check1++;
}
}
}
check2=0;
if(check1==col && row<col)
{
check2=1;
centerX("Ä> Has infinitely many solution!",29,19,4,7);
}
if(col>=row+2)
{
check2=1;
centerX("Ä> No possible solution! ",29,19,4,7);
}
}
for(y=0,z=0;y<col-1;y++,z=z+2)
{
textattr(15+(4<<4));
gotoxy(50,9+z); cprintf(" %c ",label[y]);
textattr(8+(7<<4));
gotoxy(54,9+z); cprintf("=");
if(check2==0)
{
textattr(0+(7<<4));
gotoxy(56,9+z); cprintf(" %.1f ",Matrix[y][col-1]);
}
}
for(x=0;x<row;x++)
{
for(y=0,z=0;y<col;y++,z=z+6)
{
textattr(0+(7<<4));
gotoxy(12+z,13+x); cprintf(" ");
gotoxy(12+z,13+x); cprintf("%.1f",Matrix[x][y]);
}
}
}

void SwapRow(float Matrix[10][10],int R1,int R2,int col)
{
float buffer[10];
for(x = 0; x < col; x++)
{
buffer[x] = Matrix[R1][x];
Matrix[R1][x] = Matrix[R2][x];
matrix[R2][x] = buffer[x];
}
}

void MultiplyRow(float Matrix[10][10],int R1,int R2,int col)
{
if(Matrix[R1][R2] == 0)
{
return;
}
num = (1 / Matrix[R1][R2]);

for(x = 0; x < col; x++)
{
Matrix[R1][x] = Matrix[R1][x] * num;
}
}

void MultiplyAddRow(float Matrix[10][10],int R1,int R2,int col)
{
float buffer[10];
num = Matrix[R1][R2];
if(num!=0)
{
num = num*(-1);
}
for(x = 0; x < col; x++)
{
buffer[x] = Matrix[R2][x];
buffer[x] = buffer[x] * num;
Matrix[R1][x] = Matrix[R1][x] + buffer[x];
}
}

void About_The_Matrix()
{
while(1 == 1)
{
shadow(14,6,72,20,8,0); wall1(12,5,70,19,3,3);
box1(12,6,69,18,8); shadow(18,9,68,18,8,0); wall1(16,8,66,17,4,4);
centerY(" A B O U T t h ‚ M … t r i x ",6,15,4);
centerX(" 1 ", 30,10,14,9);
centerX(" 2 ", 51,10,14,9);
centerX(" 3 ", 40,13,14,9);
centerX("The Programmers", 30,11,15,4);
centerX("Help Topics", 51,11,15,4);
centerX("Return To Main Menu", 40,14,15,4);
ans=0;
while(ans != '1' && ans != '2' && ans != '3'&& ans != 27)
{
ans = toupper(getch());
}
switch(ans)
{
case 49: The_Programmer(); break;
case 50: Help_Topics(); break;
case 51: return;
case 27: return;
}
}
}

void The_Programmer()
{
while(1 == 1)
{
shadow(10,4,76,24,8,0); wall1(8,3,74,23,7,7); box1(8,4,73,21,8);
centerY(" T h e P r o g r a m m e r s ",4,10,4);
centerY("Joel Badinas",8,0,7);
centerY("Michelle Claridad",9,0,7);
centerY("Dan Virgel Ratilla",10,0,7);
centerY("Frederick Jhun Layno",11,0,7);
centerY("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ",13,6,7);
centerY("The programmers are 3rd year students of",15,0,7);
centerY("the University of Southeastern Philippines taking up",16,0,7);
centerY("Bachelor of Science in Computer Science.",17,0,7);
centerX("<Esc> to cancel", 16,22,1,7);
while(ans != 27)
{
ans = getch();
}
if(ans == 27)
{
return;
}
}
}

void Help_Topics()
{
while(1 == 1)
{
shadow(10,4,76,24,8,0); wall1(8,3,74,23,7,7); box1(8,4,73,21,8);
centerY(" H e l p T o p i c s ",4,10,4);
centerX("This program will process the matrix entered by the user ",41,8,0,7);
centerX("to Reduced-Row Echelon Form(R.R.E.F.). This method is ",41,9,0,7);
centerX("known as Gauss-Jordan Reduction. The program will display ",41,10,0,7);
centerX("the result of the operation or the value corresponding the",41,11,0,7);
centerX("letter. ",41,12,0,7);

centerX("Please be guided that the dimension of the matrix the ",41,14,0,7);
centerX("program will accept must have a minimum of '1 x 2' and ",41,15,0,7);
centerX("a maximum of '5 x 6'. Also, the value of each element ",41,16,0,7);
centerX("should not be greater than '100'. ",41,17,0,7);

centerX("<Esc> to cancel", 16,22,1,7);
while(ans != 27)
{
ans = getch();
}
if(ans == 27)
{
return;
}
}
}

void Main_Screen()
{
textattr(1+(1<<4));
clrscr(); box1(1,1,80,24,7);
centerY("th‚ M…trix",2,10,1);
centerY(" Copyright(c) 2002 ",3,14,1);
centerY(" A Midterm Project in Numerical Analysis ",4,15,1);
centerY(" Submitted on February 2002 ",5,15,1);
textattr(13+(1<<4));
gotoxy(12,9); cprintf(" Û Û Üß ");
gotoxy(12,10); cprintf(" ÜÜÛÜ ÛÜÜÜ ÜÜÜÜ ");
gotoxy(12,11); cprintf(" Û Û Û ÛÜÜÛ ");
gotoxy(12,12); cprintf(" ÛÛ ÛÛ Û ÛÜÜÜ ");
gotoxy(12,13); cprintf("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿");
textattr(10+(1<<4));
gotoxy(29,8); cprintf(" ÛÛÛÜ ÜÛÛÛ Ü ÛÛ ");
gotoxy(29,9); cprintf(" ÛÛÛÛÛ ÛÛÛÛÛ ß ßßÛÛßß ");
gotoxy(29,10); cprintf(" ÛÛÛ Û Û ÛÛÛ ßßßßÜ ÛÛ Û Üßß Ü ÛÜ ÜÛ ");
gotoxy(29,11); cprintf(" ÛÛÛ ÛÛÛ ÛÛÛ ÜßßßÛ ÛÛ Ûß Û ßÛÜÜÛß ");
gotoxy(29,12); cprintf(" ÛÛÛ ß ÛÛÛ Þ Û ÛÛ Û Û ÛÛ ");
gotoxy(29,13); cprintf(" ÛÛÛ ÛÛÛ ßÜÜßÛ ÛÛ Û Û ÜÛßßÛÜ ");
gotoxy(28,14); cprintf("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄßßÄÄÄÄßßÄ ");
textattr(12+(1<<4));
gotoxy(57,8); cprintf(" Ü ");
gotoxy(57,9); cprintf("ßÜß");
centerY(" G A U S S - J O R D A N R E D U C T I O N ",15,4,2);
wall1(5,17,77,24,7,1);
wall1(10,18,30,22,4,4); wall1(32,18,50,22,4,4); wall1(52,18,72,22,4,4);
shadow(12,19,32,23,8,0); shadow(34,19,52,23,8,0); shadow(54,19,74,23,8,0);
centerX(" 1 ", 20,19,14,9);
centerX(" 2 ", 41,19,14,9);
centerX(" 3 ", 62,19,14,9);
centerX(" E N T E R ", 20,20,15,4);
centerX(" A B O U T ", 41,20,15,4);
centerX(" Q U I T ", 62,20,15,4);
centerX("Type number to select", 12,25,14,1);
centerX("D rkm…n", 76,25,3,1);
}

/*################ E N D ##################################*/




/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void centerX(char text[],int x1,int y1,int fore,int back)
{
int x;
textattr(fore+(back<<4));
x = (x1 - ((x1-x1)/2)) - (strlen(text)/2);
gotoxy(x,y1); cprintf("%s",text);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void centerY(char *text,int y,int fore,int back)
{
textattr(fore+(back<<4));
gotoxy(40-strlen(text)/2,y);cprintf("%s",text);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void box1(int x1,int y1,int x2,int y2,int fore)
{
int i; textcolor(fore);
for(i=x1;i<x2;i++) {
gotoxy(i,y1);cprintf("Ä");
gotoxy(i,y2);cprintf("Ä"); }
for(i=y1;i<y2;i++) {
gotoxy(x1,i);cprintf("³");
gotoxy(x2,i);cprintf("³"); }
gotoxy(x1,y1);cprintf("Ú");
gotoxy(x2,y1);cprintf("¿");
gotoxy(x1,y2);cprintf("À");
gotoxy(x2,y2);cprintf("Ù");
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void wall1(int x1,int y1,int x2,int y2,int fore,int back)
{
int i,j;
for(j = y1; j < y2; j++) {
for(i = x1;i < x2; i++) {
gotoxy(i,j); textattr(fore+(back<<4)); cprintf("°");} }
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void shadow(int x1,int y1,int x2,int y2,int fore,int back)
{
int i,j;
for(j = y1; j < y2; j++) {
for(i = x1;i < x2; i++) {
if(i+2 >= x2 || j+1 >= y2) {
gotoxy(i,j); textattr(fore+(back<<4)); cprintf("°"); } } }
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


/*+++++++++++++ E X C E S S +++++++++++++++++++++++++++++++*/
/*X=0;Y=1;
while(X<row)
{
MultiplyAddRow(Matrix,X,Y,col);
X++;Y++;
}
X=0;Y=2;
while(X<row)
{
MultiplyAddRow(Matrix,X,Y,col);
X++;Y++;
}

X=0;Y=3;
while(X<row)
{
MultiplyAddRow(Matrix,X,Y,col);
X++;Y++;
}*/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

0 comments:

Recent Post