Help With Functions

This is a discussion on Help With Functions within the C Programming forums, part of the General Programming Boards category; Hello I am creating a program that solves the quadratic equation ax^2 + bx +c. I have this program almost ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Help With Functions

    Hello I am creating a program that solves the quadratic equation ax^2 + bx +c.

    I have this program almost complete except the output of the equation in the function called display_quadratic. I need the program to display the variables a,b,c in the equation ax^2 + bx + c but I am having 2 problems. My first problem is that I cannot get the right addition and subtraction signs for the equation.

    For instance, if the program had the values for a,b,c as 2,2,3

    it will display 2x^2 2x 3

    How can I get it to display 2x^2 + 2x + 3? or if it was negative like 2x^2 - 2x - 3?

    My next question is how do I get to not display the coefficients that are 1?

    I had an if-else statement but no matter what I created it would overlap with another statement and print out twice. Any help is appreciated and welcomed. Thank you.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void display_quadratic (float a, float b, float c);
    float root1(float a, float b, float c);
    float root2(float a, float b, float c);
    float discriminant (float a, float b, float c);
    float r1,r2;
    
    int
    main (void)
    {
        float a,b,c;
    
        printf("This program solves the quadratic equation ax^2 + bx + c == 0.\n");
        printf("Enter a, b, and c:");
        scanf("%g%g%g", &a, &b, &c);
        printf("OK, the equation you're specifying is:");
        display_quadratic(a,b,c);
        printf(" == 0\n");
    
        r1 = root1(a,b,c);
    
        r2 = root2(a,b,c);
    
        printf("The roots of your equation are %g and %g, and the factorization is:\n ", r1,r2);
    
        display_quadratic(a,b,c);
        printf(" == ");
    
        if (r1 > 0)
        {
        printf("(x - %g)", r1);
        }
        else
        printf("(x + %g)", (r1 * -1));
    
    
        if (r2 > 0)
        {
        printf("(x - %g)", r2);
        }
        else
        printf("(x + %g)", (r2 * -1));
    }
    
    
    void display_quadratic (float a, float b, float c)
    {
    
        if (a == 1){
            printf("x^2 ");}
        else printf("%gx^2 ", a);
    
        if (b == 1){
            printf("x ");}
        else printf("%gx ", b);
    
        printf("%g ", c);
    }
    
    float discriminant (float a, float b, float c)
    {
        return (pow(b,2) - 4 * a * c);
    }
    
    
    float root1 (float a, float b, float c)
    {
        float root1_calculator;
    
        root1_calculator = (-b + sqrt(discriminant(a,b,c)))/2 * a;
    
        return (root1_calculator);
    }
    
    float root2 (float a, float b, float c)
    {
        float root2_calculator;
    
        root2_calculator =(-b - sqrt(discriminant(a,b,c)))/2 * a;
    
        return (root2_calculator);
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,098
    Quote Originally Posted by M_A_T_T View Post
    For instance, if the program had the values for a,b,c as 2,2,3

    it will display 2x^2 2x 3

    How can I get it to display 2x^2 + 2x + 3? or if it was negative like 2x^2 - 2x - 3?
    The way you are doing it looks fine, except in your format you have 5 pieces, but your function is only handling 3:
    2x^2
    +
    2x
    +
    3

    You need an if/else to handle the + or minus sign. In other words, if b < 0, you need to print a minus. The same logic for coefficients of 1. If abs(b) == 1, you don't print the coefficient

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Quote Originally Posted by c99tutorial View Post
    The way you are doing it looks fine, except in your format you have 5 pieces, but your function is only handling 3:
    2x^2
    +
    2x
    +
    3

    You need an if/else to handle the + or minus sign. In other words, if b < 0, you need to print a minus. The same logic for coefficients of 1. If abs(b) == 1, you don't print the coefficient
    So would it go like:
    Code:
    if (b < 0)
    {
    printf("-%gx +", b);
    }
    if (b > 0)
    {
    printf("%gx +", b);
    }
    if (abs (b) == 1)
    {
    printf("x");
    }
    What happens if lets say b is -3, then wouldn't it print --3?

    Also when b = 1 or -1, how will it know when to print the + or - sign with the abs(b) == 1?

    This was the situation I was getting stuck on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 07:54 AM
  2. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 10:50 AM

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