Thread: Quadratic Equation function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    49

    Quadratic Equation function

    Hello, so I am making a program to solve quadratic equations.. or at least I am trying to. I am running into a wall however. I get the following error message: 1.c: In function 'qe':
    error: control reaches end of non-void function


    Anyways, I am using math.h but ONLY for the pow function. I am not allowed to use it for anything else. That mean I wrote a square root function that my quadratic equation function calls, and an absolute value function that my square root function calls.

    I have been working on this for a while and need some input into what I am doing wrong. Below is my code. Thank you so much for your help.

    Code:
    /* 8. An equation of the form
    ax^2 + bx + c = 0
    is known as a quadratic equation.The values of a, b, and c in the preceding example
    represent constant values. So
    
    4x^2 - 17x - 15 = 0
    
    represents a quadratic equation where a = 4, b = –17, and c = –15.The values of x
    that satisfy a particular quadratic equation, known as the roots of the equation, can
    be calculated by substituting the values of a, b, and c into the following two
    formulas:
    If the value of b^2–4ac, called the discriminant, is less than zero, the roots of the
    equation, x1 and x2, are imaginary numbers.
    Write a program to solve a quadratic equation.The program should allow
    the user to enter the values for a, b, and c. If the discriminant is less than
    zero, a message should be displayed that the roots are imaginary; otherwise,
    the program should then proceed to calculate and display the two roots of
    the equation. (Note: Be certain to make use of the squareRoot function that
    you developed in this chapter.) */
    
    #include <math.h>
    #include <stdio.h>
    
    //function to find absolute value
    float absoluteValue (float d)
    {
    
    
    if ( d < 0 )
    d = -d;
    return d;
    }
    
    // Function to compute the square root of a number
    
    float squareRoot (float d)
    {
    
    const float epsilon = .00001;
    float guess = 1.0;
    
        while ( absoluteValue (guess * guess - d) >= epsilon )
        guess = ( d / guess + guess ) / 2.0;
        
        return guess;
    }
    
    // quadratic equation function
    float qe (float a, float b, float c)
    {
    float x1, x2;
    float d;
    
    d = (pow (b, 2) - (4 * a * c));
        
        if (d < 0)
        printf ("x1 and x2 are imaginary numbers");
        
        x1 = (-b + squareRoot (d)) / (2 * a);
        
        printf("x1 is %f", x1);
        
        x2 = (-b - squareRoot (d)) / (2 * a);
         
        printf("x2 is %f", x2);
        
      
    }
    
    int main (void)
    {
    
    float a, b, c;
    
        printf("What is a?: ");
        scanf("%f\n", &a); 
          
        printf("What is b?: ");
        scanf("%f\n", &b); 
    
        printf("What is c?: ");
        scanf("%f\n", &c); 
       
        
        qe (a, b, c);
      
      return 0;  
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I get the following error message: 1.c: In function 'qe':
    > error: control reaches end of non-void function

    Well what does this function return?
    > float qe (float a, float b, float c)

    You don't have a
    return something;

    Nor do you have in main
    something = qe (a, b, c);

    The easy thing to do is simply say
    void qe (float a, float b, float c)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Ok, so I followed your instructions for which I am kicking myself that I didnt catch earlier.

    Now I am having a different issue. When the main function asks for "c" it enters an infinite loop where it continually receives input but puts out no output. (for some reason it also asks for "a" twice). I have been looking at the program for a few hours and everything looks good to me. Why would it do that?
    Last edited by sdbuilt; 09-15-2012 at 12:02 AM.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Anybody? FYI this is not homework because I am teaching myself..

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should remove those "\n" from your scanf calls.

    Each successive pointer argument must correspond properly with each suc-
    cessive conversion specifier (but see the * conversion below). All con-
    versions are introduced by the % (percent sign) character. The format
    string may also contain other characters. White space (such as blanks,
    tabs, or newlines) in the format string match any amount of white space,
    including none
    , in the input. Everything else matches only itself.
    Scanning stops when an input character does not match such a format char-
    acter. Scanning also stops when an input conversion cannot be made (see
    below).
    The problem with a trailing space matcher in scanf is that it will keep matching all the newlines you enter until you type in something else.

    All the conversion formats (except %c) automatically skip leading whitespace anyway, so there is very little need to be concerned with trying to consume a newline from the input stream.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    OK, so that definitely got rid of the program asking for "a" twice.

    Here is how I have the main function coded now.

    Code:
    int main (void)
    {
    float a, b, c;
    
        printf("What is a?: \n");
        scanf("%f", &a); 
          
        printf("What is b?: \n");
        scanf("%f", &b); 
    
        printf("What is c?:\n");
        scanf("%f", &c); 
       
        
    qe (a, b, c);
      
      return 0;  
    }
    However, I am still not getting any output after I type in "c". It still sits there allowing me to put in more input seemingly without running the qe function at all... Thanks again for your help man.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Si vous plait donnez-moi l'answer. Merci.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Your program, exactly as you wrote it (I just made the function qe() be void as suggested in post #2) works on ideone

    Ideone.com | Online C99 Compiler & Debugging Tool

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Right, it certainly compiles when I use the make tool, but I am not getting any output at all... Did you try running the program or just compiling it?

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Weird I saw that it works with the link you gave me... Hmm are there any differences between the make compiler and ideone?

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    OK I figured it out. It doesnt give any output whenever d is negative. When d is positive it works just fine. I tried to get it to print out "x1 and x2 are negative numbers" if d is negative but that part of the program isnt working. Also I am a little clueless as to how to get the program to end if d is negative.
    Last edited by sdbuilt; 09-15-2012 at 05:19 AM.

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Because when you pass a negative value to the squareRoot() function it never exits the function.

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Aha! Ok thanks a lot. Now it works. Here is what I did to fix it.

    Code:
    void qe (float a, float b, float c)
    {
    float x1, x2;
    float d;
    
    d = (pow (b, 2) - (4 * a * c));
        
        if (d < 0)
            {
            printf ("x1 and x2 are imaginary numbers\n");
            }
        if (d >= 0)
            {
            x1 = (-b + squareRoot (d)) / (2 * a);
            x2 = (-b - squareRoot (d)) / (2 * a);
       
            printf("x1 is %f\n", x1);
            printf("x2 is %f\n", x2);
            }
     
    
    }
    The last question I have though is why didn't it print out that x1 and x2 are imaginary BEFORE it got passed down to the lower level of the function where it got stuck in the square root function?

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Because you didn't have the last '\n' at the time.
    Output, in C, is usually line-buffered, meaning it only gets effectively output in full lines (there are other reasons too). The text "x1 and x2 are imaginary numbers" is not a line so it stayed in the buffer waiting for a reason to be effectively outputted; that reason never came.

    I suggest you get into the habit of terminating all printf's with a '\n'

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    awesome, I totally get it. Line buffering probably has something to do with saving processing power or memory right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic equation solving
    By narendrav in forum C Programming
    Replies: 6
    Last Post: 05-18-2011, 08:49 AM
  2. quadratic equation
    By nynicue in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 05:57 PM
  3. Quadratic Equation
    By Lucid15 in forum C Programming
    Replies: 20
    Last Post: 09-16-2008, 05:59 PM
  4. Quadratic Equation problems
    By Rachel228 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 02:42 AM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM