-5

I know that someone else has asked this question before but they didn't write the program the way I do or make the mistake I made. So here we go.

When I run the program, no matter what, it also prints out the imaginary roots.

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

  typedef struct tagComplex
  {
  double real;
  double imag;
  }Complex;

  void main(void)
  {
  SetConsoleTitle("Solve ax^2+bx+c=0");

  double a, b, c, delta;
  Complex x1, x2;
  char k = 'y';

  while(k == 'y')
  {
    printf("Enter the values of a, b, c: ");
    scanf("%lf%lf%lf", &a, &b, &c);
    while(getchar()!= '\n');

    delta = b*b - 4*a*c;
    if(delta > 0)                   //←-------- or delta > 1e-6 ?
    {
        x1.real = (-b + sqrt(delta))/(2*a);
        x2.real = (-b - sqrt(delta))/(2*a);

        printf("x1=%.3lf x2=%.3lf\n\n", x1.real, x2.real);
    }
    if(delta == 0)                 //←-------- or delta <= 1e-6 ? 
    {
        printf("x1=x2=%.3lf\n\n", -b/(2*a));
    }
    else
    {
       x1.real = -b/(2*a);
       x1.imag = sqrt(-delta)/(2*a);

       x2.real = -b/(2*a);
       x2.imag = -sqrt(-delta)/(2*a);

       printf("x1=%.3lf+%.3lf i x2=%.3lf+%.3lf i\n\n", x1.real, x1.imag, x2.real, x2.imag);
    }

    printf("Try another equation? (y or n)\n");
    scanf("%c", &k);
  }
}

How can I fix it?

Sample data: 1 -4 3

output:     
             x1=3.000 x2=1.000

             x1=2.000+-1.#IO i x2=2.000+1.#QO i

             Try another equation? (y or n)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
D.Cosmos
  • 1
  • 2
  • 1
    Sorry this isn't an answer to your question, but the `main` function should return an `int`. – Christian Gibbons Dec 14 '18 at 01:24
  • 1
    Can you edit to include an example of input, what output you think it should give, and what it actually gives? Also, have you [debugged](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) ? – Phil M Dec 14 '18 at 01:28
  • 2
    I READ THROUGH AND STILL DOWNVOTED :-) Whining about bullies is unlikely to endear you to us, especially since it's unlikey to be bullying at all, rather the question is deficient as it stands - you shouild have provided sample data that causes the problem. Of course, I'm treating your "I'd appreciate any help" comment as allowing us to help you better formulate questions in future. – paxdiablo Dec 14 '18 at 01:32
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Shawn Dec 14 '18 at 01:32
  • Usually when comparing doubles you have to use an epsilon. Comparing directly with zero may give you unpredictable results. You can read more on below answer: https://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floating-point-values – Leni Dec 14 '18 at 01:38
  • 2
    @Shawn No, floating point math is not to blame here. That is the wrong duplicate. – Henri Menke Dec 14 '18 at 01:43
  • Note in passing that `scanf("%c", &k);` is unlikely to let you go back and have another go; it will most probably read the newline after the last number, and recognize that it isn't a `'y'` and terminate the loop. Use `scanf(" %c", &k);` with an all-important space before the `%c` to skip white space (which includes newlines). – Jonathan Leffler Dec 14 '18 at 01:47
  • @HenriMenke comparing a computed floating point value with `==` certainly is a problem. – Shawn Dec 14 '18 at 01:49
  • @Shawn That might be true but it is nevertheless not the reason for the the bug. – Henri Menke Dec 14 '18 at 01:51

1 Answers1

4

Currently your branch reads

if(delta > 0) {
    // print two real roots
}
if(delta == 0) {
    // print single root
}
else {
    // print two complex roots
}

As you can see, if delta > 0 is true, then delta == 0 will be false, and execution will go to the else branch. You have to make the delta == 0 statement conditional on the delta > 0 as well.

That is to say, replace if(delta == 0) with else if(delta == 0).

Live example on Wandbox

Henri Menke
  • 10,705
  • 1
  • 24
  • 42
  • 1
    Good answer. So many questions on SO follow the same pattern. I think debugging should be taught before programming. You get given a simple program containing a bug, and you are taught how to compile it and step through execution in a debugger to find the problem. After successfully learning that, you get to learn "hello world". – paddy Dec 14 '18 at 01:46
  • Wow yes, thanks a ton. That solves my problem. Much love from me and my program! – D.Cosmos Dec 14 '18 at 01:46
  • Oh I wasn't taught debugging but now I intend to learn it. Can I ask you guys to recommend me some tutorials? – D.Cosmos Dec 14 '18 at 01:47
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – paddy Dec 14 '18 at 01:48
  • @D.Cosmos [Good debugger tutorial for beginners](https://stackoverflow.com/questions/12546706/good-debugger-tutorial-for-beginners). – Henri Menke Dec 14 '18 at 01:49
  • @chux Yes, I know. My earlier comment was just a pasted snippet. – Henri Menke Dec 14 '18 at 08:24