Professional Documents
Culture Documents
h>
//prototypes
void starter();
//this function displays the start of the program
void getvalues(int R,int C,float A[][C+1]);//this function get values of augment
ed values
void printmatrix(int R,int C,float A[][C+1]);
//function will print t
he matrix
void Swap(int R,int C,float A[][C+1]);
//function will swap the rows of t
he matrix
float Echelon(int R1,int C1,float A1[][C1+1]);//function will convert matrix in
echelon form
float Infinite(int R2,int C2,float A2[][C2+1],float X1[]); //function for infini
te sol. case
float Unique(int R3,int C3, float A3[][C3+1],float X2[]);
//function for
no sol. case
void solution(int R,int C,float A[][C+1],float X[]); //function shows the sol
ution result
void endresult();
//function which ends
the program
int main()
//main function s
tarts here
{
starter();
//call to start
er function
int i,j,R,C;
//declaration of variables u
sed in main
//now it will take user input for how many unknown variables of ea
ch equation
printf("\n\tEnter Number of unknown variables.\n\t");
//a
sk the user
scanf(" %d",&R);
//get the valu
e from user
C=R;
//no.of unknown vaariables is always equal to no o
f equations
float A[R][C+1];
//declaration of array which contains the augmente
d matrix
float X[10]={0};
//declaring array which contains row to swap
getvalues(R,C,A);
//call for getting the input for augmented matrix
printf("\tThe augmented matrix of the system of equations is\n");
printmatrix(R,C,A);
//call to show the matrix before echelon form
Swap(R,C,A);
//call to check the matrix for any zero value and
then swap
Echelon(R,C,A);
//call for converting in echelon form
printf("\tAfter reducing the matrix in echelon form\n");
printmatrix(R,C,A);
//print the matrix after echelon form
solution(R,C,A,X);
//call to display the final result
endresult();
//call to end the program
return 0;
//ensures program ends
}
//main bloack ends
//function for starting page starts here
void starter()//definition of the funtion starter of void type
{
//the very first welcoming screen display
printf("\n_______________________________________________________________");
printf("\n!!
");
printf("\n!!
\t
\t!!");
printf("\n!!
\t
\t!!");
printf("\n!!
\t WELCOME TO THE WORLD OF LINEAR EQUATIONS \t!!");
printf("\n!!
\t YOU CAN SOLVE ANY KIND OF EQUATION USING \t!!");
printf("\n!!
\t
\t!!");
printf("\n!!
\t \tGAUSSIAN ELIMINATION METHOD \t\t!!");
printf("\n!!
\t
\t!!");
printf("\n!!
\t
\t!!");
printf("\n!!______________________________________________________________!!
\n");
printf("\n!!
----------------GENERAL INSTRUCTION------------------- !!
");
printf("\n!!
1)Number of equations and variables should be equal.
!!
");
printf("\n!!
2)Gaussian method is applicable for square matrices.
!!
");
printf("\n!!
3)Equations in the form of matrix are as AX=B \t!!\n!!\t
where\t\t\t\t\t\t\t!!\n!!\t A is the matrix of coefficients.\t\t\t!!");
printf("\n!!
B is matrix for constants.\t\t\t\t!!");
printf("\n!!
X is the unknown matrix which contains variables.
!!
");
printf("\n!!______________________________________________________________!!
\n");
//general instructions for the user are also included so that he could unders
tand
// what he is doing
}
//starter function ends here
//definition of getvalues function starts here
//it will get the co efficient of the variables and adjust them in an array
void getvalues(int R,int C,float A[][C+1])
{
int i,j; //declare i for row check and j for colomn check
printf("\n\tEnter coefficients which are the elements of matrix A\n");
int equno=0; //asking user for input and equno is used to checkfor no of e
quation
for (i=0; i<R; i++)
//this for loop will move through each row
{
equno++;
//increment in variable
printf("\tequation %d \n",equno); //show equation no.whose values are e
ntered
for (j=0;j<C; j++) //for loop will move through each coloumn
{
printf("\t A[%d][%d] ",i+1,j+1);
scanf("%f",&A[i][j]);
//takes user input and adjust in the array
}
printf("\n");
//switch lines
}
printf("\tEnter Elements Of Matix B\n"); //asks for values in constant ma
trix
for (i=0; i<R; i++)
//loop for rows
{
for (j=C; j<C+1; j++) //loop for coloumn
{
printf("\t B[%d][%d] ",i+1,j+1);
scanf("%f",&A[i][j]); //takes input and adjust in last colomn of
array
}
}
}
}
else
{
X1[i]=-A2[R2-2][C2-1];
}
}
}//infinite function ends here
//function for unique solution
//back substitution of unique answers held here
float Unique(int R3,int C3, float A3[][C3+1],float X2[])
{
int i,j,Pass4,Pass5; //declaration variables
Pass4=0;
for (i=0; i<R3; i++) //loop for rows
{
Pass4++;
if (Pass4>1)
{
for (j=1; j<Pass4; j++) //loop for coloumn
{
X2[i]=X2[i]+A3[R3-(i+1)][C3] -X2[j-1]*A3[R3-(i+1)][C3-j
];
if (j==1)
{
A3[R3-(i+1)][C3]=0;
}
}
}
else
{
X2[i]=A3[R3-1][C3];
}
}
} //unique function ends here
//function for displaying answrs starts
//will show the respective solution of the matrices
void solution(int R,int C,float A[][C+1],float X[])
{
int i,j;
//declare i for row check and j for colomn check
printf("\n\t******HERE IS THE ANSWER******\t\n");
if (A[R-1][C-1]==0 && A[R-1][C]!=0) //condition for no solution
{
printf("\n\n\t The given system of equation has no solut
ion\n");
}
if (A[R-1][C-1]==0 && A[R-1][C]==0) //condition for infinite solution
{
Infinite(C,R,A,X); //calls infinite function
printf("\n\tThe given system of equation has Infinite S
olutions\n\t");
for (i=0; i<(R-1); i++) //loop for rows
{
printf("\tX[%d] = %.2ft\n",i+1,X[R-(i+2)]);
} //displays the result of infinite answer
printf("\tX[%d] = t\n",R);
}
// solution function ends here