Can someone help em out with this:
Im suppose to do this:
http://www.ise.ufl.edu/cgs2421/cpp/files/projects/pro2.pdf
I gotta use loops to solve the horner algorythm
This is whta i have but is not working

#include <iostream>
using namespace std;

int main (void) 
{// VARIABLE DECLARATIONS:
	double choice;
	
	// EXECUTABLE CODE:
	cout << "CGS 2421 Polynom" << endl;
	cout << "This program evaluates the value of an arbitrary polynom" << endl;
	cout << "at a given point. x is input, and f(x) is the output " <<endl;
	cout << endl;
	
	do {
	// present menu of options

 	cout << "0. Quit" << endl;
    cout << "1. Set scientific format" << endl;
	cout << "2. Set fixed format" << endl;
	cout << "3. Evaluate polynom" << endl;
    cout << endl;
    cout << "Enter your choice : ";
    cin >> choice;
	
		if (choice < 0) {
		cout << "'"<< choice <<"'"" is the incorrect choice" <<	endl;
		}
		else if (choice > 3) {
		cout << "'"<< choice <<"'"" is the incorrect choice" <<	endl;
		}
		if (choice == 1) {
			cout.setf (ios::scientific);
		}
		if (choice == 2) {
			cout.setf (ios::fixed);
		}
			if (choice == 3) {
				double x;
				cout << "Enter x : ";
				cin >> x;
				double term;
				cout << "Enter termination value : ";
				cin >> term;
				cout << "Enter space separated coefficients (higher degree first) followed by value " << term << " : " << endl;				
				double a;
				double sum=0.0, num;
				cin >> a >> term;
			
				num = a;
				
				while (a != term) {
						
					sum+=sum*x+a;
					if (a > num) {num = a;}	
					
				}
					cout << "x=" << x << "; " << "f(x)=" << sum << endl;
				
				
			}

		}while (choice != 0);
		return 0;
}

I'm not sure what is wrong

ty

Salem commented: Well done on using code tags on your first post +19
Ancient Dragon commented: We like people who know how to post +25

It doesn't seem like you are taking in your coefficients properly. If you are going to do it the way you are, you need to expand the while loop around the statements where you get a new a and term each time.

Otherwise you're using the "a" you read in over and over again in the calculation.

You're also keeping "a" in "num" but never doing anything with it again.

So how would you recommend me doing that. I don't know much about
C++.... I'm taking it for a class and is the 3rd week on it.
How can I make the store the value of multiple coefficients for it to calculate??????
And what do you mean by expanding the while loop??
I though that using that if statemt inside the while would do the trick.... ty

Could you take me through a typical input/output (the output I can kinda gather) and elaborate on what the termination factor is?
I think I see what you are trying to do but I'm not sure. It seems to me like you want to take in a bunch of coefficients and store them all in a? Is that true?
Perhaps you're using a different approach, I was getting my info off of here. Apologies for the request for background it's been (well let's not say hehe) too long.

Yeah I'm actually trying to store the coefficients only in variable a... my teacher told me that it can be done..... abd the termination value is the last coefficient of the polynom that doesnt multyple times x.
2x^2+3x+3=0
In this case 3 will be the last term.
I dont know how to make the function evaluate the coefficients entered in the program.
ty

You need to take in the order of the polynomial from the user so that you can loop over the Horner's code that many times + 1 and take in the coefficient, do the processing and hang onto the result, all within the loop.

His method evaluate() is correct (EDIT: took a second look). You were adding the calculation back on to the value with the += and that is definitely incorrect.

Sorry for asking to sprecifically....... How do I do that????
I'm trying to get the input of coefficients inside the while loop but i need to initialiaze a first which i was told i don't need to by my teacher.
can you please rewrite that part of my code correctly.
I'm not sure how to fix it.
Ty and sry for asking so much.

It's rarely a bad idea to initialize something. Were you able to follow iamthwee's example? Post what you've attempted with the revision thusfar and we can straighten it out.

It's rarely a bad idea to initialize something. Were you able to follow iamthwee's example? Post what you've attempted with the revision thusfar and we can straighten it out.

I mostly dont understand the different terms he is using.
This is what I have so far:

#include <iostream>
using namespace std;

int main (void) 
{// VARIABLE DECLARATIONS:
	double choice;
	
	// EXECUTABLE CODE:
	cout << endl;
	cout << "CGS 2421 Polynom" << endl;
	cout << "This program evaluates the value of an arbitrary polynom" << endl;
	cout << "at a given point. x is input, and f(x) is the output " <<endl;
	cout << endl;
	
	do {
	
 	cout << "0. Quit" << endl;
    cout << "1. Set scientific format" << endl;
	cout << "2. Set fixed format" << endl;
	cout << "3. Evaluate polynom" << endl;
    cout << endl;
    cout << "Enter your choice : ";
    cin >> choice;
	
		if (choice < 0) {
		cout << "'"<< choice <<"'"" is the incorrect choice" <<	endl;
		}
		else if (choice > 3) {
		cout << "'"<< choice <<"'"" is the incorrect choice" <<	endl;
		}
		if (choice == 1) {
			cout.setf (ios::showpoint|ios::scientific);
		}
		if (choice == 2) {
			cout.setf (ios::showpoint|ios::fixed);
		}
			if (choice == 3) {
				double x;
				cout << "Enter x : ";
				cin >> x;
				double term;
				cout << "Enter termination value : ";
				cin >> term;
				cout << "Enter space separated coefficients (higher degree first) followed by value " << term << " : " << endl;				
				double a;
				double sum=0.0, num;
				cin >> a >> num >> term;
				
				
				while (a != term) {
					
					sum = sum * x + a; 
					sum++;
					if (a < num) {(num = a); }		
					
			}
					cout << "x=" << x << "; " << "f(x)=" << sum << endl;
			
			}

		}while (choice != 0);
		return 0;
}

a should be new at each turn of the crank so to speak, but you only read it in once.
Take a look at the example about 1/4 of the way down the page on this site. You take in your "first" coefficient 2, from 2*x^4, then bn = 2. You take in -3, if you're evaluating at x = 2, then you have 2*2-3 = 1 (using the prior bn, and now updated bn to be 1. There's no incrementing going on and it doesn't matter if this coefficient is less than the terminal one. Since your "an" is different on each step you need to read in a fresh one. You can use your while loop for this but you can change your condition to i < order+1 (but in that case you should certainly initialize i).

Based on all that I ended up with something like:

if(i == 0)
	bn = coeff;
else 
bn = bn*x+coeff;

with all that inside a for loop (but you can use while) along with the statement to get input from the user.

Maybe it's still clear as mud at this point. I just have some difficulty with what you have proposed... I think this is the simplest way to do it (well, iamthrwee's example initializes p using a ghost term inside his loop which is even simpler) but if your method is fundamentally not getting the input it needs I'm not sure how it would be functional.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.