Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

#1 Chris_027 Posted 03 October 2010 - 03:23 PM

Chris_027

Member

• Member
• 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

#2 MS-Free Posted 10 October 2010 - 01:22 PM

MS-Free

Member

• Member
• 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