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>

Attachments
//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!*/

> 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).

how come it doesn't compile....it compiles really well in VISUAL C++ 6.0 without a single error.ok as u told me how do i round up the value and assign it to a variable.
eg:
i have 0.33345
and 0.003333

without using a if cond will i be able to assign the value 0 to the variable?

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
This article has been dead for over six months. Start a new discussion instead.