Thread: Help with this quadratic

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Angry Help with this quadratic

    Hello all,
    I hope I am not bombarding you with my problem but I need to know what was wrong with the code below:

    Code:
    /* program to calculate the root of a quadratic equation*/
    #include <stdio.h>
    #include <math.h>
    main()
    {
       float a, b, c, d, r=0.0, x1, x2;
       printf ("\nEnter value for a\n");
       scanf ("%f", &a);
       printf ("\nEnter value for b\n");
       scanf ("%f", &b);
       printf ("\nEnter value for c\n");
       scanf ("%f", &c);
       d=sqrt((b*b)-4*a*c);
       if (d>r)
        x1=(-b+d)/(2*a);
        x2=(-b-d)/(2*a);
        printf("\nThe real roots are %f, %f\n", x1, x2);
       else if (d<r){
        printf ("\nThere are no real root because the equation turns to infinity\n");
        };
       else
        x1=(-b/2*a); 
        x2=(-b/2*a); 
        printf("\nThe real roots are %f, %f\n", x1, x2);
    }
    Below is the error:
    c:\program files\miracle c\quadrati.c: line 18: Parse Error, expecting `'}''
    'else if (d<r){ printf ("\nThere are no real root because the equation turns to infinity\n")'
    aborting compile

    Please help out.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Like it says, missing some brackets in your code:
    Code:
       if (d>r) {
        x1=(-b+d)/(2*a);
        x2=(-b-d)/(2*a);
        printf("\nThe real roots are %f, %f\n", x1, x2);
        }
       else if (d<r){
        printf ("\nThere are no real root because the equation turns to infinity\n");
        }
       else {
        x1=(-b/2*a); 
        x2=(-b/2*a); 
        printf("\nThe real roots are %f, %f\n", x1, x2);
        }
    [edit]Yeah, like Happy said, you should get rid of the semicolon as well... didn't catch that one before. [/edit]
    Last edited by hk_mp5kpdw; 11-08-2006 at 09:21 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Your if-else statements should be in blocks like this :

    Code:
    if(condition)
    {
             // do stuff
    }
    else
    {
             // do other stuff
    }
    That's one thing. And the other thing is that :

    Code:
    else if (d<r){
        printf ("\nThere are no real root because the equation turns to infinity\n");
        };
    You probably don't want that semi-colon there.

    Further, you should get rid of Miracle C. It's as old as the dickens, there are better free compilers and IDE's out there.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Further, you should get rid of Miracle C. It's as old as the dickens, there are better free compilers and IDE's out there.
    I use Borland C 5.5 and got the same error as well.

  5. #5
    Registered User taelmx's Avatar
    Join Date
    Oct 2006
    Location
    Japan
    Posts
    55
    Code:
    /* program to calculate the root of a quadratic equation*/
    #include <stdio.h>
    #include <math.h>
    main()
    {
       float a, b, c, d, r=0.0, x1, x2;
       printf ("\nEnter value for a\n");
       scanf ("%f", &a);
       printf ("\nEnter value for b\n");
       scanf ("%f", &b);
       printf ("\nEnter value for c\n");
       scanf ("%f", &c);
       d=sqrt((b*b)-4*a*c);
       if (d>r){
        x1=(-b+d)/(2*a);
        x2=(-b-d)/(2*a);
        printf("\nThe real roots are %f, %f\n", x1, x2);
    }   
    else if (d<r){
        printf ("\nThere are no real root because the equation turns to infinity\n");
        }
       else {
        x1=(-b/2*a); 
        x2=(-b/2*a); 
        printf("\nThe real roots are %f, %f\n", x1, x2);
    }




    //Try that.

  6. #6
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    You need to check the sign of the discriminant before attempting to get the square root of it, as you may be trying to take the sqrt of a negative value. Eg.
    Code:
    d = b * b - 4 * a * c;
    if(!d) printf("one real root: %f", -b / (2 * a));
    if(d > 0)
    {
    	d = sqrt(d);
    	
    	printf("two real roots: %f, %f", (-b + d) / (2 * a), (-b - d) / (2 * a));
    }
    else printf("no real roots");
    Last edited by SirNot; 11-08-2006 at 10:15 AM.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by Happy_Reaper
    Your if-else statements should be in blocks like this :

    Code:
    if(condition)
    {
             // do stuff
    }
    else
    {
             // do other stuff
    }
    That's one thing. And the other thing is that :

    Code:
    else if (d<r){
        printf ("\nThere are no real root because the equation turns to infinity\n");
        };
    You probably don't want that semi-colon there.

    Further, you should get rid of Miracle C. It's as old as the dickens, there are better free compilers and IDE's out there.
    Honestly, i dont know what I could have done without the able and intelligent people in this forum, I did it and it works great. Thanks so much.

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by abs.emailverify
    I use Borland C 5.5 and got the same error as well.
    I never said the compiler was the problem, just that you should not use Miracle C (as it could potentially cause you problems).
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    and dont forget to include return in your main function. Main should always return 0. Include this at the end

    Code:
    return 0;
    ssharish2005

  10. #10
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    > and dont forget to include return in your main function. Main should always return 0. Include this at the end

    Supposed that main returns an integer value I guess ... what does just "main() {" return ? (?.?)

  11. #11
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Supposed that main returns an integer value I guess ... what does just "main() {" return ? (?.?)
    Main should always return an int, and as such should be declared as int main().
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  12. #12
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    > Main should always return an int, and as such should be declared as int main().

    Hehe, I know, just wondering about this case.

  13. #13
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Even in that case, I think most compilers default to int when a function's return value isn't specified. So main still returns an int. But you're going into compiler specific land...Not a good place to be.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by TriKri
    > and dont forget to include return in your main function. Main should always return 0. Include this at the end

    Supposed that main returns an integer value I guess ... what does just "main() {" return ? (?.?)

    it is by default int main(). You specify it or not it is int.

    ssharish2005

  15. #15
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    > it is by default int main(). You specify it or not it is int.

    Ok, thanks for the info. Though I'm not planing to ever use just "main() {".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic formula in c
    By cakestler in forum C Programming
    Replies: 2
    Last Post: 02-08-2009, 09:41 PM
  2. Quadratic Function
    By abs.emailverify in forum C Programming
    Replies: 5
    Last Post: 11-14-2006, 02:07 PM
  3. Quadratic Probing
    By TheSquid in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2005, 11:35 PM
  4. Quadratic Factoring
    By neandrake in forum C++ Programming
    Replies: 11
    Last Post: 04-15-2002, 04:49 PM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM