Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Quadratic formula in C


  • Please log in to reply

#1
Chris_027

Chris_027

    Member

  • Member
  • PipPip
  • 87 posts
Hi I was wondering if anyone could help me out with a program I am trying to build. I'm new to the C language and am trying to learn as much as I can. My program solves the quadratic equation and I am trying to modify it with IF statements to make it capable of solving problems with complex solutions.
This is what I have so far.

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

/*Function Prototypes */
double find_solution_1(double a, double b, double c);    /*Calculates 1st solution */
double find_solution_2(double, double, double);    /*Calculates 2nd solution */


int main(void)
{
    double a, b, c, s1, s2;
    
    printf("Enter the coefficient values for a, b, and c\n");   /*get input*/
    scanf("%lf" , &a);
    scanf("%lf" , &b);
    scanf("%lf" , &c);
    
    s1 = find_solution_1(a, b, c);                              /*function calls*/
    s2 = find_solution_2(a, b, c);
    printf("x = %.2f, x = %.2f\n" , s1, s2);                        /*display results*/
    
    
    
    system("pause");
    return (0);
}

double find_solution_1(double a, double b, double c)    /*Define Function */
{
       double  result;
       
       result = (-b + sqrt(b * b - 4 * a * c))/(2 * a);
       
       return (result);
}
double find_solution_2(double a, double b, double c)    /*Define Function */
{
       double  result;
       
       result = (-b - sqrt(b * b - 4 * a * c))/(2 * a);
       
       return (result);
}
Any help would be greatly appreciated.
  • 0

Advertisements


#2
MS-Free

MS-Free

    Member

  • Member
  • PipPipPip
  • 425 posts
Great question. I hadn't really thought much about trying to program with complex numbers before...

If no naticve support is offered - your going to have to split your answers into 2 variables - a real part and an imaginary part.

Personally in regards to quadratics - I would create a function for calculating the value under the radical (sense that's where any imaginaries would come into play.

Perhaps also have a boolean variable isComplex.

Then, when isComplex = true, the final answer output may have to be a string.

solution = String.valueOf(Real) + String.valueOf(Imaginary)

I'm not going to go and give a specific answer in code sense I haven't learned C syntax yet.

Great programming exercise! Thanks for the challenge.

Hope that helps a little.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP