Tuesday, June 3, 2014

Automatic Linear Equation Solver

INTRODUCTION:

Yes i am an mechanical engineer but i did take take computer science in high school so that did give me the love of C++ which led to this project in my first semester. I know this project has just been made obsolete by those new calculators which can even plot graphs( who needs MATLAB ?).

PURPOSE :
To solve linear equations, provide tutorial on solving linear equations and to generate exercise problems for the user to practice.

PROGRAM CODING :
/*
Title : Automatic Linear Equation Solver
Authors : Bharathan.R

Copyright :
Date of creation : 16.11.2011
Time of creation : 1:30 P.M IST

C-Program to
1. solve linear equation upto 3 variables
2. give a tutorial on how to solve linear equation upto 2 variables
3. give practice problems to solve linear equation upto 2 variables
*/


#include <iostream>
#include <string>
#include<conio.h>
#include<fstream.h>
using namespace std;
float Round(float arn)
{ int ba=arn*100;
float ha=ba;
float ca;
ca=ha/100;
return ca;
}
void solve()
{
system("CLS");
cout<<"\nEnter equations of the form ax+ by +cz = d\n\n";
int a1,b1,c1,d1,a2,b2,c2,d2,a3,b3,c3,d3;
char k1,l1,m1,n1,o1,p1,k2,l2,m2,n2,o2,p2,k3,l3,m3,n3,o3,p3;
printf("Enter First Equation : ");
scanf("%d",&a1);
scanf("%c",&k1);
scanf("%c",&l1);
scanf("%d",&b1);
if(l1=='-')
{
b1=-b1;
}
scanf("%c",&m1);
scanf("%c",&n1);
scanf("%d",&c1);
if(n1=='-')
{
c1=-c1;
}
scanf("%c",&o1);
scanf("%c",&p1);
scanf("%d",&d1);
printf("\nEnter Second Equation : ");
scanf("%d",&a2);
scanf("%c",&k2);
scanf("%c",&l2);
scanf("%d",&b2);
if(l2=='-')
{
b2=-b2;
}
scanf("%c",&m2);
scanf("%c",&n2);
scanf("%d",&c2);
if(n2=='-')
{
c2=-c2;
}
scanf("%c",&o2);
scanf("%c",&p2);
scanf("%d",&d2);
printf("\nEnter Third Equation : ");
scanf("%d",&a3);
scanf("%c",&k3);
scanf("%c",&l3);
scanf("%d",&b3);
if(l3=='-')
{
b3=-b3;
}
scanf("%c",&m3);
scanf("%c",&n3);
scanf("%d",&c3);
if(n3=='-')
{
c3=-c3;
}
scanf("%c",&o3);
scanf("%c",&p3);
scanf("%d",&d3);
float x1,x2,x3,x4,x5,x6,x8,x9,x10,x12,x13,x14;
float x11,x15,x7,x16;
float x,y,z;
x1=a1*(b2*c3)-a1*(b3*c2);
x2=-b1*(a2*c3)+b1*(a3*c2);
x3=c1*(a2*b3)-c1*(a3*b2);//to find delta
x16=(x1+x2)+x3;
if(x16==0)
{
printf("Either no solution or infifnite solution");
}
else
{
x4=d1*(b2*c3)-d1*(b3*c2);
x5=-b1*(d2*c3)+b1*(d3*c2);// to find delta of x
x6=c1*(d2*b3)-c1*(d3*b2);
x7=(x4+x5)+x6;
x8=a1*(d2*c3)-a1*(d3*c2);
x9=d1*(a2*c3)-d1*(a3*c2);//to find delta of y
x10=c1*(a2*d3)-c1*(a3*d2);
x11=(x8-x9+x10);
x12=a1*(b2*d3)-a1*(b3*d2);
x13=-b1*(a2*d3)+b1*(a3*d2);
x14=d1*(a2*b3)-d1*(a3*b2);//to find delta
x15=(x12+x13)+x14;
x=x7/x16;
y=x11/x16;
z=x15/x16;
x=Round(x);
y=Round(y);
z=Round(z);
cout<<"\n\n\tANS :";
cout<<"\n\n\t\tthe value of X="<<x;
cout<<"\n\n\t\tThe value of Y="<<y;
cout<<"\n\n\t\tThe value of Z="<<z;
}
}
void solve(float a,float b)
{
cout<<" \n\n\tANS : x = "<<b/a;
}
void solve(float a1,float b1,float c1,float a2,float b2,float c2)
{ float a=c1*b2;
float b=a/b1;
float g=b-c2;
float c=a1*b2;
float d=c/b1;
float e=d-a2;
float f=g/e;
float m=a1*f;
float n=c1-m;
float h=n/b1;
f=f+0.00001;
h=h+0.00001;
f=Round(f);
h=Round(h);
if(a1*b2==a2*b1)
cout<<"\n\nAnswer is undefined";
else
{
cout<<" \n\n\tANS : \n\t\tx = ";
cout<<f<<"\n\t\ty = "<<h;
}
}
void check(float a,float b)
{
float c;
c=b/a;
float f1;
cout<<" \n\n\n\tEnter Answer here : \n\t\tx = ";
cin>>f1;
f1=Round(f1);
c=Round(c);
system("CLS");
if(f1==c)
cout<<"\n\n\n\n\n\t\t\tCORRECT";
else
cout<<"\n\n\n\n\n\t\t\tWRONG";
cout<<"\n\n\n\n\n\t\t\tThe solution is \n\t\tx = "<<c;
}
void check(float a1,float b1,float c1,float a2,float b2,float c2)
{ float a=c1*b2;
float b=a/b1;
float g=b-c2;
float c=a1*b2;
float d=c/b1;
float e=d-a2;
float f=g/e;
float m=a1*f;
float n=c1-m;
float h=n/b1;
f=f+0.00001;
h=h+0.00001;
float f1,f2;
cout<<" \n\n\n\tEnter Answer here : \n\t\tx = ";
cin>>f1;
cout<<"\n\t\ty = ";
cin>>f2;
f1=Round(f1);
f2=Round(f2);
f=Round(f);
h=Round(h);
system("CLS");
if((h==f2)&&(f==f1))
cout<<"\n\n\n\n\n\t\t\tCORRECT";
else
cout<<"\n\n\n\n\n\t\t\tWRONG";
cout<<"\n\n\n\n\n\t\t\tThe solution is \n\t\tx = ";
cout<<f<<"\n\t\ty = "<<h;
}
int rn()
{
srand(time(0));
int randNumber = rand();
const int MAX = 30;
int number = (randNumber % MAX) + 1;
return number;
}
int main()
{
int ch1_i,ch2_i,ch3_i;
int a1,b1,c1,d1,a2,b2,c2,d2;
do
{
system("CLS");
cout<<"\n\n\n\n\t\t";
cout<<"WELCOME TO \n\n\n\n\t\t\tTHE LINEAR EQUATION SOLVER\n\n\n";
cout<<"\n\t\t\t\tBharathan.R";
getch();
system("CLS");
cout<<"\n\n\t\t\tTHE LINEAR EQUATION SOLVER\n\n\n";
cout<<"\n\t\t\t\t1.Automatic linear equation solver";
cout<<"\n\t\t\t\t2.Tutorial on how to solve linear equations";
cout<<"\n\t\t\t\t3.Practice Problems";
cout<<"\n\t\t\t\t4.Exit Program";
cout<<"\n\n\t\tEnter choice : ";
cin>>ch1_i;
if(ch1_i==1)
{
system("CLS");
cout<<"\n\n\n\n\n\t\t\t A.L.E.S \n\n\t\t\tLoading . . .";
getch();
system("CLS");
cout<<"\n\n\t\t\tChoose type of equation\n\n\n";
cout<<"\n\t\t\t\t1.One variable";
cout<<"\n\t\t\t\t2.Two variable";
cout<<"\n\t\t\t\t3.Three variable";
cout<<"\n\t\t\t\t4.Return to homepage";
cout<<"\n\n\t\tEnter choice : ";
cin>>ch2_i;
if(ch2_i==1)
{system("CLS");
cout<<"\n\n\n\t\tEnter the Equation to be solved ";
cout<<"\n\n\n\t\tEquation 1 : ax=b (where x is any random variable) \n\n\n Enter value of a : ";
cin>>a1;
cout<<"\n\nEnter value of b : ";
cin>>d1;
solve(a1,d1);
}
if(ch2_i==2)
{
system("CLS");
cout<<"\n\n\n\t\tEnter the Equations to be solved ";
cout<<"\n\n\n\t\tEquation 1 : a1x+b1y=c1 (where x,y are variables) \n\n\n Enter value of a1 : ";
cin>>a1;
cout<<"\n\nEnter value of b1 : ";
cin>>b1;
cout<<"\n\nEnter value of c1 : ";
cin>>d1;
cout<<"\n\n\n\t\tEquation 2 : a2x+b2y=c2 (where x,y are variables) \n\n\n Enter value of a2 : ";
cin>>a2;
cout<<"\n\nEnter value of b2 : ";
cin>>b2;
cout<<"\n\nEnter value of c2 : ";
cin>>d2;
solve(a1,b1,d1,a2,b2,d2);
}
if(ch2_i==3)
{
solve();
}
getch();
}
else if(ch1_i==2)
{
system("CLS");
cout<<"\n\n\n\n\n\t\t\t TUTORIAL \n\n\t\t\tLoading . . .";
getch();
system("CLS");
cout<<"\n\n\t\t\tChoose type of equation\n\n\n";
cout<<"\n\t\t\t\t1.One variable";
cout<<"\n\t\t\t\t2.Two variable";
cout<<"\n\t\t\t\t3.Return to homepage";
cout<<"\n\n\t\tEnter choice : ";
cin>>ch2_i;
system("CLS");
if(ch2_i==1)
{
FILE *fp;
char str[1000];
fp=fopen("lin1.txt","r");
{
while(fgets(str,1000,fp)!=NULL)
{
printf("%s",str);
}
}
getch();
fclose(fp);
}
if(ch2_i==2)
{
FILE *fp;
char str[1000];
fp=fopen("lin2.txt","r");
{
while(fgets(str,1000,fp)!=NULL)
{
printf("%s",str);// to open the file lin2
}
getch();
fclose(fp);
}
}
if(ch2_i==3)
{
}
getch();
}
else if(ch1_i==3)
{
system("CLS");
cout<<"\n\n\n\n\n\t\t\t EXERCISES \n\n\t\t\tLoading . . .";
getch();
system("CLS");
cout<<"\n\n\t\t\tChoose type of equation\n\n\n";
cout<<"\n\t\t\t\t1.One variable";
cout<<"\n\t\t\t\t2.Two variable";
cout<<"\n\t\t\t\t3.Return to homepage";
cout<<"\n\n\t\tEnter choice : ";
cin>>ch2_i;
if(ch2_i==2)
{ int i=1;
float a1=4;
float b1=3;
float c1=17;
float a2=3;
float b2=4;
float c2=18;
cout<<"Problem "<<i<<"\n\n\tsolve\n\t\t"<<a1<<"x +"<<b1<<"y ="<<c1<<"\n\t\t"<<a2<<"x +"<<b2<<"y ="<<c2;
check(a1,b1,c1,a2,b2,c2);
do{
a1=rn()-3;
b1=rn()+5;
c1=rn()-4;
a2=rn();
b2=rn()+7;
c2=rn()+2;
cout<<"\n\n\n\tHELPBAR\n\n1.Next Problem\n2.Home Page\nEnter choice : ";
cin>>ch3_i;
if(ch3_i==1)
{i++;
cout<<"Problem "<<i<<"\n\n\tsolve\n\t\t"<<a1<<"x +"<<b1<<"y ="<<c1<<"\n\t\t"<<a2<<"x +"<<b2<<"y ="<<c2;
check(a1,b1,c1,a2,b2,c2);}
}while(ch3_i!=2);
}
if(ch2_i==1)
{int i=1;
float a=4;
float b=8;
cout<<"Problem "<<i<<"\n\n\tsolve\n\t\t"<<a<<"x = "<<b;
check(a,b);
do{
a=rn()-3;
b=rn()+5;
cout<<"\n\n\n\tHELPBAR\n\n1.Next Problem\n2.Home Page\nEnter choice : ";
cin>>ch3_i;
if(ch3_i==1)
{i++;
cout<<"Problem "<<i<<"\n\n\tsolve\n\t\t"<<a<<"x = "<<b;
check(a,b);
}while(ch3_i!=2);
}
if(ch2_i==3)
{
}
getch();
}
else if(ch1_i==4)
{system("CLS");
cout<<"\n\n\n\n\n\t\t\tTHANK YOU\n\n\t\tFor using our program\n\n\n\t\t A product by\n\t\t B&CO";
}
else
{
system("CLS");
cout<<"\n\n\n\n\n\t\t\t Invalid Choice \n\n\t\t\tPlease try again";
getch();
}
}while(ch1_i!=4);
getch();
}

OUTPUT:

Home Page

Menu
 Solver 2D
Solver 3D
 Tutorial
 Practice Problems


Conclusion:

So if you ever have trouble solving linear equations check out our product. We promise to not only solve yours problems but we also promise to teach you to solve them in the future.

No comments:

Post a Comment