# Quadratic Function

Printable View

• 11-13-2006
abs.emailverify
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.
• 11-13-2006
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.
• 11-13-2006
abs.emailverify
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
• 11-13-2006
Happy_Reaper
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.
• 11-14-2006
dwks
Quote:

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; };```
Quote:

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);`
• 11-14-2006
King Mir
If a = 0, the the 1 root is given by -c/b.