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 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];
}
}
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;
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?