Help with polynomial class arrays.

My C++ teacher for my past 2 classes has been horrible. She doesn't help when we need it and the tutors at my school don't either. I just need help getting started on this first project. I"ll seriously take any help I can get.

Problem: You will create the implementation of a Polynomial data type.
Specifics: I will provide the Polynomial class specification and a driver program to test your Polynomial
class. It is your job to fill in the methods/functions I have specified.

Data Representation:
Your Polynomial will be represented as a 21-term integer array. The index to the array
represents the degree. The value at a given index is the coefficient for that degree. Since there
are only 21 terms, your highest index will be 20 and your lowest 0. Therefore, your highest
exponent will be 20 and your lowest 0. Let’s assume we wanted to store the following
polynomial: x20 + 3x16 + 15x4 + 2x3 + 16x2 + 5. In the instance of such a Poly, our terms array
would look like the following:
examplePoly.terms

Degree 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Coeff 1 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 15 2 16 0 5


Poly.h file:
#ifndef POLY_H
#define POLY_H
#include <cmath>
#include <iostream>
using namespace std;
const unsigned int MAX = 21;
class Poly{
int terms[MAX];
public:
Poly();
double Eval(double x);
void Reset();
void operator+=(const Poly& p);
void Derivative();
friend istream& operator>>(istream& is, Poly& p);
friend ostream& operator<<(ostream& os, const Poly& p);
friend Poly operator+(const Poly& p1, const Poly& p2);
friend Poly operator*(const Poly& p1, const Poly& p2);
};
#endif

Main Driver provided:

// Feel free to change this main program as you wish.
// You may find it easier to use to read polynomials
// from a file rather than from standard input
#include "Poly.h"
#include <iostream>
#include <fstream>
using namespace std;
void testConstructor(ostream&);
void testInput(istream&, ostream&);
void testReset(istream&, ostream&);
void testEvaluate(istream&, ostream&);
void testAdd(istream&, ostream&);
void testMult(istream&, ostream&);
void testPlusEqual(istream&, ostream&);
void testDerivative(istream&, ostream&);
int getChoice();
int main(){
ifstream input("input.txt");
ifstream reset("reset.txt");
ifstream eval("eval.txt");
ifstream add("add.txt");
ifstream mult("mult.txt");
ifstream plusE("pluse.txt");
ifstream deriv("deriv.txt");
int choice = getChoice();
while(choice){
system("cls");
switch (choice){
case 1: testConstructor(cout);break;
case 2: testInput(input,cout); break;
case 3: cout << "Tested in other methods" ; break;
case 4: testReset(reset,cout); break;
case 5: testEvaluate(eval,cout); break;
case 6: testAdd(add,cout); break;
case 7: testMult(mult,cout); break;
case 8: testPlusEqual(plusE,cout); break;
case 9: testDerivative(deriv,cout); break;
default: cout << "invalid selection" << endl;
}
cout << "Press Enter to Continue" << endl;
cin.get();cin.get();
system("cls");
choice = getChoice();
}
return 0;
}
void testConstructor(ostream&os){
os<< "TESTING CONSTRUCTOR" << endl;
os<< "EXPECT:\t" << "<>" << endl;
Poly p;
os << "ACTUAL:\t" << p << endl;
}
void testInput(istream& is, ostream&os){
os << "TESTING EXTRACTION OPERATOR" << endl;
os << "Enter Polynomial in specified format" << endl;
Poly p;
is >> p; os<< endl;
os<< "ACTUAL:\t" << p << endl;
}
void testReset(istream& is, ostream&os){
os<< "TESTING RESET, MUST HAVE INPUT" << endl;
os<< "Enter a Polynomial" << endl;
Poly p;
is >> p;
os<< "BEFORE RESET:\t" << p << endl;
p.Reset();
os<< "AFTER RESET:\t" << p << endl;
}
void testEvaluate(istream& is, ostream&os){
os<< "TESTING EVALUATE" << endl;
os << "Enter a Polynomial" << endl;
Poly p;
is >> p;
os<< "Enter the value for x" << endl;
int x;
is>> x;
os<< "Polynomial:\t" << p << endl;
os<< "evaluated at\t" << x << endl;
os<< "is \t " << p.Eval(x) << endl;
}
void testAdd(istream& is, ostream&os){
os<< "TESTING ADD" << endl;
Poly p1, p2;
os<< "Enter Left Operand" << endl;
is>> p1;
os<< "Enter Right Operand " << endl;
is>> p2;
os<< p1 << " + " << p2 << "=" << endl;
os<< p1 + p2 << endl;
}
void testMult(istream& is, ostream&os){
os<< "TESTING MULTIPLY" << endl;
Poly p1, p2;
os<< "Enter Left Operand" << endl;
is>> p1;
os<< "Enter Right Operand " << endl;
is>> p2;
os<< p1 << " * " << p2 << "=" << endl;
os<< p1 * p2 << endl;
}
void testPlusEqual(istream& is, ostream&os){
os<< "TESTING PLUS EQUAL" << endl;
Poly p1, p2;
os<< "Enter Left Operand" << endl;
is>> p1;
os<< "Enter Right Operand " << endl;
is>> p2;
os<< p1 << " += " << p2 << "=" << endl;
p1+=p2;
os<< p1 << endl;
}
void testDerivative(istream& is, ostream&os){
os<< "TESTING DERIVATIVE" << endl;
Poly p1;
os<< "Enter Polynomial" << endl;
is>> p1;
os<< "The Derivative of " << p1 << "is" << endl;
p1.Derivative();
os<< p1 << endl;
}
int getChoice(){
cout << "Select one" << endl;
cout << "1. Test Constructor" << endl;
cout << "2. Test Input " << endl;
cout << "3. Test Output" << endl;
cout << "4. Test Reset " << endl;
cout << "5. Test Evaluate " << endl;
cout << "6. TestAdd" << endl;
cout << "7. TestMult" << endl;
cout << "8. TestPlusEqual" << endl;
cout << "9. TestDerivative" << endl;
cout << "0. Quit" << endl;
int c;
cin >> c;
return c;
}

This seems pretty straight forward.
You just have to fill in the functions in class Poly.
What do you have so far?

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

welcome!
please learn about code tags, the <> on the side editor, or code and /code in [] brackets to block it off.

Also, asking a specific question will get you much farther than 'help me'.

a poly is easier to represent in reverse: in math you put the high exponent first, x^2+3x+5 but in code, array[0] = 5; array[1] = 3; array[2] = 1 ... and it may not be obvious but the array index is the power of x (anything to 0 is 1, so the first term there is 5*x^0 which is 5).

note that missing terms just get a zero. so x^2 is p[0] = 0, p[1] = 0], and p[2] = 1 right?

a pointer would near give you the derivative. not exactly, but..
int poly[20];
int *deriv = &poly[1]; //deriv is now shifted the above power scheme.. poly[1] is now deriv[0] so all your exponents dropped a power, so you just need a simple multiply loop to finish it up. This is destructive, so if you need to keep the original poly as well, you can't get as much freebie from this approach. If you discard the poly or work with a copy, its useful, just remember its size is -1 too so don't go out of bounds.

other advice... knock out the simple ones first.
using the scheme to store it that I gave you, addition should be very simple, for example!
Did it ASK for integers? Can you not have 3.14R^2 in this world?
Last edited on
Topic archived. No new replies allowed.