this is a program which will help you to find the roots of any given function....
and please note that i have made comments on the lines which compiler gives an error.
actually it compiles really well.
i think it is something wrong in the values which are assigned to floating point variables.
my email is <snipped email>
//how to make it for decimal division,eg:x+=0.0001
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
double temp;
double x;
double valueArray[10];//max constants
int found=0;
int index,n,prntcnt;
cout<<"enter the value for n: ";
cin>>n;
if (n==1)
{cout<<"For ALL x equation is balanced"<<endl;
return 0;}
for (prntcnt=n-1;prntcnt>=0;prntcnt--){
cout<<"(A"<<n-prntcnt<<")";
if (prntcnt!=0)
cout<<'x'<<prntcnt<<" + ";
else
cout<<" = 0";
}
for (index=0; index<n;index++)
{
cout<<"\n\n";
cout<<"enter a value for (A"<<index+1<<"): ";
cin>>valueArray[index];
}
for (x=0;found!=n-1&&x<250;x+=0.01) {
for (temp=0,--index;index>=0;index--)
temp=temp+valueArray[index]*pow(x,n-1-index);
if (temp==0) //include "||fabs(temp)<1e-5)" to the boolean condition to get the answer
cout<<endl<<++found<<" ROOT is "<<x;
index=n;
}
if (found!=n-1){
for (x=-0.01;found!=n-1&&x>-250;x-=0.01) {
for (temp=0,index--;index>=0;index--)
temp=temp+valueArray[index]*pow(x,n-1-index);
if (temp==0||fabs(temp)<1e-5)
cout<<endl<<++found<<" ROOT is "<<x<<endl;
index=n;
}
}
if (found==0)
cout<<"NOT FOUND!";
else
{for ( ;found<n-1;)
cout<<++found<<" ROOT is one of the ABOVE"<<endl;
}
cout<<endl;
return 0;
}
/*
the case is when u give the equation 2x-1=0 it says "not found". but if u include the above
given st. to the if's boolean condition it will give the correct root without giving an error.
note that this is bcoz; when x is given a value which will make 2x-1 closer to zero the variable
temp is getting a very very small value but not ZERO!*/
- 4 Contributors
- forum4 Replies
- 13 Views
- 11 Years Discussion Span
- comment Latest Post by iamthwee
John A 1,896
> and please note that i have made comments on the lines which compiler gives an error.
> actually it compiles really well.
If you say that it compiles well, how come it has so many "errors"?
A good idea would be to post the code here instead of including it as an attachment; it's kind of a hassle to download the file and open it with our editor, you know?
As for your problem - sorry, I'm not that good at math. I kind of understand it; maybe someone with better math skills can explain this better...
As you noted in the bottom of your program, the variables are not completely accurate, and will sometimes contain a number that should be 0, but is instead something very close to 0. This can screw up your program, and is a common problem with other math problems such as matrix math. The best solution is to round down the number to 0 if it's smaller than a certain value (say 0.01; you might have to fine-tune it a little bit better).
~s.o.s~ 2,560
It depends on what kind of requirement is behind the rounding but you can do something like :
x = floor (0.23) ; // 0
x = floor (0.90) ; // 0
iamthwee 1,547
>which will help you to find the roots of any given function
Will it? The best way to find the roots of any given function, assuming the function has only one variable is to use numerical methods.
Some functions can also have complex solutions...