help with creating program where you input an equation (x^2 + 3x +5) etc

Pages: 12
so, i'm creating my own program for a project and its urgent! D:

this is what it looks like so far :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//graphing calculator. plot points derive antiderive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double coeff[10];
double degree;
cout << "Welcome to my graphing utility!\n";
cout << "Insert your equation and the points you want to input and I will do the rest.\n";
cout << "What is the highest degree of your equation: (up to 10) \n";
cin >> degree;
for ( int n = 0 ; n<degree ; n++)
	{cout << "what is your number " << n +1 << " coefficient: \n";
	 //would i insert an array here?
     cin >>coeff[n+1];}
	




 
 system("pause");
}


.. can someone see if this is the right path? :O and.. is there an easier way to do this? such as where the user can just straight out input the equation without saying its degree etc?
Last edited on
i've made some progress! here's my new code!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//graphing calculator. plot points derive antiderive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double coeff[10]={0}, points[10]={0};
double degree, points;
cout << "Welcome to my graphing utility!\n";
cout << "Insert your equation and the points you want to input and I will do the rest.\n";
cout << "What is the highest degree of your equation: (up to 10) \n";
cin >> degree;

for ( int n = 0 ; n<degree ; n++)
	{cout << "what is your number " << n+1 << " coefficient: \n";
     cin >>coeff[n+1];}
cout >> coeff[10]

cout << "How many points do you want to plot: (up to 10) \n";
cin>> points;
for ( int p=0, p<points ; p++)
	{cout << "What is point number " << p+1 << ": \n";
	cin >> points[p+1]}

cout << "Your equation is: " << coeff[1]*pow(point[1],10) + coeff[2]*pow(point[2],9) + coeff[3]*pow(point[3],8) + coeff[4]*pow(point[4],7) + coeff[5]*pow(point[5],6) + coeff[6]*pow(point[6],5) + coeff[7]*pow(point[7],4) + coeff[8]*pow(point[8],3) + coeff[9]*pow(point[9],2) + coeff[10]*pow(point[10],1);



 
 system("pause");
}
Last edited on
.. can someone see if this is the right path?

I can see an array-out-of-bounds error off the bat:

for ( int n = 0 ; n<degree ; n++) { ... cin >> coeff[n+1]; }

If the user selects 10 for the highest degree, then when n == 9 in the loop, you will access coeff[n + 1] (coeff[10]) which would be one more than the maximum index (9).

If you have an array double coeff[10];, then the highest degree polynomial you could represent would be 9. coeff[0] would be the constant, coeff[1] would be the coefficient of x, ... , and coeff[9] would be the coefficient of x9.

//would i insert an array here?

I don't understand what you mean with the above question. Please elaborate. What you're doing (asking for each coefficient) sounds reasonable unless your assignment asked otherwise.

is there an easier way to do this?

I think the way you're doing it is the easiest way for obtaining the equation. You could get the coefficients from a file to save typing for when you're testing.

such as where the user can just straight out input the equation without saying its degree etc?

That would be harder, though easier from the user's perspective. For that you would have to do some string parsing. I would say only do that if your assignment requires it. I mean, it wouldn't be that bad as the parsing would be fairly straight forward: Assuming the user can only use 'x' for the variable, then you would split/tokenize the string using the symbols '-', '+', '*', '^', and 'x', where the numbers directly proceeding the '^'s would be the degrees, and the numbers (or negative signs) directly proceeding the 'x's would be the coefficients.

Also, to nit-pick: a polynomial's degree can only be an integer.
Last edited on
@shacktar thaanks! here, i updated my code more!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//graphing calculator. plot points derive antiderive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double coeff[11]={0};
int point[11]={0}; // i changed them to 11, so when n=10, it would fill up the 11th spot! is that right?
double degree, points;
cout << "Welcome to my graphing utility!\n";
cout << "Insert your equation and the points you want to input and I will do the rest.\n";
cout << "What is the highest degree of your equation: (up to 10) \n";
cin >> degree;

for ( int n = 0 ; n<degree ; n++)
	{cout << "what is your number " << n+1 << " coefficient: \n";
     cin >>coeff[n+1];}


cout << "How many points do you want to plot: (up to 10) \n";
cin>> points;
for ( int p=0; p<points ; p++)
	{cout << "What is point number " << p+1 << ": \n";
	cin >> point[p+1];}

cout << "Your equation is: " << coeff[1]*pow(point[1],10) + coeff[2]*pow(point[2],9) + coeff[3]*pow(point[3],8) + coeff[4]*pow(point[4],7) + coeff[5]*pow(point[5],6) + coeff[6]*pow(point[6],5) + coeff[7]*pow(point[7],4) + coeff[8]*pow(point[8],3) + coeff[9]*pow(point[9],2) + coeff[10]*pow(point[10],1);




 
 system("pause");
}


i messed up on this part cout << "Your equation is: " << coeff[1]*pow(point[1],10) + coeff[2]*pow(point[2],9) + coeff[3]*pow(point[3],8) + coeff[4]*pow(point[4],7) + coeff[5]*pow(point[5],6) + coeff[6]*pow(point[6],5) + coeff[7]*pow(point[7],4) + coeff[8]*pow(point[8],3) + coeff[9]*pow(point[9],2) + coeff[10]*pow(point[10],1); so i'm working on it!

edit: this is my newest function!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//graphing calculator. plot points derive antiderive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double coeff[11]={0};
int point[11]={0}; // i changed them to 11, so when n=10, it would fill up the 11th spot! is that right?
double degree, points;
cout << "Welcome to my graphing utility!\n";
cout << "Insert your equation and the points you want to input and I will do the rest.\n";
cout << "What is the highest degree of your equation: (up to 10) \n";
cin >> degree;

for ( int n = 0 ; n<degree ; n++)
	{cout << "what is your number " << n+1 << " coefficient: \n";
     cin >>coeff[n+1];}


cout << "How many points do you want to plot: (up to 10) \n";
cin>> points;
for ( int p=0; p<points ; p++)
	{cout << "What is point number " << p+1 << ": \n";
	cin >> point[p+1];}
cout << "Your equation is: " << (coeff[1]"x^10" + coeff[2]"x^9" + coeff[3]"x^8" + coeff[4]"x^7" + coeff[5]"x^6" + coeff[6]"x^5" + coeff[7]"x^4" + coeff[8]"x^3" + coeff[9]"x^2" + coeff[10]"x^1";

 cout << coeff[1]*pow(point[1],10) + coeff[2]*pow(point[1],9) + coeff[3]*pow(point[1],8) + coeff[4]*pow(point[1],7) + coeff[5]*pow(point[1],6) + coeff[6]*pow(point[1],5) + coeff[7]*pow(point[1],4) + coeff[8]*pow(point[1],3) + coeff[9]*pow(point[1],2) + coeff[10]*pow(point[1],1);
 cout << endl;

 cout << coeff[1]*pow(point[2],10) + coeff[2]*pow(point[2],9) + coeff[3]*pow(point[2],8) + coeff[4]*pow(point[2],7) + coeff[5]*pow(point[2],6) + coeff[6]*pow(point[2],5) + coeff[7]*pow(point[2],4) + coeff[8]*pow(point[2],3) + coeff[9]*pow(point[2],2) + coeff[10]*pow(point[2],1);
 cout << endl;
 
 cout << coeff[1]*pow(point[3],10) + coeff[2]*pow(point[3],9) + coeff[3]*pow(point[3],8) + coeff[4]*pow(point[3],7) + coeff[5]*pow(point[3],6) + coeff[6]*pow(point[3],5) + coeff[7]*pow(point[3],4) + coeff[8]*pow(point[3],3) + coeff[9]*pow(point[3],2) + coeff[10]*pow(point[3],1);
 cout << endl;
 
 cout << coeff[1]*pow(point[4],10) + coeff[2]*pow(point[4],9) + coeff[3]*pow(point[4],8) + coeff[4]*pow(point[4],7) + coeff[5]*pow(point[4],6) + coeff[6]*pow(point[4],5) + coeff[7]*pow(point[4],4) + coeff[8]*pow(point[4],3) + coeff[9]*pow(point[4],2) + coeff[10]*pow(point[4],1);
 cout << endl;
 
 cout << coeff[1]*pow(point[5],10) + coeff[2]*pow(point[5],9) + coeff[3]*pow(point[5],8) + coeff[4]*pow(point[5],7) + coeff[5]*pow(point[5],6) + coeff[6]*pow(point[5],5) + coeff[7]*pow(point[5],4) + coeff[8]*pow(point[5],3) + coeff[9]*pow(point[5],2) + coeff[10]*pow(point[5],1);
 cout << endl;
 
 cout << coeff[1]*pow(point[6],10) + coeff[2]*pow(point[6],9) + coeff[3]*pow(point[6],8) + coeff[4]*pow(point[6],7) + coeff[5]*pow(point[6],6) + coeff[6]*pow(point[6],5) + coeff[7]*pow(point[6],4) + coeff[8]*pow(point[6],3) + coeff[9]*pow(point[6],2) + coeff[10]*pow(point[6],1);
 cout << endl;
 
 cout << coeff[1]*pow(point[7],10) + coeff[2]*pow(point[7],9) + coeff[3]*pow(point[7],8) + coeff[4]*pow(point[7],7) + coeff[5]*pow(point[7],6) + coeff[6]*pow(point[7],5) + coeff[7]*pow(point[7],4) + coeff[8]*pow(point[7],3) + coeff[9]*pow(point[7],2) + coeff[10]*pow(point[7],1);
 cout << endl;
 
 cout << coeff[1]*pow(point[8],10) + coeff[2]*pow(point[8],9) + coeff[3]*pow(point[8],8) + coeff[4]*pow(point[8],7) + coeff[5]*pow(point[8],6) + coeff[6]*pow(point[8],5) + coeff[7]*pow(point[8],4) + coeff[8]*pow(point[8],3) + coeff[9]*pow(point[8],2) + coeff[10]*pow(point[8],1);
 cout << endl;
 
 cout << coeff[1]*pow(point[9],10) + coeff[2]*pow(point[9],9) + coeff[3]*pow(point[9],8) + coeff[4]*pow(point[9],7) + coeff[5]*pow(point[9],6) + coeff[6]*pow(point[9],5) + coeff[7]*pow(point[9],4) + coeff[8]*pow(point[9],3) + coeff[9]*pow(point[9],2) + coeff[10]*pow(point[9],1);
 cout << endl;
 
 cout << coeff[1]*pow(point[10],10) + coeff[2]*pow(point[10],9) + coeff[3]*pow(point[10],8) + coeff[4]*pow(point[10],7) + coeff[5]*pow(point[10],6) + coeff[6]*pow(point[10],5) + coeff[7]*pow(point[10],4) + coeff[8]*pow(point[10],3) + coeff[9]*pow(point[10],2) + coeff[10]*pow(point[10],1);
 cout << endl;
 


but it doesnt work :\ still trying to figure out the problem
Last edited on
// i changed them to 11, so when n=10, it would fill up the 11th spot! is that right?

Yes, though why aren't you filling in coeff[0]? That's where you should put in the constant term (though I guess since you're taking coeff[n] as the coefficient of x10-n, then coeff[0] would be the coefficient of x10).

but it doesnt work

How so?

Well, this won't compile:

cout << "Your equation is: " << (coeff[1]"x^10" + coeff[2]"x^9" + coeff[3]"x^8" ...

You'd want to do something like:

cout << "Your equation is: " << coeff[1] << "x^10 + " << coeff[2] << "x^9 + " << coeff[3] << ...
Last edited on

thaanks! realised my mistake on this part.
cout << "Your equation is: " << (coeff[1]"x^10" + coeff[2]"x^9" + coeff[3]"x^8" ...

but for this part, i get this error!
part:
1
2
 cout << coeff[1]*pow(point[1],10) + coeff[2]*pow(point[1],9) + coeff[3]*pow(point[1],8) + coeff[4]*pow(point[1],7) + coeff[5]*pow(point[1],6) + coeff[6]*pow(point[1],5) + coeff[7]*pow(point[1],4) + coeff[8]*pow(point[1],3) + coeff[9]*pow(point[1],2) + coeff[10]*pow(point[1],1);
 cout << endl;
// and anything else like that.
error:
1
2
3
4
5
1>Graph.cpp(27): error C2668: 'pow' : ambiguous call to overloaded function
1>          c:\Program Files\Microsoft Visual Studio 10.0\VC\include\math.h(583): could be 'long double pow(long double,int)'
1>          c:\Program Files\Microsoft Visual Studio 10.0\VC\include\math.h(535): or       'float pow(float,int)'
1>          c:\Program Files\Microsoft Visual Studio 10.0\VC\include\math.h(497): or       'double pow(double,int)'
1>          while trying to match the argument list '(int, int)'
That's saying you're passing two ints to pow but there's no such pow overload that takes two ints. Try casting the first parameter of pow to a float or double.
thank you! i fixed that problem :DD
but i've run into a new one :\ so, everything works spiffy! but when it does the data is stored in the arrays, it always takes up the first spot first; this becomes a problem when they choose something that isnt of the 10th degree!

say i input to the 6th degree,

it should say 0x^10....6x^6+5x^5...1x^1

but in reality, it says 6x^10+5x^9....+0x^3+0x^2+0x^1 D: do you have any suggestions/solutions shacktar?
Yeah, you could make it so it works like this:

coeff[1]*pow(point[1],1) + coeff[2]*pow(point[1],2) + coeff[3]*pow(point[1],3) + coeff[4]*pow(point[1],4) + ...

So, you would be asking the user for the coefficient of x, then the coefficient of x2, then the coefficient of x3 ...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//graphing calculator. plot points derive antiderive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double coeff[11]={0};
double point[11]={0}; // i changed them to 11, so when n=10, it would fill up the 11th spot! is that right?
double degree, points, con;
cout << "Welcome to my graphing utility!\n";
cout << "Insert your equation and the points you want to input and I will do the rest.\n";
cout << "What is the highest degree of your equation: (up to 10) \n";
cin >> degree;

for ( int n = 0 ; n<degree ; n++)
	{cout << "what is the coefficient of x^" << n+1  << endl;
     cin >>coeff[n+1];}
cout << "What is your constant: \n";
cin >> con;

cout << "How many points do you want to plot: (up to 10) \n";
cin>> points;
for ( int p=0; p<points ; p++)
	{cout << "What is point number " << p+1 << ": \n";
	cin >> point[p+1];}
cout << "Your equation is: " << coeff[10] << "x^10 + " << coeff[9] << "x^9 + " << coeff[8] << "x^8 + " << coeff[7] << "x^7 + " << coeff[6] << "x^6 + " << coeff[5] << "x^5 + " << coeff[4] << "x^4 + " << coeff[3] << "x^3 + " << coeff[2] << "x^2 + " << coeff[1] << "x^1 + " << con << endl;

 cout << coeff[10]*pow(point[1],10) + coeff[9]*pow(point[1],9) + coeff[8]*pow(point[1],8) + coeff[7]*pow(point[1],7) + coeff[6]*pow(point[1],6) + coeff[5]*pow(point[1],5) + coeff[4]*pow(point[1],4) + coeff[3]*pow(point[1],3) + coeff[2]*pow(point[1],2) + coeff[1]*pow(point[1],1) + con;
 cout << endl;

 cout << coeff[10]*pow(point[2],10) + coeff[9]*pow(point[2],9) + coeff[8]*pow(point[2],8) + coeff[7]*pow(point[2],7) + coeff[6]*pow(point[2],6) + coeff[5]*pow(point[2],5) + coeff[4]*pow(point[2],4) + coeff[3]*pow(point[2],3) + coeff[2]*pow(point[2],2) + coeff[1]*pow(point[2],1) + con;
 cout << endl;
 
 cout << coeff[10]*pow(point[3],10) + coeff[9]*pow(point[3],9) + coeff[8]*pow(point[3],8) + coeff[7]*pow(point[3],7) + coeff[6]*pow(point[3],6) + coeff[5]*pow(point[3],5) + coeff[4]*pow(point[3],4) + coeff[3]*pow(point[3],3) + coeff[2]*pow(point[3],2) + coeff[1]*pow(point[3],1) + con;
 cout << endl;
 
 cout << coeff[10]*pow(point[4],10) + coeff[9]*pow(point[4],9) + coeff[8]*pow(point[4],8) + coeff[7]*pow(point[4],7) + coeff[6]*pow(point[4],6) + coeff[5]*pow(point[4],5) + coeff[4]*pow(point[4],4) + coeff[3]*pow(point[4],3) + coeff[2]*pow(point[4],2) + coeff[1]*pow(point[4],1) + con;
 cout << endl;
 
 cout << coeff[10]*pow(point[5],10) + coeff[9]*pow(point[5],9) + coeff[8]*pow(point[5],8) + coeff[7]*pow(point[5],7) + coeff[6]*pow(point[5],6) + coeff[5]*pow(point[5],5) + coeff[4]*pow(point[5],4) + coeff[3]*pow(point[5],3) + coeff[2]*pow(point[5],2) + coeff[1]*pow(point[5],1) + con;
 cout << endl;
 
 cout << coeff[10]*pow(point[6],10) + coeff[9]*pow(point[6],9) + coeff[8]*pow(point[6],8) + coeff[7]*pow(point[6],7) + coeff[6]*pow(point[6],6) + coeff[5]*pow(point[6],5) + coeff[4]*pow(point[6],4) + coeff[3]*pow(point[6],3) + coeff[2]*pow(point[6],2) + coeff[1]*pow(point[6],1) + con;
 cout << endl;
 
 cout << coeff[10]*pow(point[7],10) + coeff[9]*pow(point[7],9) + coeff[8]*pow(point[7],8) + coeff[7]*pow(point[7],7) + coeff[6]*pow(point[7],6) + coeff[5]*pow(point[7],5) + coeff[4]*pow(point[7],4) + coeff[3]*pow(point[7],3) + coeff[2]*pow(point[7],2) + coeff[1]*pow(point[7],1) + con;
 cout << endl;
 
 cout << coeff[10]*pow(point[8],10) + coeff[9]*pow(point[8],9) + coeff[8]*pow(point[8],8) + coeff[7]*pow(point[8],7) + coeff[6]*pow(point[8],6) + coeff[5]*pow(point[8],5) + coeff[4]*pow(point[8],4) + coeff[3]*pow(point[8],3) + coeff[2]*pow(point[8],2) + coeff[1]*pow(point[8],1) + con;
 cout << endl;
 
 cout << coeff[10]*pow(point[9],10) + coeff[9]*pow(point[9],9) + coeff[8]*pow(point[9],8) + coeff[7]*pow(point[9],7) + coeff[6]*pow(point[9],6) + coeff[5]*pow(point[9],5) + coeff[4]*pow(point[9],4) + coeff[3]*pow(point[9],3) + coeff[2]*pow(point[9],2) + coeff[1]*pow(point[9],1) + con;
 cout << endl;
 
 cout << coeff[10]*pow(point[10],10) + coeff[9]*pow(point[10],9) + coeff[8]*pow(point[10],8) + coeff[7]*pow(point[10],7) + coeff[6]*pow(point[10],6) + coeff[5]*pow(point[10],5) + coeff[4]*pow(point[10],4) + coeff[3]*pow(point[10],3) + coeff[2]*pow(point[10],2) + coeff[1]*pow(point[10],1) + con;
 cout << endl;

 system("pause");
}


yes! thaanks! :DD my code is working spiffy now! but grr. at this part,

1
2
 cout << coeff[10]*pow(point[10],10) + coeff[9]*pow(point[10],9) + coeff[8]*pow(point[10],8) + coeff[7]*pow(point[10],7) + coeff[6]*pow(point[10],6) + coeff[5]*pow(point[10],5) + coeff[4]*pow(point[10],4) + coeff[3]*pow(point[10],3) + coeff[2]*pow(point[10],2) + coeff[1]*pow(point[10],1) + con;
 cout << endl;


it adds the coefficient despite the user only wanting two points! dkfhaliuf is there a way to be able to output my answers using a for loop?
Yes, in fact you can do the whole output using a nested for loop.

Hint: Notice how I can generalize your output to the following:

cout << coeff[10]*pow(point[p],10) + coeff[9]*pow(point[p],9) + coeff[8]*pow(point[p],8) + coeff[7]*pow(point[p],7) ...

where p is the index of the current point.

And like I said earlier, you can use coeff[0] to store the constant term.
Last edited on
AWWWWH SWEET! THANK YOU SO MUCH SHACKTAR! :DD IT WORKS LIKE A CHARM NOW!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//graphing calculator. plot points derive antiderive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double coeff[11]={0};
double point[11]={0}; 
double degree, points;
cout << "Welcome to my graphing utility!\n";
cout << "Insert your equation and the points you want to input and I will do the rest.\n";
cout << "What is the highest degree of your equation: (up to 10) \n";
cin >> degree;

for ( int n = 0 ; n<degree ; n++)
	{cout << "what is the coefficient of x^" << n+1  << endl;
     cin >>coeff[n+1];}
cout << "What is your constant: \n";
cin >> coeff[0];

cout << "How many points do you want to plot: (up to 10) \n";
cin>> points;
for ( int p=0; p<points ; p++)
	{cout << "What is point number " << p+1 << ": \n";
	cin >> point[p];}
cout << "Your equation is: " << coeff[10] << "x^10 + " << coeff[9] << "x^9 + " << coeff[8] << "x^8 + " << coeff[7] << "x^7 + " << coeff[6] << "x^6 + " << coeff[5] << "x^5 + " << coeff[4] << "x^4 + " << coeff[3] << "x^3 + " << coeff[2] << "x^2 + " << coeff[1] << "x^1 + " << coeff[0] << endl;

cout << "x | y \n";
 for ( int p ; p<points ; p++)
 {cout << point[p] << "|" ;
 cout << coeff[10]*pow(point[p],10) + coeff[9]*pow(point[p],9) + coeff[8]*pow(point[p],8) + coeff[7]*pow(point[p],7) + coeff[6]*pow(point[p],6) + coeff[5]*pow(point[p],5) + coeff[4]*pow(point[p],4) + coeff[3]*pow(point[p],3) + coeff[2]*pow(point[p],2) + coeff[1]*pow(point[p],1) + coeff[0];
 cout << endl;} 

 system("pause");
}


just gonna try to add a derivative section and etc now! thanks for all your help :DD
No problem!

But, you can clean it up even further :)

You can compute this sum:

coeff[10]*pow(point[p],10) + coeff[9]*pow(point[p],9) + coeff[8]*pow(point[p],8) + ... + coeff[0]

with a for loop as well. Note you can generalize the last term to be coeff[0]*pow(point[p],0) because (point[p])0 is 1 for all cases.

Your computation would look something like this:

1
2
3
4
5
6
7
8
9
10
11
for ( int p = 0 ; p<points ; p++)
{cout << point[p] << "|" ;

double sum = 0.0;
for( ... )
{
   sum += (general term of the sum);
}

cout << sum;
cout << endl;}
hiya shacktar! D: sorry for bothering you again, but i think i'm doing this wrong!
how does it look?
1
2
3
4
5
6
7
for ( int p ; p<points ; p++)
 {cout << point[p] << "|" ;
 //cout << coeff[10]*pow(point[p],10) + coeff[9]*pow(point[p],9) + coeff[8]*pow(point[p],8) + coeff[7]*pow(point[p],7) + coeff[6]*pow(point[p],6) + coeff[5]*pow(point[p],5) + coeff[4]*pow(point[p],4) + coeff[3]*pow(point[p],3) + coeff[2]*pow(point[p],2) + coeff[1]*pow(point[p],1) + coeff[0]*pow(point[p],1);
for( int n=10; ; n-- )
{
   sum += coeff[n]*pow([p],10);
}}
Three things:

1) You're missing the condition in the loop:

for( int n=10; /*condition involving n goes here*/; n-- )

So, n is going from 10 to zero. You need a condition there that's false when n is less than zero.

2) You have a 10 as the exponent here:

sum += coeff[n]*pow([p],10);

But if you look at the actual sum, it's not always 10 is it? You have it right having coeff[n]. Now, back to the actual sum: look at the index supplied to coeff and look at the exponent supplied to pow. Notice a similarity?

3) You haven't initialized sum. I have initialized sum in the code I posted earlier.

Hope this helps.
heyy! thaanks! i realised i initialized sum out of the for loop.. D:

would this part fix it?
1
2
3
4
5
6
7
8
9
cout << "How many points do you want to plot: (up to 10) \n";
cin>> points;
for ( int p ; p<points ; p++)
 {cout << point[p] << "|" ;
 //cout << coeff[10]*pow(point[p],10) + coeff[9]*pow(point[p],9) + coeff[8]*pow(point[p],8) + coeff[7]*pow(point[p],7) + coeff[6]*pow(point[p],6) + coeff[5]*pow(point[p],5) + coeff[4]*pow(point[p],4) + coeff[3]*pow(point[p],3) + coeff[2]*pow(point[p],2) + coeff[1]*pow(point[p],1) + coeff[0]*pow(point[p],1);
for( int n=10; n =0 ; n-- )
{ double sum = 0.0;
   sum += coeff[n]*pow([p],n);
}}


here's my full code! would you mind looking it over before it turn it in as my final project shacktar? :O


edit: THAT IS NOT my final project! i forgot to save the code on my USB so i will upload the real code at 9 pm when i get home! do you mind helping me check out line 28 though>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//graphing calculator. plot points derive  .
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double coeff[11]={0};
double point[11]={0}; 
double degree, points;
cout << "Welcome to my graphing utility!\n";
cout << "Insert your equation and the points you want to input and I will do the rest.\n";
cout << "What is the highest degree of your equation: (up to 10) \n";
cin >> degree;

for ( int n = 0 ; n<degree ; n++)
	{cout << "what is the coefficient of x^" << n+1  << endl;
     cin >>coeff[n+1];}
cout << "What is your constant: \n";
cin >> coeff[0];

cout << "How many points do you want to plot: (up to 10) \n";
cin>> points;
for ( int p ; p<points ; p++)
 {cout << point[p] << "|" ;
 //cout << coeff[10]*pow(point[p],10) + coeff[9]*pow(point[p],9) + coeff[8]*pow(point[p],8) + coeff[7]*pow(point[p],7) + coeff[6]*pow(point[p],6) + coeff[5]*pow(point[p],5) + coeff[4]*pow(point[p],4) + coeff[3]*pow(point[p],3) + coeff[2]*pow(point[p],2) + coeff[1]*pow(point[p],1) + coeff[0]*pow(point[p],1);
for( int n=10; n =0 ; n-- )
{ double sum = 0.0;
   sum += coeff[n]*pow([p],n);
}}

cout << "x | y \n";
 for ( int p ; p<points ; p++)
 {cout << "(" << point[p] << "," ;
 cout << coeff[10]*pow(point[p],10) + coeff[9]*pow(point[p],9) + coeff[8]*pow(point[p],8) + coeff[7]*pow(point[p],7) + coeff[6]*pow(point[p],6) + coeff[5]*pow(point[p],5) + coeff[4]*pow(point[p],4) + coeff[3]*pow(point[p],3) + coeff[2]*pow(point[p],2) + coeff[1]*pow(point[p],1) + coeff[0];
 cout << ")" << endl;} 

 system("pause");
}


i get this problem
1
2
3
1>graph.cpp(28): error C2059: syntax error : ')'
1>graph.cpp(28): error C2059: syntax error : ','
1>graph.cpp(28): error C2059: syntax error : ')'


at line 28 .. D:


Last edited on
If you're going to be handing it in soon then I would recommend just printing out the full sum like you're doing on line 34.

As for line 28, you put just [p] when you should've put point[p]. Other than that, that line looks good. However, for the rest of that loop:

1. You're initializing sum within the loop. This won't do much good because we want to print the sum after the loop. What you should do is initialize sum just within the for ( int p ; p<points ; p++) loop and print it after the loop where you determine the sum.

2. The "condition" you have for this loop for( int n=10; n =0 ; n-- ) will not work although it will compile. n = 0 is just an assignment, assigning zero to n. Since this is in the place where a condition is expected, it will return the value of n, which is zero, which would be false, so the loop would never execute. What you want is something in the form of a == b, or a > b, or a <= b, ..., i.e. a boolean expression, that will be false when n is less than zero.

But, like I've said if the deadline is looming it's probably better to just keep the working expression like you have on line 34. Depending on your teacher's personality, you could show a for loop commented out saying "I tried to print this out as a loop but I couldn't quite get it to work". It may help or it may not. That would be for you to judge.
:OO thanks! gahh! i'm rushing things. i need to take a breather . cant believe i missed the point [p] .. its due tuesday so no worries! :D thanks

and, would i do for ( int n=10; n =>0; n--) instead? since i want it to work for everything greater than or equal to zero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//graphing calculator. plot points derive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
int sub ( double coeff, int power) 
{ double der;
	der = power*coeff;
	return (der);
}

int main()
{
double coeff[11]={0};
double point[11]={0}; 
double degree, points;
char x;
cout << "Welcome to my graphing utility!\n";
cout << "Would you like to a. plot points, or b. derive the function? \n";
cin >> x;
if ( x== 'a')
{cout << "The derivative of \n";   
 cout << coeff[10] << "x^10 + " << coeff[9] << "x^9 + " << coeff[8] << "x^8 + " << coeff[7] << "x^7 + " << coeff[6] << "x^6 + " << coeff[5] << "x^5 + " << coeff[4] << "x^4 + " << coeff[3] << "x^3 + " << coeff[2] << "x^2 + " << coeff[1] << "x^1 + " << coeff[0];
 cout << " is: ";
 cout << sub(coeff[10],10) << "x^9 + " << sub(coeff[9],9) << "x^8 + " << sub(coeff[8],8) << "x^7 + " << sub(coeff[7],7) << "x^6 + " << sub(coeff[6],6) << "x^5 + " << sub(coeff[5],5) << "x^4 + " << sub(coeff[4],4) << "x^3 + " << sub(coeff[3],3) << "x^2 + " << sub(coeff[2],2) << "x + " << coeff[1] << " + 0\n"; }
	
else   
{cout << "Insert your equation and the points you want to input and I will do the rest.\n";
cout << "What is the highest degree of your equation: (up to 10) \n";
cin >> degree;

for ( int n = 0 ; n<degree ; n++)
	{cout << "what is the coefficient of x^" << n+1  << endl;
     cin >>coeff[n+1];}
cout << "What is your constant: \n";
cin >> coeff[0];

char z;
do {
cout << "How many points do you want to plot: (up to 10) \n";
cin>> points;
for ( int p=0; p<points ; p++)
	{cout << "What is point number " << p+1 << ": \n";
	cin >> point[p];}

cout << "Your equation is: " << coeff[10] << "x^10 + " << coeff[9] << "x^9 + " << coeff[8] << "x^8 + " << coeff[7] << "x^7 + " << coeff[6] << "x^6 + " << coeff[5] << "x^5 + " << coeff[4] << "x^4 + " << coeff[3] << "x^3 + " << coeff[2] << "x^2 + " << coeff[1] << "x^1 + " << coeff[0] << endl;


cout << "x | y \n";
 for ( int p ; p<points ; p++)
 {cout << "(" << point[p] << "," ;
 //cout << coeff[10]*pow(point[p],10) + coeff[9]*pow(point[p],9) + coeff[8]*pow(point[p],8) + coeff[7]*pow(point[p],7) + coeff[6]*pow(point[p],6) + coeff[5]*pow(point[p],5) + coeff[4]*pow(point[p],4) + coeff[3]*pow(point[p],3) + coeff[2]*pow(point[p],2) + coeff[1]*pow(point[p],1) + coeff[0]*pow(point[p],1);
 double sum=0.0;
 for( int n=10; n >= 0 ; n-- )
{sum += coeff[n]*pow(point[p],n);}
 cout << sum << ")" <<endl;}

cout << endl;
cout << "Do you want to add more points? <y/n> \n";
cin >> z;
} while (z != 'n');}


system("pause");}


 
 


this is my new code! thanks for all your help shacktar! everything's spiffy for the plotting points part! but i think there's a mistake with my if else :| if i comment out the if else part, then it works!
Pages: 12