1

I just started my C++ class yesterday and I'm experiencing some difficulties with accommodating myself to the language. I'm trying to complete a quadratic formula calculator, using five different user-given coefficients (which are then put into arrays), being used to calculate the roots, later placing the smaller roots (from the five equations) into a new array.

What I'm not getting is how to use the coefficients now placed into the arrays to calculate the roots, as well as the calculation for the smaller root. My code so far (I'm only modifying the main function):

#include <iostream>
#include <cmath> 
#include <cstdlib>


// True if candidate root is a root of the polynomial a*x*x + b*x + c = 0 
bool check_root(int a, int b, int c, float root) {  
  // plug the value into the formula
  float check = a * root * root + b * root + c;
  // see if the absolute value is zero (within a small tolerance)
  if (fabs(check) > 0.0001) {
    std::cerr << "ERROR:  " << root << " is not a root of this formula." << std::endl;
    return false;
  } else {
    return true;
  }
}

/* Use the quadratic formula to find the two real roots of polynomial.   Returns 
true if the roots are real, returns false if the roots are imaginary.  If the roots 
are real, they are returned through the reference parameters root_pos and root_neg. */ 
bool find_roots(int a, int b, int c, float &root_pos, float &root_neg) {
  // compute the quantity under the radical of the quadratic formula
  int radical = b*b - 4*a*c;
  // if the radical is negative, the roots are imaginary
  if (radical < 0) {
    std::cerr << "ERROR:  Imaginary roots" << std::endl;
    return false;
  }
  float sqrt_radical = sqrt(radical);  
  // compute the two roots
  root_pos = (-b + sqrt_radical) / float(2*a);
  root_neg = (-b - sqrt_radical) / float(2*a);
  return true;
}

int main() {
  int b_array[5];
  int c_array[5];
  int smaller_root[5];
  for (int i=0;i<5;i++){
    std::cout << "Enter a 'b' coefficient for the quadratic function: a*x*x + b*x + c = 0" << std::endl;
    int b;
    std::cin >> b;
    b_array[i] = b;
  }
  for (int i=0;i<5;i++){
    std::cout << "Enter a 'c' coefficient for the quadratic function: a*x*x + b*x + c = 0" << std::endl;
    int c;
    std::cin >> c;
    c_array[i] = c;
  }
  for (int i=0;i<5;i++){
    float root_1, root_2;
    bool success = find_roots(1,b_array[i],c_array[i], root_1,root_2);
    if (root_1>root_2) 
        smaller_root[i] = root_1;
    if (root_2>root_1)
        smaller_root[i] = root_2;
    } else {
      std::cerr << "ERROR:  Unable to verify one or both roots." << std::endl; 
    }
  }
  return 0; 
}

Thank you!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Mariankka
  • 89
  • 3
  • 10
  • Do you know where exactly things start to go wrong? Is it only in the third for loop that you don't know how to use coefficients? Or do you think the problem starts somewhere else? – PunDefeated Jan 28 '16 at 00:12
  • Your second `if` should be `else if`. Otherwise, you'll report an error whenever `root_1` is the smaller root. – Barmar Jan 28 '16 at 00:17
  • I don't see any problem with the way you're using the arrays. What exactly is the problem you're having? Show your sample input, the expected results, and the results you're getting instead. – Barmar Jan 28 '16 at 00:19
  • What is the `check_root()` function for? You're never calling it. – Barmar Jan 28 '16 at 00:21
  • 1
    I don't understand the error message at the end. It looks like it will report an error whenever the two roots are the same, which happens whenever `b^2 = 4ac`. Why does that mean it couldn't verify the roots? – Barmar Jan 28 '16 at 00:22
  • I think the problem starts at the third for loop, at least that's where I'm having difficulties. All of these for loops were originally a while loop, and I wasn't really sure which parts to get rid of. The error at the end was provided with the original code from the class. – Mariankka Jan 28 '16 at 00:31
  • @Mariankka Please edit your post to include the description of the problem: "It looks like it will report an error whenever the two roots are the same, which happens whenever `b^2 = 4ac`. Why does that mean it couldn't verify the roots?". Then, when you get the answer, **DO NOT** edit your question to fix the error. Otherwise people will just waste time to find an error that is no longer there, and current answers look stupid. Instead, if that solves the problem, mark the answer as accepted and optionally upvote it. But the code in the question should be the original one. Thank you! – Fabio says Reinstate Monica Jan 28 '16 at 00:48
  • 1
    have you actually successfully compiled this code? – Chris Cleeland Jan 28 '16 at 01:18

1 Answers1

1

Shouldn't this:

if (root_1>root_2) 
    smaller_root[i] = root_1;
if (root_2>root_1)
    smaller_root[i] = root_2;
} else {
  std::cerr << "ERROR:  Unable to verify one or both roots." << std::endl; 
}

be

if (root_1>root_2) {
    smaller_root[i] = root_1;
} else
if (root_2>root_1) {
    smaller_root[i] = root_2;
} else {
  std::cerr << "ERROR:  Unable to verify one or both roots." << std::endl;
}

I think the brace you have before the 'else' is closing the for loop }

Kevin Aud
  • 378
  • 2
  • 12
  • Yes, it definitely should be. – Mariankka Jan 28 '16 at 00:39
  • @Mariankka One of the reasons why it's a good idea to always put braces around the body of any conditional. See http://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 for more. – Barmar Jan 28 '16 at 01:07
  • @Mariankka You should also use an editor that automatically indents code. Then it will be obvious when you have structural problems like this. – Barmar Jan 28 '16 at 01:08
  • did that fix the looping problem you were having or is there still issues? – Kevin Aud Jan 28 '16 at 01:14
  • 1
    the compiler should have complained about the incorrect scoping – Chris Cleeland Jan 28 '16 at 01:17