-1

I'm attempting to create a program which extracts a polynomial equation from a file and adds, subtracts, or multiplies the two polynomials together (depending upon the middle operand). However, I'm getting a two compiler errors that I can't quite make sense of.

The first occurs here:

int** getPolynomial(string polynomial, int &count, string Exponent[], string Coefficient[], int polyArray[][1]){
for (int i = 0; i<100; i++){
    for (int n = 0; n<100; n++){


Exponent[n] = atoi(polyArray[n][1]);
        }
        Coefficient[i] = atoi(polyArray[i][0]);

}

}

Where I'm getting an error "argument of type 'int*' is incompatible with type 'const char*' " on the lines Exponent[n] = atoi(polyArray[n][1]); } Coefficient[i] = atoi(polyArray[i][0]); Second error occurs just below,

int** add(int **left, int leftCount, int **right, int countRight, int &countResult){
for (int q; q < leftCount; q++){
    for (int r = 0; r < 2; r++){
        if (left[q][1] == right[q][r]){
            countResult[q][0] = left[q][0] + right[q][0];
        }
    }
}

}

On the line countResult[q][0] = left[q][0] + right[q][0];, where the compiler is noting that the first q "expression must have pointer-to-object type", although I can't see how making q a pointer would help.

Full code here: http://pastebin.com/FsM1ydE5. Any help on this matter would be greatly appreciated.

Hatches
  • 1
  • 1
  • The error message is pretty clear, isn't it? `polyArray[i][0];` isn't a `const char*` type as required. – πάντα ῥεῖ Apr 29 '16 at 16:46
  • I'm trying to convert the strings in array 'Coefficient' to ints in the array 'polyArray' though. Do I have the positions of the two arrays mixed up? – Hatches Apr 29 '16 at 16:50
  • The assignment operator takes the value on its right and assigns it to the var on its left. That's kinda basic stuff.. – Martin James Apr 29 '16 at 16:54
  • Sounds like you're fighting windmills :-P. Stop using raw arrays and pointers. Have a thorough look at [The c++ standard containers library](http://en.cppreference.com/w/cpp/container). – πάντα ῥεῖ Apr 29 '16 at 16:55
  • I think I can use the information [on this post](http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char) to fix the problem with needing to use const char* when I've got strings. Any idea whats up with the q in countResult needing to be a pointer apparently? I tried simply changing it to one in the for loop but as expected that really didn't help due to how the loop is built. – Hatches Apr 29 '16 at 17:06

1 Answers1

1

You're having trouble with your types:

  1. atoi returns an int, so you'll either need to change Exponent and Coefficent to int[] or you'll need to stop using atoi and just assign the value directly.
  2. CountResult is an int& which means that it cannot be indexed. You'll need to make it an int[][] to use it as you are currently.

This post would be incomplete without some direction: Use vectors to represent your polynomials. You can use the index to indicate the exponent and the value to indicate the coefficient, as long as your polynomials will be relatively dense.

Now let's say that we're given string polyArray[][] which contains our powers and their coefficients in position 0 and 1 of the secondary array, respectively. And we want to read this into vector<int> foo which will represent our polynomial. We could just do:

for(auto& i : polyArray)
    if(i[0] > size(foo)){
        foo.resize(i[0]);
    }
    foo[i[0]] = i[1];
}

Now let's say that we're given 2 polynomials that we want to sum, say vector<int> left and vector<int> right, and we want to sum these into a newly constructed polynomial: vector<int>countResult, we can do this as follows:

if(size(left) > size(right)) {
    countResult.reserve(size(left));
    transform(cbegin(right), cend(right), cbegin(left), begin(countResult), plus<int>());
    countResult.insert(end(countResult), next(cbegin(left), size(right)), cend(left));
} else {
    countResult.reserve(size(right));
    transform(cbegin(left), cend(left), cbegin(right), becing(countResult), plus<int>());
    countResult.insert(end(countResult), next(cbegin(right), size(left)), cend(right));
}
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288