Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Can't get the output from chechForRoots to work. when i run it, i write three numbers and expect to see output from chechForRoots, but it just write "Waiting for a character to be pressed from the keyboard to exit." and close the program.

Objective-C
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>

double calDisk(double, double, double);
double checkForRoots(double, double, double);
double calRoot1(double, double, double);
double calRoot2(double, double, double, double, double);

main()
{
  double a,b,c;
  
  printf("Enter coeficients a, b, c\n");
  scanf("%lf %lf %lf", &a, &b, &c);

  if (a == 0 || b == 0 || c == 0){
    printf("Error: Roots cannot be determined \n");
    exit(0);}

  else{
    double discriminant = calDisk(a, b, c);
    double checkForRoots(double discriminant,double a,double b);}
  
  printf("Waiting for a character to be pressed from the keyboard to exit.\n");
   getch();
   return 0;
}

double calDisk(double a, double b, double c){
  double discriminant = (b * b - 4 * a * c);
  return discriminant;}

double checkForRoots(double discriminant, double a, double b)
{   if (discriminant < 0)
    printf("No roots\n");

    else if (discriminant == 0){
      printf("Roots are real and equal\n");
            double root1 = -b / (2.0 * a);
            double root2 = root1;
            printf("Root1 = %f\n", root1);
            printf("Root2 = %f\n", root2);}
    
    else{
     printf("Roots are real and distinct \n");
            double root1 =(-b + sqrt(discriminant)) / (2.0 * a);
            double root2 =(-b - sqrt(discriminant)) / (2.0 * a);
            printf("Root1 = %f  \n", root1);
            printf("Root2 = %f  \n", root2);}
}
Posted
Updated 28-Oct-14 8:43am
v2
Comments
CHill60 28-Oct-14 14:32pm    
What do you mean "can't get it to work"? What is your specific problem?
Member 10753543 28-Oct-14 14:38pm    
Can't get the output from chechForRoots to work. when i run it, i write three numbers and expect to see output from chechForRoots, but it just write "Waiting for a character to be pressed from the keyboard to exit." and close the program.

You're not calling checkForRoots - you're just prototyping it again.
 
Share this answer
 
Comments
Member 10753543 28-Oct-14 15:02pm    
thanks for the help, it works now. :)
Sergey Alexandrovich Kryukov 28-Oct-14 15:42pm    
Good catch, 5ed.
But I want to add that OP only thought the code is fixed and accepted the answer. In fact, it creates working but conceptually incorrect code. Please see Solution 3.
—SA
Your problem is not a complete understanding of some elementary school mathematics. Your more "advanced problem" is not understanding approximate methods of numerical calculations, which is a much more complicated issue. The quadratic equation problem solved numerically is approximate by its nature.

I can assume that you are analyzing/solving the equation
ax² + bx + c = 0
You are doing it wrong.
First of all, for a ≡ b ≡ c ≡ 0, "Roots cannot be determined" it a totally wrong answer. Correct answer will be "Any real number is a root". Also this is true: "Any complex number is a root". See the difference? You can easily check up this statement.

Another cases are when there are no real roots (is would be much nicer to output complex root) and exactly two roots. You really should not make a distinction between having two roots and only one root. Conceptually, you always have two or zero roots, and the two roots have the same value. The set of cases when the two roots are equal is of the measure null. More accurately, two different roots is the case of general position. For some basics of it, please see:
https://en.wikipedia.org/wiki/General_position[^].

It has a special meaning for approximate calculations (remember, those calculations are always approximate; not only the roots, but even a, b and c are, by definition, know with limited accuracy. Look at the quadratic function graph. As it get closer to the position where the extremum of the function value touches the x axis, two roots get close and close. With approximate calculations, you don't know exactly when it touches. The roots just became "equal" when the difference between them becomes lower than the accuracy of the real number representation (floating-point, in our case). In fact, you can slightly modify one of the coefficient while having the roots "equal" in this sense, which, of course, is not the case for ideal real numbers. So, the solution will only be correct if you calculate both roots, "equal" or not.

Now, your formula for calculation of the roots is correct, but it can be considered as questionable from the point of view of best accuracy of approximate calculation. As I remember, in his The Art of Computer Programming, Donald Knuth insisted that one should choose one of the roots of your solution, the one causing less loss of precision due to + or − sign in the solution formula; and the second root should be found using the Viera's formula:
https://en.wikipedia.org/wiki/Quadratic_equation[^],
https://en.wikipedia.org/wiki/Vieta%27s_formulas[^].

—SA
 
Share this answer
 
v2
Comments
Member 10753543 28-Oct-14 17:56pm    
Thanks for the great answer, we haven't had a lot about complex numbers, so we learn new stuff every day. didn't know about the part you always would have two roots real or complex. But, the assignment was too write out if we got 0, 1 or two roots.
Sergey Alexandrovich Kryukov 28-Oct-14 19:11pm    
You are very welcome. Complex numbers are very important, they can effectively replace tradition trigonometry (exponents instead of sin/cos, etc), critically important in theory of vibration, mechanics, theory of electricity and all electronic technology, and, very generally, physics, all described in complex numbers.

Good luck, call again.

—SA
nv3 29-Oct-14 4:52am    
Nice little treatment. 5.
Sergey Alexandrovich Kryukov 29-Oct-14 13:41pm    
Thank you very much.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900