Synthetic Division Program Being Weird?

I am writing a code to do synthetic division. I can not figure out why it isn't working. I also plan on translating this into BASIC-Ti. This is stressing me out because it should be way below my coding level... I think I am missing something obvious.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;

int main()
{
  int Mult;
  int N;
  int Div;
  
  cout << "What is the multiplicity of your polynomial?\n";
  cin >> Mult;
  N = Mult; 
  int Term[Mult];

  while (N >= 0){
      cout << "What is the Coefficient of Val " << N << endl;
      cin >> Term[N];
      N--;
  }
  cout << "What is the dividend without X\n";
  cin >> Div;
  int TermA[Mult];
  int i = Mult;
  string TERM[Mult];
  
  while (i >= 0){
  TermA[i] = Term[i-1] - (TermA[i] * Div);
  TERM[i] = to_string(TermA[i]) + "X^" + to_string(i-1) + " + ";
  cout << TERM[i];
  i--;
  }
  
}
C++ does not support variable length arrays (VLA) (although some compilers do as extension). The compiler cannot possibly know how large the arrays on lines 15, 24 and 26 should be. The standard C++ way is to use std::vector instead of plain arrays.

Loop on lines 17-21 iterates the range from Mult to 0. That is Mult+1 values. One more than the array has. Out of range error.

Loop on lines 28-33 iterates the range from Mult to 0. Not just out of range error as above, but two; the i-1, when i==0 dereferences Term[-1].

Array TermA is used uninitialized.


Furthermore, X^1, X^0, X^-1 and trailing '+' are less common math notation.
TermA is initialized, I moved it when I was testing something, forgot to move it back. Also the display isn't final, it was just a quick way to debug. Also thanks!
Topic archived. No new replies allowed.