简体   繁体   中英

My function factoring program has problems with variables, shows incorrect answers

I'm making a program that factors functions (f(x), not fully factored though):

#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;
int x3;
int x2;
int x;
int remain;
int r = 0;
int factor;

int main() {

  int b, i, j = 0;
  int factors[101];

  cout << "f(x) = x^3 + x^2 + x + r (Factor tool)" << endl;
  cout << "x^3?: ";
  cin >> x3;
  cout << "x^2: ";
  cin >> x2;
  cout << "x: ";
  cin >> x;

  printf("remain  (Y intercept): ");
  scanf("%d", &b);
  cout << "f(x) = " << x3 << "x^3 + " << x2 << "x^2 + " << x << "x + " << b
       << "" << endl;

  cout << "factors of remainder are: " << endl;
  for (i = 1; i <= b; i++) {
    if (b % i == 0) {
      factors[j++] = i;
      printf("%d\t", i);
    }
  }
  getchar();

  while (true) {
    int good;

    if (factors[1] == 0) {
      cout <<endl;
      cout << "Equation Cannot be factored";
      break;
    }
    int factorv = factors[r];
    int nx1 = x3 * factors[r];
    int nx2 = (nx1 + x2);
    int nx3 = x + (nx2 * factors[r]);
    int nx4 = remain + (nx3 * factors[r]);

    if (nx4 == 0) {
      int factored = (0 - factors[r]);
       cout <<endl;
       cout << "The Factored Function: f(x) = "
           << "(x " << factored << ")(" << nx1 << "x^3 + " << nx2 << "x^2 + "
           << nx3 << "x"
           << ")"
           << "";
      break;

    } else {
      r = r + 1;
    }
  }
}

but in this part of the code, it shows as (x 0)(0x^3 + (x3 input instead of calculated nx1)x^2 + (x2 input instead of calculated nx2)x).

 if (nx4 == 0) {
    int factored = (0-factors[r]);
    cout<<"The Factored Function: f(x) = "<<"(x "<<factored<<")("<<nx1<<"x^3 + "<<nx2<<"x^2 + "<<nx3<<"x"<<")"<<"";
    break;

What happen to my nx variables? Why is it coming up incorrect or as a 0 when it was calculated properly above?

You have some of your variables twice:

int nx1;
int nx2;
int nx3;
int nx4;

They exists as global variables and again in the scope of main . They have the same name but are different variables.

Take the lesson: Global variables are no good.

Moreover you have a logic error in your code. When I add a std::cout << r << std::endl; in the last while loop I see its value increasing until there is a segfault, because factors[r] is out-of-bounds. broken code @ wandbox

I cannot really tell you how to fix it, because I would have to dive into the maths first. I can only suggest you to never use infinte loops in numerical codes without an "emergency exit". What i mean is that unless fully tested, you cannot be sure that the loop will end at some point and when it doesn't typically the consequences are bad and difficult to diagnose. Always make sure the loop will end at some point:

int max_iteratons = 100;
int counter;
while (counter < max_iteratons) {
    // do something
    ++counter;
}
if (counter == max_iterations) std::cout << "i have a bug :(";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM