As you can see I am new here. I found this site from googling a question about a getChoice() function , and I was able to learn how to implement the function in my project to fit my needs from the post.
(http://www.daniweb.com/forums/thread162456.html). Well anyways, I have more issues that are giving me real big headaches and I wonder if you can give me a little guidance/aid:
I believe I have the menu set up to how I need it, and I also believe that the "Enter polynomial" part (Case 1) is correct. What I am confused about is Case 2 and 3. I haven't started/don't know where to start with Case 3 (integrating then evaluating from inputted limits [a,b]). Some guidance on where to go with that would be awesome.
Edit: case 3 is all good except if the coefficient is 1. I don't want the program to print a 1 in front of the x.
NOTE: I would like to use the prototypes listed at the bottom of the code, I have included more info within the code.
#include<iostream>
#include<cmath>
using namespace std;
int getChoice();
int firstChoice();
int getN();
double f(double x, double coef[], int n);
double integral(double a, double b, double coef[], int n);
void print_polynomial(double coef[], int n);
int main (void) {
int userChoice;
int n;
int i;
double polynom,a,b;
double coef[10];
cout << "This program finds definite integral\nof polynomial on [a,b]\n\n";
cout << "0. Quit\n1. Enter Polynomial\n2. Print Polynomial\n3. Integrate\n\nEnter your choice: ";
userChoice = firstChoice();
do{
switch(userChoice)
{
case 0: //ends program
return 0;
case 1:
cout << "Enter degree of a polynom between 2 and 10 : ";
n=getN();
cout << "Enter space separated coefficients starting from highest degree\n";
for(i=n;i>=0;i--)
cin>>coef[i];
//prompts the user for the degree of the polynomial.
//Degrees may not be less than 2 or greater than 10
//for the purposes of this program. Continue prompting
//the user for the degree until he inputs a valid one.
//End result should be stored polynomial.
break;
case 2: print_polynomial(coef,n);
//prints the polynomial in standard form (highest degree first.)
//I want to be able to get rid of the "1" in 1*x (aka if coefficient is 1, don't print 1.)
break;
case 3: //prompts the user for the integration limits a and b
//then outputs the integral of the polynomial from a to b.
cout<<"Enter the integration limits:\n";
cin>>a>>b;
integral(a,b,coef,n);
break;
}
cout << "0. Quit\n1. Enter Polynomial\n2. Print Polynomial\n3. Integrate\n\nEnter your choice: ";
userChoice=getChoice();
}while(true);
}
//Prototypes
//This prototype is built so the value 2 or 3 cannot be chose before a polynomial is inputted
//This only works the very first run of the program
int firstChoice()
{
int choice;
cin >> choice;
if (choice == 2 || choice == 3)
{
cout << "Enter polynomial first\n0. Quit\n1. Enter Polynomial\n2. Print Polynomial\n3. Integrate\n\nEnter your choice: ";
cin >> choice;
}
if (choice != 0 && choice != 1 && choice != 2 && choice != 3)
{
cout << "'" << choice << "'is an invalid choice\nEnter your choice : ";
cin >> choice;
}
return choice;
}
//This is the prototype that is going to loop after the first run
int getChoice()
{
int choice;
cin >> choice;
while (choice != 0 && choice != 1 && choice != 2 && choice != 3)
{
cout << "'" << choice << "'is an invalid choice\nEnter your choice : ";
cin >> choice;
}
return choice;
}
int getN()
{
int degree;
cin >> degree;
while (degree < 2 || degree > 10)
{
cout << "Enter degree of a polynom between 2 and 10 : \n";
cin >> degree;
}
return degree;
}
double f(double x, double coef[], int n)
{ //Evaluates the polynomial specified by n and coef[] at x.
return 0;
}
double integral(double a, double b, double coef[], int n)
{ //Evaluates the integral of the polynomial specified by coef[] and n from a to b.
return 0;
}
void print_polynomial(double coef[], int n)
{ cout<<coef[n]<<"*x^"<<n;
for(int i=n-1;i>=2;i--)
{if(coef[i]>0)
cout<<"+";
if(coef[i]!=0)
if(coef[i]==1)
cout<<"x^"<<i;
else
cout<<coef[i]<<"*x^"<<i;
}
if(coef[1]>0)
cout<<"+";
if(coef[1]!=0)
if(coef[1]==1)
cout<<"x ";
else
cout<<coef[1]<<"*x ";
if(coef[0]>0)
cout<<"+";
cout<<coef[0];
cout<<endl<<endl;
//Do not print 1’s in front of coefficients except the first term.
}