function doesn't reply
-
Hello all; I write a simple program to calculate quadratic equation with function in c.
/*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); }
the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me. -
Hello all; I write a simple program to calculate quadratic equation with function in c.
/*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); }
the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.hasani2007 wrote:
printf("%f\n%f",solver(a,b,c));
-here you try to print 2 float values but feed printf only with one.
hasani2007 wrote:
return(x1,x2);
-this is not the way to return 2 values from a function (i guess this is what you are tryin to do), use output parameters, a struct, global variables, ..., e.g like:
void solver(float a,float b, float c, float &outX1, float &outX2){
...
outX1 = x1;
outX2 = x2;
}...
float x1, x2;
solver(a,b,c,x1,x2)
printf("%f\n%f",x1,x2);
...or
struct result
{
float x1;
float x2;
};result solver(float a,float b, float c){
...
result res;
res.x1 = x1;
res.x2 = x2;
return res;
}...
result res = solver(a,b,c);
printf("%f\n%f",res.x1,res.x2);
...> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
Hello all; I write a simple program to calculate quadratic equation with function in c.
/*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); }
the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.I'm not an expert in the details of C/C++, but the
solver
function definition shows afloat
should be returned and it appears you're trying to return a tuple.CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
Hello all; I write a simple program to calculate quadratic equation with function in c.
/*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); }
the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.Your program to calculate quadratic equations with . Input MUST have the format:
AX2 + BX + C = 0
EXAMPLE: input the equation2X2 + 4X -30 = 0
as:A= 2 B= 4 C= -30
The answers should be 3 and -5.x1=3
x2=-5
#include <stdio.h>
#include <math.h>float x1,x2;
bool solver(float a,float b, float c)
{
float delta = sqrt( (b*b) - (4*a*c) );if (!a) return false; x1 = ( -b + delta)/(2\*a); x2 = ( -b - delta)/(2\*a); return true;
}
int main()
{
float a=2,
b=4,
c=-30;/*
printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);*/
if ( solver(a, b ,c) ) printf("x1=%f\nx2=%f\n",x1,x2);
return 0;
}..
-
Hello all; I write a simple program to calculate quadratic equation with function in c.
/*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); }
the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.I want to add a comment to Code-o-mat's reply. The value of the expression (x1,x2) is the value of x2. You were trying to return two float values, however, you did not pay attention to the "," operator, and thought that (x1,x2) represents the two value you wanted to return. The compiler actually returned just the value of x2. Please read the documentation of C/C++ for the operator ",".
-
Your program to calculate quadratic equations with . Input MUST have the format:
AX2 + BX + C = 0
EXAMPLE: input the equation2X2 + 4X -30 = 0
as:A= 2 B= 4 C= -30
The answers should be 3 and -5.x1=3
x2=-5
#include <stdio.h>
#include <math.h>float x1,x2;
bool solver(float a,float b, float c)
{
float delta = sqrt( (b*b) - (4*a*c) );if (!a) return false; x1 = ( -b + delta)/(2\*a); x2 = ( -b - delta)/(2\*a); return true;
}
int main()
{
float a=2,
b=4,
c=-30;/*
printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);*/
if ( solver(a, b ,c) ) printf("x1=%f\nx2=%f\n",x1,x2);
return 0;
}..
Please, don't give such a bad solution!!! I've adjusted it to avoid global variables:
#include #include bool solver(float a,float b, float c, float &x1, float &x2)
{
float delta = sqrt( (b*b) - (4*a*c) );if (!a) return false; x1 = ( -b + delta)/(2\*a); x2 = ( -b - delta)/(2\*a); return true;
}
int main()
{
float a=2,
b=4,
c=-30;/*
printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);*/
float x1,x2;
if ( solver(a, b ,c, x1, x2) ) printf("x1=%f\nx2=%f\n",x1,x2);return 0;
} -
Please, don't give such a bad solution!!! I've adjusted it to avoid global variables:
#include #include bool solver(float a,float b, float c, float &x1, float &x2)
{
float delta = sqrt( (b*b) - (4*a*c) );if (!a) return false; x1 = ( -b + delta)/(2\*a); x2 = ( -b - delta)/(2\*a); return true;
}
int main()
{
float a=2,
b=4,
c=-30;/*
printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);*/
float x1,x2;
if ( solver(a, b ,c, x1, x2) ) printf("x1=%f\nx2=%f\n",x1,x2);return 0;
}:)