Quadratic calculation using void

After the user inputs an A, B and c value it gives me an x and y value. This is the quadratic formular −𝑏±√𝑏2 −4𝑎𝑐 2𝑎. The questions requires me to use a void function and use the return value.
Below is my code and I am having some error on this. Please help me.


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void getx(double A, double B, double C);
void gety(double A, double B, double C);

int main(){

double a;
double b;
double c;

cout << " this program calculates the value of x and y value " << endl;
cout << " Please enter value a,b and c " << endl;
cin>>a;
cin>>b;
cin>>c;
getx(a,b,c);
gety(a,b,c);

cout << " value of x " << x << endl;
cout << " value of -x" << y << endl;

return 0;

}

void getx(double A, double B, double C)

{

double x;
x = (B + (1/2(B * B - 4 * A * C))) / (2 *A);
return x;
}
void gety(double A, double B, double C)
{
double y;

y = (+B - (1/2(B * B - 4 * A * C))) / (2 * A);

return y;
}
~
To calculate the square root you need
 
#include <cmath> 
and call the sqrt() function.
http://www.cplusplus.com/reference/cmath/sqrt/
A void function cannot return a value. You must have misunderstood your assignment.
OP has made a pretty specific claim about the function returning void. Sounds like he knows his assignment to me.

He can use other methods to return a value, such as a reference argument...?

void solve_quadratic( double a, double b, double c, double& x1, double& x2 );

Follow your instructions, but I presume “x” and “y” are for the left and right roots, right? (It is odd to refer to a coordinate value on the X axis as “y”.)

By putting this all in one function you’ll need to be more careful about how you compute your roots.

1. Find your discriminant (the stuff under the radical). If negative, there are no real roots. Set x1 and x2 both equal to NaN or something and return;
1
2
3
4
5
6
7
8
#include <limits>
...

  if (discriminant < 0.0)
  {
    x1 = x2 = std::numeric_limits <double> ::quiet_NaN();
    return;
  }


2. Find denominator: double denom = 2 * a;. Again, if this is zero, there is something wrong with the input. Return NaNs.

3. Find the square root of the discriminant. (Use std::sqrt() in <cmath>.)

4. Compute x1 and x2.

Hope this helps.
Topic archived. No new replies allowed.