Here is my code:

#include "Poly.h"

int main(){

  Poly p;

  int degree = p.degree();
  cout << degree << endl;

  int coefficient = p.coefficient(5);

  return 0;

}
#include<iostream>

using namespace std;

class Poly{

 public:

  Poly(); //constructor
  int degree(); //Returns the degree of the polynomial
  int coefficient(int); //Returns the coefficient of the x term
  int changeCoefficient(int, int); //Replaces the coefficient of the x term
  void returnPoly(); //Prints out Poly

 private:

  int power;
  int newCoefficient;
  int poly[10];

};
#include "Poly.h"

int poly[10] = {4, 5, 9, 4, 3, 3, 1, 2, 7, 1};

Poly::Poly(){

  //Empty constructor

}//end Poly

int Poly::degree(){

  return poly[2];

} //end degree

int Poly::coefficient(int power){

  for(int i = 0; i < 10; i++){

    i++; //increment i to skip over coefficient

    if(poly[i] == power){

      return poly[i - 1];
      
    }

  }

}//end coefficient

int Poly::changeCoefficient(int newCoefficient, int power){

  for(int j = 0; j < 10; j++){

    j++;//increment j to skip over coefficient

    if(poly[j] == power){

      poly[j-1] = newCoefficient;

    }

  }

}//end changeCoefficient

When I run the code I get the main returning a int of 134514845, not 5 like I want it to. Why is that and how can I fix that? Thanks,

Recommended Answers

All 4 Replies

int Poly::coefficient(int power){

  for(int i = 0; i < 10; i++){

    i++; //increment i to skip over coefficient

    if(poly[i] == power){

      return poly[i - 1];
      
    }

  }

What does the function return if your value is not found?

And secondly Though I understand the code. I am pretty unable to understand the motive of your code. Would you mind giving more info on what exactly you wish to do.

In the int Poly::degree() function, if I change return Poly[2] to Poly[15] it returns 134514720, no error about going out of the array range is shown when I run the program.

Basically the numbers in the array represent coefficients and powers of a Polynomial. The 1st, 3rd, 5th, 7th, and 9th numbers are coefficients, and the 2nd, 4th, 6th, 8th, and 10th numbers are powers, so that is why in the for loops I am incrementing i twice, to get to only the coefficients of the problem.

i dint realise that you have a private member poly in the header file and then again another variable named initialised as poly with the numbers.

So you are actually getting values from poly which has no got initialised with a value and not the other poly which is declared globally.

In the int Poly::degree() function, if I change return Poly[2] to Poly[15] it returns 134514720, no error about going out of the array range is shown when I run the program.

No, you wont see any error. In C++ an array is not a class, it does not have member functions or throw exceptions, it is just a block of memory. So when you access an element past the bounds you have defined you are simply reading a block of memory that exists past your array and representing its contents as an integer.

If you want error checking you need to either implement it yourself or use a class that manages it for you, such as the STL vector and list classes.

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.