advice please!!! :D

hi i am having some trouble with my program. i am just very stuck. so far what is happening is i have a txt file with a random polynomial in it (for example: 2x^3+4x^-5+3x^7) my program reads in the polynomial stores it into an array and sorts the polynomial in ascending order from smallest exponent to largest exponent. then it outputs the sorted polynomial into the output file. now what i i need to do next is what i am having trouble with is next i need to ask the user for what number they would like as the value x and then it will calculat the polynomial and output the answer to the user.

im going to write a new function called calcPoly and i think the actual calculating would look like:
polynomial.coefficient*(pow(x,polynomial.exponent))
that would take care of the first term in the polynomial but would i have to put it into a loop? to calculate each term of it and then add it altogether.

so i have an idea on what im doing im just stuck.
im sorry if this is confusing and long so 100 points to anyone who actually reads all of this and has any input, suggestions, or advice.
any help is really appricieated!! :D thanks in advance!!

#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;

struct term
{
int coefficient;
int exponent;
};
void setPoly(ifstream &InputFile, term polynomial[], int &size);
void setTerm(ifstream &InputFile, term polynomial[], term &newTerm);
void InsertSort(ifstream &InputFile, term polynomial[],term newTerm, int &size);
int FindPosition(term newTerm, term polynomial[], int size);
void MoveData(int position, term polynomial[], int &size);
void OutputArray(ofstream &OutputFile, term polynomial[], int size);

int main()
{

ifstream InputFile;
ofstream OutputFile;

term polynomial[100];
int size=0;

InputFile.open("polynomial.txt");
if (!InputFile)
{
cout << "Error opening inputfile!" << endl;
exit(1);
}

OutputFile.open("results.txt");

setPoly(InputFile, polynomial, size);
OutputArray(OutputFile, polynomial, size);

InputFile.close();
OutputFile.close();

return 0;
}

void setPoly(ifstream &InputFile, term polynomial[], int &size)
{
term newTerm;
int next;
char nextchar;
char blank = ' ';
bool done = false;

size = 0;
while(!InputFile.eof() && next <=100 && !done)
{
setTerm(InputFile, polynomial, newTerm);

if(InputFile)
{
InsertSort(InputFile, polynomial, newTerm, size);
InputFile.get(nextchar);
while(nextchar == blank)
{
InputFile.get(nextchar);
}
done = nextchar == '\n';
}
else
{
done = true;
}
}
}

void setTerm(ifstream &InputFile, term polynomial[], term &newTerm)
{
char c;
InputFile >> newTerm.coefficient;
InputFile.get(c);
InputFile.get(c);
InputFile >> newTerm.exponent;
}


void InsertSort(ifstream &InputFile, term polynomial[], term newTerm, int &size)
{

int position;

if (size == 0)
{
polynomial[0]=newTerm;
size++;
}
else
{
position = FindPosition(newTerm, polynomial, size);
MoveData(position, polynomial, size);
polynomial[position] = newTerm;
size ++;
}

}

int FindPosition(term newTerm, term polynomial[], int size)
{
int index;
index = 0;
while (index < size)
{
if (newTerm.exponent > polynomial[index].exponent)
{
index++;
}
else
return(index);
}
return(size);
}

void MoveData(int position, term polynomial[], int &size)
{
int index;
for (index=size+1; index>position; index--)
{
polynomial[index] = polynomial[index-1];
}
}

void OutputArray(ofstream& OutputFile, term polynomial[], int size)
{
int i;
char plus = '+';
for(i=0; i<size; i++)
{
if(i!=size-1)
{
OutputFile << polynomial[i].coefficient << "x^" << polynomial[i].exponent;
OutputFile.put(plus);
}
else
{
OutputFile << polynomial[i].coefficient << "x^" << polynomial[i].exponent << endl;
}
}
}
You're correct.
1
2
double result = 0;
for(int i = 0; i < size; i++) result += calcTerm(polynomial[i], x);
where calcTerm is coefficient*pow(x, exponent).
Is there something else wrong?
thank you so much for your reply! i really appriciate it!
i was working with it and was having trouble with the pow so i changed it to do it without using pow.
this is what i added:
{
int x;
int result = 0;
int xpow = 1;

cout<< "What number would you like to be inserted as x?" << endl;
cin >> x;

for(int i=0; i<size; i++)
{
result += polynomial[i].coefficient * xpow;
xpow *= x;
}

cout << result << endl;
}

and this compiles but when i type in a number it doesnt give me the right answer
i think it might have something to do with negative in it? or is that wrong?
im not sure?
oh sorry i was being stupid

i set xpow = polynomial[i].exponent and that equal to 1

still not right answer though
What kind of trouble could you be having with pow?
Anyway, to calculate c*x^e you have to multiply c by x e times
1
2
3
int c, e, x;
int result = c;
for(int i = 0; i < e; i++) result *= x;

Now you have to repeat this for every term and add up all results.
Topic archived. No new replies allowed.