I have to be able to read text from a file then manipulate it to find the average value of an integral over a given bounds [a,b] from an input file, then export it to another file. I do not know how to store the information to process it. Say this is the input file input.txt (each line is a different polynomial):
3 1.3 0.9 4.7 2 -5.4 5
2 5 6.3 1 0.4 10.7
1 1 0 -1 2
I would want to be able to store the bold numbers as the degree of the polynomial. The underlined numbers are the values of a,b respectively. the rest of the numbers are the coefficients.
ex) line 1 would be 3rd degree polynomial 1.3x^2+0.9x+2 integrated over [-5.4,5]
I realize if I store each line as an array of coef[], I can set
a=coef[1]
b=coef[0]
but I don't know how I would find the degree (bold number, first number in each line) to set it equal so that I can evaluate the polynomial to integrate etc. since the lines each have different amounts of numbers. If i can get these each set to a separate memory, then I know how to evaluate the integral and print like I need it.
Here's my code so you may see what I am doing (pardon the redundancy). Please let me know if I am unclear:
#include<iostream>
#include<cmath>
#include<cctype>
using namespace std;
char getChoice();
char 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) {
char userChoice;
int n,x,i;
double a,b;
double coef[10];
cout << "\nThis program finds definite integral\nof polynomial on [a,b]\n\n";
cout << "0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\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];
break;
case '2': print_polynomial(coef,n);
cout<<endl;
break;
case '3': integral(a, b, coef, n);
cout<<endl;
break;
case '4'://This case is the area in which the input file would be read,
//processed, and outputted.
cout<<endl;
break;
}
cout << "0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
userChoice=getChoice();
}while(true);
}
//Prototypes
char firstChoice()
{
char choice;
cin >> choice;
while(isalpha(choice))
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}
if (choice == '0' || choice == '1' || choice == '4')
{
return choice;
}
while(choice!= '0' && choice != '1' && choice != '4')
{
if (choice == '2' || choice == '3')
{
cout << "Enter the polynomial first\n0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
cin >> choice;
}
if (choice != '0' && choice != '1' && choice != '2' && choice != '3' && choice != '4')
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}
}
return choice;
}
char getChoice()
{
char choice;
cin >> choice;
while(isalpha(choice))
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}
while (choice != '0' && choice != '1' && choice != '2' && choice != '3' && choice != '4')
{
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 : ";
cin >> degree;
}
return degree;
}
double f(double x, double coef[], int n)
{
double hold;
double sum=0;
cout << "x : ";
cin >> x;
for(int i=n;i>=1;i--)
{
hold=sum;
sum=coef[i]*pow(x,i);
sum+=hold;
}
sum=sum+coef[0];
cout << sum;
}
double integral(double a, double b, double coef[], int n)
{
double sum1=0;
double sum2=0;
double finalSum;
double hold1,hold2;
cout << "Enter integration limits a b : ";
cin>>a>>b;
for(int i=n;i>0;i--)
{
hold1=sum1;
sum1=coef[i]*(pow(a,i+1)/(i+1));
sum1=sum1+hold1;
}
sum1=sum1+coef[0]*a;
for(int i=n;i>0;i--)
{
hold2=sum2;
sum2=coef[i]*(pow(b,i+1)/(i+1));
sum2=sum2+hold2;
}
sum2=sum2+coef[0]*b;
finalSum=sum2-sum1;
cout << "Integral of f(x) equals to " << finalSum;
}
void print_polynomial(double coef[], int n)
{
if(coef[n]==1) cout << "x^"<<n;
if(coef[n]==-1) cout<< "-x^"<<n;
if(coef[n]>0 && coef[n]!=1) cout << coef[n]<<"*x^"<<n;
if(coef[n]<0 && coef[n]!=-1) cout << coef[n]<<"*x^"<<n;
for(int i=n-1;i>=2;i--)
{
if(coef[i]>0) cout<<"+";
if(coef[i]==1) cout<<"x^"<<i;
if(coef[i]==-1) cout<<"-x^"<<i;
if(coef[i]<0 && coef[i]!=-1) cout<<coef[i]<<"*x^"<<i;
if(coef[i]>0 && coef[i] !=1) cout<<coef[i]<<"*x^"<<i;
}
if(coef[1]==1) cout <<"+x";
if(coef[1]==-1) cout<<"-x";
if(coef[1]>0 && coef[1] !=1) cout<<"+"<<coef[1]<<"*x";
if(coef[1]<0 && coef[1] !=-1) cout<<coef[1]<<"*x";
if(coef[0]>0) cout<<"+"<<coef[0];
if(coef[0]<0) cout<<coef[0];
}