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

Pages: 12
The only problem I can see with the if-else is a reversal in logic. You say the options are a for plotting points and b for taking the derivative yet when the user selects 'a' it does the derivative...
ahh! i realized my mistake there! and also, when if the user chooses to derive, the program doesnt prompt the user to insert their equation first! fixed that.

i have another question; after choosing a and deriving, how would i ask them if they wanted to quit or continue on to plotting points? the if else statement ends the program after one thing. would i do a do while instead of if else?
do not mind this comment! i'm putting my code up so i can see it when i'm on my desktop! thanks for all your help shacktar! i hope my trivial blunders arent annoying you!

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
68
69
70
71
72
73
74
75
76
//graphing calculator. plot points derive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
double 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. derive the function, or b. plot points? \n";
cin >> x;
if ( x== 'a')
{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 << "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: \n";
 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");}


 
 
i hope my trivial blunders arent annoying you!

It's no problem at all!

would i do a do while instead of if else?

You wouldn't do the loop instead of the if-else but rather with the if-else inside the loop.

It would look something like this (notice I added a third option to the main menu):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
do
{
   cout << "Welcome to my graphing utility!\n";
   cout << "Would you like to a. derive the function, b. plot points, or c. quit? \n";
   cin >> x;
   if ( x== 'a')
   {
      //do the derivative
   }
   else if (x == 'b')
   {
      //do the plotting
   }
} while(/*user has not quit*/);


Also, consider putting this code in a function because it's repeated twice:

1
2
3
4
5
6
7
8
9
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];


Another thing is I consider this function to be extraneous (and poorly named):

1
2
3
4
5
double sub ( double coeff, int power) 
{ double der;
	der = power*coeff;
	return (der);
}


It's called sub yet all it does is multiply a double by an int. Consider removing this function and just using the multiplication in its place.

And all these places are more opportunities to use a for loop instead of hard-coding the whole output:

1
2
3
4
5
6
7
8
9
10
1)
cout << "The derivative of \n";   
 cout << coeff[10] << "x^10 + " << coeff[9] << "x^9 + " << coeff[8] << "x^8 + " << coeff[7] << "x^7 + " ...

2)
cout << " is: \n";
cout << sub(coeff[10],10) << "x^9 + " << sub(coeff[9],9) << "x^8 + " << sub(coeff[8],8) << "x^7 + " << sub(coeff[7],7) ...

3)
cout << "Your equation is: " << coeff[10] << "x^10 + " << coeff[9] << "x^9 + " << coeff[8] << "x^8 + " << coeff[7] ...


Finally, you should have return 0; at the end of main (indicating successful execution). And, you should look into alternatives to system("pause");

http://www.cplusplus.com/forum/articles/11153/
Last edited on
:OO thanks for all the tips! i am unclear on how to make a for loop into a function! do you mind giving me hints on that? but, here's my new code! and i've turned everything into for loops! i hope they're right! also added a do whole to the whole thing :D

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//graphing calculator. plot points derive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;
double 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,m;
do 
{cout << "Welcome to my graphing utility!\n";
cout << "Would you like to a. derive the function, or b. plot points? \n";
cin >> x;
if ( x== 'a')
{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 << "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];
for ( int k = 10; k >= 0; k--)
{cout << coeff[k] << "x^k + ";
cout << " is: \n";
// 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";
for ( int t = 10; t >= 0; t--)
{cout << sub(coeff[t],t) << "x^" << t-1 << "+ ";

 cout << "Do you want to continue to plot points? <y/n> \n";
 cin >> m; }
	
else if (x== 'b')
{{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 << "Your equation is: ";
for ( int k = 10; k >= 0; k--)
{cout << coeff[k] << "x^k + ";


cout << "x | y \n";
 for ( int p ; p<points ; p++)
 {cout << "(" << point[p] << "," ;
 
 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');}
}while (m != 'n'); }

return 0;}





 
 
i am unclear on how to make a for loop into a function! do you mind giving me hints on that?

A function doesn't have to be one operation. Essentially you would put the code in question:

1
2
3
4
5
6
7
8
9
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];


passing in coeff[] and passing in degree by reference. If you haven't learned that yet then I would recommend just leaving that code as is.

i've turned everything into for loops! i hope they're right!

You shouldn't just hope, you should run it and test. That said, your code doesn't even compile at this point. Some things I've noticed:

1
2
3
4
for ( int k = 10; k >= 0; k--)
{cout << coeff[k] << "x^k + "; // <- here you're missing a terminating bracket and it will show "k" for all exponents instead of the actual number. It will also show a '+' after the constant term.
cout << " is: \n";
...


1
2
3
for ( int t = 10; t >= 0; t--)
{cout << sub(coeff[t],t) << "x^" << t-1 << "+ "; // <- you're missing a terminating bracket here and your final term will have an x-1 and it will show a '+' after the last term.
...


1
2
3
4
cout << "Your equation is: ";
for ( int k = 10; k >= 0; k--)
{cout << coeff[k] << "x^k + "; // <- you're missing a terminating bracket here and it will show a '+' after the constant term
...


Also, 'm' is only given a value if the user chooses option 'a'. I would recommend doing it the way I've shown, i.e. give the user a third menu option c. to quit. That way you don't need to have this other user option variable 'm' floating around.
Last edited on
heyy! sorry for the shoddy job earlier! :| was trying to get everything ready to go to sacromento for mothers day!

i've taken care of the mistakes i made earlier!
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//graphing calculator. plot points derive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;

double 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. derive the function, or b. plot points or c. quit? \n";
cin >> x;
if ( x== 'a')
{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 << "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];
for ( int k = 10; k >= 1; k--)
{cout << coeff[k] << "x^" << k << " + ";}
cout << coeff[0] << " is: \n";
// 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";
for ( int t = 10; t >= 2; t--)
{cout << sub(coeff[t],t) << "x^" << t-1 << " + ";} 
cout  << coeff[2] << endl;}
	
else if (x== 'b')
{{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 << "Your equation is: ";
for ( int k = 10; k >= 1; k--)
{cout << coeff[k] << "x^" << k << " + ";}
cout << coeff[0] << " is: \n";

cout << "x | y \n";
 for ( int p ; p<points ; p++)
 {cout << "(" << point[p] << "," ;
 
 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');}}

else (x=='c');
{system("pause");}
}




is my new code! how does it look? :D i have a problem with the do while look where i ask if the user wants to input more points though! if i press y, it asks me for points, but doesnt actually do the calculation!

and .. do you know how i would make a, b, and c like a menu thing? at the end of each job, i want to ask them to choose again and they would be able to choose a b or c.. ._. am i confusing you? sorry!
if i press y, it asks me for points, but doesnt actually do the calculation!

I guess it could be because a newline is getting stuck in the stream. Try putting cin.sync(); after cin >> z; to be sure. It could also be because you're not initializing p here:

1
2
3
4
cout << "x | y \n";
 for ( int p ; p<points ; p++) //<- p is not being initialized here
 {cout << "(" << point[p] << "," ;
...


do you know how i would make a, b, and c like a menu thing? at the end of each job, i want to ask them to choose again and they would be able to choose a b or c

Do you mean so that the user can only type a, b, or c? You can't control what they type but if you want them to only be able to select, a, b, or c you would have to ask them in a loop, like this for example:

1
2
3
4
5
6
7
8
char x;
cout << "Welcome to my graphing utility!\n";

do
{
   cout << "Would you like to a. derive the function, or b. plot points or c. quit? \n";
   cin >> x;
} while ( (x != 'a') && (x != 'b') && (x != 'c') );


If that's not what you meant, then you should be placing the whole thing in a loop like I showed earlier:

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
char x;
cout << "Welcome to my graphing utility!\n";

do
{

   //optionally, here ask the user in a loop until they select a valid option

   //do
   //{
   cout << "Would you like to a. derive the function, or b. plot points or c. quit? \n";
   cin >> x;
   //} while ( (x != 'a') && (x != 'b') && (x != 'c') );

   if ( x== 'a')
   {
      //do derivation
      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;
      ...
   }
   else if(x == 'b')
   {
      //plot points
      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;
      ...
   }
} while (x !='c');
Last edited on
closed account (4z0M4iN6)
I don't know, wether this would be helpful.
But you could have a look at it:

http://cplusplus.com/forum/beginner/70858/
heyy! THANKS SHACKTAR FOR ALL YOUR HELP! D: i cant believe i missed that again! gahh. my program works like a spiffy now! :DD

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//graphing calculator. plot points derive and asymptotes.
#include <iostream>
#include <math.h>
using namespace std;

double 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";

do{
cout << "Would you like to a. derive the function, or b. plot points or c. quit? \n";
cin >> x;
cout << endl;

if ( x== 'a')
{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 << "The derivative of \n";   
 
for ( int k = 10; k >= 1; k--)
{cout << coeff[k] << "x^" << k << " + ";}
cout << coeff[0] << " is: \n";

for ( int t = 10; t >= 2; t--)
{cout << sub(coeff[t],t) << "x^" << t-1 << " + ";} 
cout  << coeff[2] << endl;}
	
else if (x== 'b')
{{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 << endl;

cout << "Your equation is: ";
for ( int k = 10; k >= 1; k--)
{cout << coeff[k] << "x^" << k << " + ";}
cout << coeff[0] << endl;

cout << "x | y \n";
 for ( int p=0 ; p<points ; p++)
 {cout << "(" << point[p] << "," ;
 
 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');}}
cout << endl;
}while (x != 'c');


{system("pause");}
}




here's how it looks! wootwoot!

Glad I could help!
Topic archived. No new replies allowed.
Pages: 12