Polynomial graph

Oct 17, 2013 at 7:33pm
Hey!

I'm trying to come up with a code that asks the user to enter the values of the coefficients a,b,c,d of the polynomial function a*x*x*x+b*x*x+c*x+d and display the function using asterisks with the x-axis being vertical and the y-axis horizontal. So it would look something like this:

Example:x*x+4
Enter coefficient a: 0
Enter coefficient b: 1
Enter coefficient c: 0
Enter coefficient d: 4:

0 | *
1 | *
2 | *
3 | *
4 | *
5 | *

EDIT: I can't seem to be able to show the graph properly, it seems that I'm not allowed to use multiple spaces here ://.

I believe I have to use a for loop or something similar but all I came up yet is this:

#include <iostream>
using namespace std;

main(){

int a, b, c, d, x=1;
float pol;

cout << "Enter coefficient a: ";
cin >> a;
cout << "Enter coefficient b: ";
cin >> b;
cout << "Enter coefficient c: ";
cin >> c;
cout << "Enter coefficient d: ";
cin >> d;
pol = a*x*x*x+b*x*x+c*x+d;
for(i=
cout << pol << endl;
}

Thanks!
Last edited on Oct 17, 2013 at 7:36pm
Oct 17, 2013 at 9:26pm
Oct 22, 2013 at 9:00am
That wasn't very helpful... I know I'm supposed to use a for loop.
Oct 22, 2013 at 9:32am
You'd better make it clear - what you want use loop for and what hinders you from using it?

Do you have problem calculation the value of the polynom in the loop for several different values of X? But what the problem can be here...

By the way:

pol = a*x*x*x+b*x*x+c*x+d;

could be refactored to:

pol = ((a*x+b)*x+c)*x+d;

and that could be calculated with a loop too, if you want to put coefficients into array.
Last edited on Oct 22, 2013 at 9:33am
Oct 22, 2013 at 9:37am
I can't seem to be able to show the graph properly, it seems that I'm not allowed to use multiple spaces here ://.

You can use output tags to show us the graph. You should also use code tags when posting code, to format it correctly and make it more readable.

You can use the buttons to the right of the edit window when posting.
Topic archived. No new replies allowed.