Question:
So basically using the class below I want to create polynomials as can be seen in the example:

Command: s
Set coefficient. Give A n c: x 3 33.33
Command: o
Output poly. Give A: x
Polynomial x: 0x^0 + 0x^1 + 0x^2 + 33.33x^3
Command: s
Set coefficient. Give A n c: x 2 2.222
Command: o
Output poly. Give A: x
Polynomial x: 0x^0 + 0x^1 + 2.222x^2 + 33.33x^3

Class:

#include <vector>

class poly
{
private:
    std::vector<double> coefficientVector;
		// Constructed implicitly to initially be null.
    int SIZE() const;  // Shorter way to write size() of a poly
private:
    bool wellFormed () const;  // Null or last coefficient is non-zero
    void trimZeros ();  // Remove high-end zero coefficients.
public:
    void nullify (); // Create 'null' poly with no coefficients
    poly ();  // Not needed when based on 'vectors'
    bool isNull () const;
    int degree() const;

public:
    void setCoefficient (const int exponent, const double coefficient);
    double getCoefficient (int exponent) const;
    void output() const;
    poly& operator =(const poly& rtSide);
    poly operator +(const poly& rtSide) const;
    poly operator -(const poly& rtSide) const;
    poly operator *(const poly& rtSide) const;

    double evaluate (double x) const;  // Evaluate the poly at point x
    // Boolean and, or, not.  False = null, True otherwise
    poly operator &(const poly& rtSide) const;
    poly operator |(const poly& rtSide) const;
    poly operator ~() const;
};  // End of poly class
int main(){
    cout << "Give A n c: ";
    int exponent;
    double coefficient;
    char name;
    cin >> name >> exponent >> coefficient ;
    poly* name1 = new poly[26][26];
    int count = 0;
    name1[0][0] = name;
    name1[0][1] = count;
    if (name1[0][1] == NULL){
    name1[2 + (exponent/1) ][0] = exponent;
    name2[2 + (exponent/1)][1] = coefficient;
    cout << name1[i];
    return 0;}

The problem is I am not sure how to implement the setcoefficient function, this was just a trial.

Recommended Answers

All 15 Replies

Not sure that you know what you are doing here. The main part of the program will not work at all. Simply delete it from line 2 to line 14. It is utter junk.

First off: Provide a default constructor/copy constructor / assignment operator and deletion operator. I know that the deletion operator isn't going to have anything in it but but you will most likely want something in it later.

Next: Get that working in a real compilable/runable program. Create a polynomial and copy it to anther. I know it does nothing yet but it really really helps to do it in small steps.

Next: Think about what is meant by adding one polynomial coefficient e.g. 6x^8, do you want to expand your vector to size 9, and have 8 zero coefficients, or do you want to keep track of the powers. The write a test program that works with constants in the main program. At that point you will be beginning to get a feel for how it works.

Next do : output/operator+ and operator-. They are simpler than the rest.

Then tackle the more difficult * operator. That is difficult because if you multiply say (x^3+x^2) by (x^2+2) you get a polynomial with powers 5,4,3 and 2. The increase in the number of terms is a little tricky.

Ok so honestly I don't understand what you said, my confusion is how to create the polynomial i.e the setcoefficient function but I tried something.

void poly::setCoefficient (const int exponent, const double coefficient){
     coefficientVector(exponent);
     coefficientVector[exponent] = coefficient;
     SIZE = exponent;
     }
     
poly poly:operator +(const poly& rtSide) const{
     poly result;
     for( int i = 0; i <= SIZE ; i++){
          result( coefficientVector[i] + rtSide[i]);
          }
     return result;}

Let us look at that untested code! -- Please get something to build and test otherwise you will get nowhere. -- The smallest possible successful program is were to start.

Anyway: Consider what your code does:

First you pass two values, the index (exponent) and the coefficient value. Good.

Second, you try to resize a vector to size exponent using the construct. That can only be done as a constructor (e.g. in the poly constructor) so doesn't work, you wanted coefficientVector.resize(exponent); .

Next having reserved exponent spaces you promptly set the exponent+1 space to something... remember vectors and arrays in c++ start from 0.

Then you set the size to exponent.

All of that logic is wrong as well, as shown by the example :

// set poly x^3-2x 
poly A;
A.setCoefficient(3,1.0);
A.setCoefficient(1,-2.0);
// Oh dear A has size 1
std::cout<<A.getSize()<<std::endl;

As you see you should only resize if the exponent is bigger than size.

So
(a) compile your code.
(b) write very small parts before compiling -- it makes it easier.
(c) check your logic on paper -- it really helps
(d) try to get something to work, the minimal program it doesn't need to do much
-- don't be afraid to use dummy functions e.g.

poly poly::opertaor+(const poly& rtSide) const
{ 
    // dummy return until I get round to writing this function:
   std::cerr<<"Warning : incomplete function"<<std::endl;
   return poly(*this);
}

(e) slowly add features keeping the program working.

So I tried to get this running but kept getting SIZE is private or poly::coefficientVector is private

#include "poly.h"
#include "help.h"
#include <iostream>

using namespace std;

void poly::setCoefficient (const int exponent, const double coefficient){
     coefficientVector[exponent] = coefficient;
   //  SIZE() = exponent;
     }
     
poly poly::operator +(const poly &rtSide) const{
     poly result;
 //    if ( SIZE <= rtSide.SIZE){
   //       poly result(SIZE);
     //     result.SIZE = SIZE;}
     //else{
     //     poly result(rtSide.SIZE);
      //    result.SIZE = rtSide.SIZE;}     
     for( int i = 0; i <= result.SIZE() ; i++){
          result.coefficientVector[i] =  coefficientVector[i] + rtSide.coefficientVector[i];
          }
     return result;}
 
int main(){
    poly A;
    A.setCoefficient(3, 3.33);
    for( int i = 0; i <= 26 ; i++){
          cout << i << "\t" << A.coefficientVector[i];
          }
    return 0;}

Please post a whole working program. If I can't compile it and run it, it makes it difficult to help.

The operator+ part, it doesn't work and we can fix that after we fix the setCoefficient part, the constructors, assignment operator and the output method.

The set coeff might work as it is for some inputs, but is missing a test of size and the possibility to resize the vector.

Note that : SIZE()=exponent; will not work since SIZE() is a function that returns a value. It is the equivalent to writing 10=exponent; (which obviously wont work). You don't need it I think but I am guessing.

I keep getting the error "[linker error]undefined reference to ..."

#include "poly.h"
#include "help.h"
#include <iostream>

using namespace std;

void poly::setCoefficient (const int exponent, const double coefficient){
     coefficientVector[exponent] = coefficient;
   //  SIZE() = exponent;
     }
     
poly poly::operator +(const poly &rtSide) const{
     poly result;
 //    if ( SIZE <= rtSide.SIZE){
   //       poly result(SIZE);
     //     result.SIZE = SIZE;}
     //else{
     //     poly result(rtSide.SIZE);
      //    result.SIZE = rtSide.SIZE;}     
     for( int i = 0; i <= result.SIZE() ; i++){
          result.coefficientVector[i] =  coefficientVector[i] + rtSide.coefficientVector[i];
          }
     return result;}

int poly::degree () const { 
    return SIZE();}
 
void poly::output() const{ 
         for( int i = 0; i <= 26 ; i++){
          cout << i << "\t" << coefficientVector[i];
          }}
     
int main(){
    poly A;
    A.setCoefficient(3, 3.33);
    A.output();
    cout<< A.degree();
    system("PAUSE");
    return 0;}

Come on lets us play guess the error message!!!! WHAT DOES YOUR ERROR MESSAGE SAY ?????

I get undefined reference to `poly::poly() so were is your constructor [for a start]

I get the same error message as you but I am unsure what to do

What does it say: it says that there is no poly::poly(). So were is your

poly::poly() : // maybe stuff here...
{
   // maybe stuff here 
}

While you are at it, fix the other errors and repost the working code.

I did classes in a previous program before and never got this message , so I am still unsure how to fix this error.

Take my previous post, take the code and fill in the comments with real code.

Read the words of the error message OUT-LOUD. I am serious, then you will see that you have a reference to a method that is not defined.

consider this code:

void testFunc();

int main()
{
   testFunc();
}

Now make that work, i.e. compile AND RUN. That is your error in a nutshell..

I still don't know how to fix the error:


This is my .h file

#include <vector>

class poly
{
private:
    std::vector<double> coefficientVector;
		// Constructed implicitly to initially be null.
    int SIZE() const;  // Shorter way to write size() of a poly
private:
    bool wellFormed () const;  // Null or last coefficient is non-zero
    void trimZeros ();  // Remove high-end zero coefficients.
public:
    void nullify (); // Create 'null' poly with no coefficients
    poly ();  // Not needed when based on 'vectors'
    bool isNull () const;
    int degree() const;

public:
    void setCoefficient (const int exponent, const double coefficient);
    double getCoefficient (int exponent) const;
    void output() const;
    poly& operator =(const poly& rtSide);
    poly operator +(const poly& rtSide) const;
    poly operator -(const poly& rtSide) const;
    poly operator *(const poly& rtSide) const;

    double evaluate (double x) const;  // Evaluate the poly at point x
    // Boolean and, or, not.  False = null, True otherwise
    poly operator &(const poly& rtSide) const;
    poly operator |(const poly& rtSide) const;
    poly operator ~() const;
};  // End of poly class

This is my .cpp file

#include "poly.h"
#include "help.h"
#include <iostream>

using namespace std;

void poly::setCoefficient (const int exponent, const double coefficient){
     coefficientVector[exponent] = coefficient;
   //  SIZE() = exponent;
     }
     
poly poly::operator +(const poly &rtSide) const{
     poly result;
     for( int i = 0; i <= result.SIZE() ; i++){
          result.coefficientVector[i] =  coefficientVector[i] + rtSide.coefficientVector[i];
          }
     return result;}

int poly::degree () const { 
    return SIZE();}
 
void poly::output() const{ 
         for( int i = 0; i <= 26 ; i++){
          cout << i << "\t" << coefficientVector[i];
          }}
    
//    double getCoefficient (int exponent) const;
//    void output() const;
//    poly& operator =(const poly& rtSide);     
         
//    poly operator -(const poly& rtSide) const;
//    poly operator *(const poly& rtSide) const;

So to fix this I commentted out the extra functions in my .h file but now my program just crashes.

We are not mind readers: no code, no help because there is no way to help.

commented: Manners -1

Got it to work :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.