Quadratic Function

This is a discussion on Quadratic Function within the C Programming forums, part of the General Programming Boards category; Hello all, I want to rewrite the quadratic equation as a function and as a function in the main. Below ...

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

    Quadratic Function

    Hello all,
    I want to rewrite the quadratic equation as a function and as a function in the main. Below are the two programs but was not giving accourate answer.

    For the function alone:
    Code:
    float quadratic(float x1, float x2)
    {
       float a, b, c, d, g, h=0.0;
       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);
       g=2*a;
       if (g==h)
       {
         printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
       }
       else
       {
         x1=(-b+d/g); 
         x2=(-b-d/g); 
         printf ("\nFirst real root is," x1);
         printf ("\nSecond real root is," x2);
       }
       return (x1, x2);
    }
    it was not printing any values neither showing any calculations done

    The second one that has main with it
    Code:
    /* program to calculate the root of a quadratic equation*/
    #include <stdio.h>
    #include <math.h>
    
    float quadratic(float a, float b, float c)
    {
       float d, g, h=0.0, x1;
       g=2*a;
       if (g==h)
       {
         printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
       }
       else
       {
         x1=(-b+((b*b)-4*a*c))/g; 
       }
       return (x1);
    }
    
    float quadratic1(float a, float b, float c)
    {
       float d, g, h=0.0, x2;
       g=2*a;
       if (g==h)
       {
         printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
       }
       else
       {
         x2=(-b-((b*b)-4*a*c))/g; 
       }
       return (x2);
    }
    main()
    {
       float a, b, c, 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);
       x1 = quadratic(a,b,c);
       x2 = quadratic1(a,b,c);
       printf("\na=%f,", a);
       printf("\nb=%f,", b);
       printf("\nc=%f,", c);
       printf("\nThe real roots are %f, %f", x1, x2);
    }
    Please help me out.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
      return (x1, x2);
    Don't return 2 values. I would suggest returning 0 as well.

    You're program ends when main ends, closing the window before you can see anything. you can use getch() to pause it until a key is pressed.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by skorman00
    Code:
      return (x1, x2);
    Don't return 2 values. I would suggest returning 0 as well.

    You're program ends when main ends, closing the window before you can see anything. you can use getch() to pause it until a key is pressed.
    It doesnt have a main function, it is just a user-defined function, also i want it to print out the result but was not doing that.

    [QUOTE=abs.emailverify]
    Code:
    float quadratic(float x1, float x2)
    {
       float a, b, c, d, g, h=0.0;
       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);
       g=2*a;
       if (g==h)
       {
         printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
       }
       else
       {
         x1=(-b+d/g); 
         x2=(-b-d/g); 
         printf ("\nFirst real root is," x1);
         printf ("\nSecond real root is," x2);
       }
       return (x1, x2);
    }
    it was not printing any values neither showing any calculations done

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Here's the 2 things that are wrong :

    1) A function in C can't return a tuple. You'd have to return a float pointer if you actually want to return both those answers.

    2) There's a reason you aren't seeing any calculation being done, namely that

    Code:
    printf ("\nFirst real root is," x1);
    won't print what you expect it to, while

    Code:
    printf ("\nFirst real root is, %f" x1);
    probably will.
    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

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    1) A function in C can't return a tuple. You'd have to return a float pointer if you actually want to return both those answers.
    Or you could return a structure containing several variables.
    Code:
    struct twofloats_t {
        float a, b;
    };
    Code:
    printf ("\nFirst real root is," x1);
    won't print what you expect it to, while
    Code:
    printf ("\nFirst real root is, %f" x1);
    probably will.
    It probably won't, unless you add a comma too.
    Code:
    printf ("\nFirst real root is, %f", x1);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    If a = 0, the the 1 root is given by -c/b.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21