### quadratic equation passing values to a function

Hi,

I've been trying the below code and it's not giving the correct results. I'd like help with it.

Thanks.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 # include # include using namespace std; void getValues(double &a, double &b, double &c); double calculate(); void display(); void getValues(double &a, double &b, double &c) { cout<<"Enter the coefficient a"<>a; cout<<"Enter the coefficient b"<>b; cout<<"Enter the coefficient c"<>c; } double calculate() { double a, b, c, d, x; x = (b * b) - (4 * a * c); d = sqrt (x); if(d < 0){ cout<<"There are no real roots"< 0){ cout<<"Root 1 is "<<-b+sqrt(d)/(2*a)<<" Root 2 is "<<-b-sqrt(d)/(2*a); } else{ cout<<"There is one equal root "<<(-b+d)/2*a; } } int main() { double a, b, c; getValues(a, b, c); calculate(); return 0; }
 it's not giving the correct results

It doesn't even compile on my box.

e.g. you're telling calculate to return a double, but your function does not return anything.

leaving that aside though...
 123 double a, b, c, d, x; x = (b * b) - (4 * a * c);

what do you think x will be calculated to, considering you haven't initialised a, b or c..?

remove the declarions for a,b and c in this function, and have them be passed in.

Last edited on
You haven't passed any values to the calculate function, you need to pass the values you got by reference to calculate such as,

calculate(a,b,c);

Also when you say,

if(d < 0){

Well you should be saying,

if (x < 0)

as you have already taken sqrt of x.
Thanks guys for the assistance. I've managed to fix the issues you pointed out and it's now working.
Topic archived. No new replies allowed.