Solving cubic equations (OPTIONAL)

  1. Perform exercise P4.4 on page 169 of the text. The program should input the coefficients, and then output the solution(s):
    Input coefficients of x^3 + ax^2 + bx + c? -2 -1 2
    The three solutions are 1, -1 and 2
    

    Warning: The equations in the book may not be correct. Continue on with the lab, anyway.

  2. Be sure that cin.fail() is checked, and that an appropriate error is reported and return with a non-zero status (say, 1). It's good style to send error messages to cerr rather than cout.

  3. Before outputting the result(s), have the program check to see that if the solution are plugged into the polynomial, the result really is 0. If not, display an error suggesting a bug in your program. Remember to cope with the possibility of roundoff errors.

  4. Thoroughly test your program. You can check with a neighbor to see if results are consistent. If they differ, you can also check with a neighbor to figure out where results differ.

    Make sure the answers are correct when you give it good input. Make sure the program gives an appropriate result on bad input. Also, test your testing routine (the part that plugs values back into the polynomial). You can do this either by introducing a bug into your program. But a much better way is to set a breakpoint in gdb and change one of the solutions a bit using

      set var = value