1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/* author : Chathura Widanage
date : 2012/02/03
purpose : solve simultaneous eqns
Title : simultaneous equations solver
*/
#include<stdio.h>
#include<process.h>
int unkw,ary=0;
int two();
int three();
int reset();
int main(){
printf("The Simultaneous Equations Solver\n------------------------------------------------------\n");
ask:{printf("No of unknown variables?\n1.) 2\n2.) 3\nYour choice here...");
scanf("%d",&unkw);}
if(unkw==1){
two();
}
else if(unkw==2){
three();
}
else{
printf("Invalid choice... Let's retry\n\n");
goto ask;
}
return 0;
}
/*****************************************************************************/
int two(){
float a[6];
printf("\nThinking that your equations are in the form of,\n a1X+a2Y=a3 ---- (1)\n b1X+b2Y=b3 ---- (2)\n\n");
for(int i=0;i<2;i++)
{
char let='a'+i;
for(int j=0;j<3;j++)
{
ary=ary+1;
printf("Enter %c%d.. ",let,j+1);
scanf("%f",&a[ary-1]);
}
}
float x=(float)((a[4]*a[2])-(a[1]*a[5]))/((a[0]*a[4])-(a[1]*a[3]));
float y=(float)((a[0]*a[5])-(a[3]*a[2]))/((a[0]*a[4])-(a[1]*a[3]));
printf("\nThe value of X is %f\n",x);
printf("The value of Y is %f\n\n",y);
reset();
return 0;
}
/*****************************************************************************/
int three(){
float a[12],bar=0;
printf("\nThinking that your equations are in the form of,\n a1X+a2Y+a3Z=a4 ---- (1)\n b1X+b2Y+b3Z=b4 ---- (2)\n c1X+c2Y+c3Z=c4 ---- (3)\n\n");
for(int i=0;i<3;i++)
{
char let='a'+i;
for(int j=0;j<4;j++)
{
ary=ary+1;
printf("Enter %c%d.. ",let,j+1);
scanf("%f",&a[ary-1]);
}
}
bar = (a[0]*(a[5]*a[10]-a[6]*a[9])-a[1]*(a[4]*a[10]-a[8]*a[6])+a[2]*(a[4]*a[9]-a[8]*a[5]));
float x=(a[3]*(a[5]*a[10]-a[9]*a[6])+a[7]*(a[9]*a[2]-a[1]*a[10])+a[11]*(a[1]*a[6]-a[5]*a[2]))/bar;
float y=(a[3]*(a[8]*a[6]-a[4]*a[10])+a[7]*(a[0]*a[10]-a[8]*a[2])+a[11]*(a[4]*a[2]-a[0]*a[6]))/bar;
float z=(a[3]*(a[4]*a[9]-a[8]*a[5])+a[7]*(a[8]*a[1]-a[0]*a[9])+a[11]*(a[0]*a[5]-a[4]*a[1]))/bar;
printf("\nThe value of X is %f\n",x);
printf("The value of Y is %f\n",y);
printf("The value of Z is %f\n\n",z);
reset();
return 0;
}
/****************************************************************************/
int reset(){
char answ;
ask:{
printf("Do you want to reset the application? Y/N\n");
scanf("%c",&answ);
}
if(answ=='Y'||answ=='y'){
printf("\n\n");
main();
}
else if(answ=='N'||answ=='n'){
exit(1);
}
else{
printf("Something wrong in your choice...... Let's retry\n\n");
goto ask;
}
return 0;
}
|