Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return value from bool function with formal parameters?

Tags:

c++

int Solve(double& root1,double& root2) //this is function in the class Eq2
{
    int discrimant = b*b - 4*a*c;
    if(discriminant<0)
    return false;
    else if(discriminant>0)
    {
    root1 = (-b - sqrt(discriminant))/(2*a);
    root2 = (-b + sqrt(discriminant))/(2*a);
    return root1;    //must return root1 and root2
    }
}

int main()
{
Eq2 eq1(1, -5, 6);   //discriminant=1,root1=2,root2=3
Eq2 eq2(1, -6, 8);   //discriminant=4,root1=2,root2=4
Eq2 eq3(1, 2, 3);
Eq2 eq4(0, 0, 0);

double root1, root2;
Eq2 eqq[4] = { eq1,eq2,eq3,eq4 };
for (int i = 0; i < 4; i++)
{
    eqq[i].print();
}
cout << "The roots of the equation are:"<<eq1.Solve(root1,root2);   //here 
i call the function

system("pause");
return 0; 
}

I made the function int and it is working but i can ouput just 1 root.I need to return root1 and root2.Thank you everyone for your help!I'm still in the beginning and i have a lot to learn.

like image 751
Amanda Avatar asked Dec 06 '25 08:12

Amanda


1 Answers

Disclaimer:

While writing my own MCVE, OP edited the question. Hence, my identifiers are a little bit different. I hope this will not cause confusion...

Discriminant is such a special word, that I immediately remembered that I learnt it in math. The rest was lost but immediately refreshed from Wikipedia and what else google found.

The actual issue of OP seems to be to understand how reference parameters can be used to return values from functions without using (or additionally to) return. This is IMHO already covered in the other answers.

Hence, I focused on demonstrating this in action.

Thereby, I noticed some additional issues which should be considered.

  1. std::sqrt should be called with value ≥0 only.

If the argument is less than -0, FE_INVALID is raised and NaN is returned.

  1. Division by 0 should be checked.

  2. (Due to hint of my colleague who is Dipl.-Math.) With very small values, the results of division become non-trustable.

It's in general a bad idea to compare floating point values with constants due to the usual rounding issues. Hence, I introduce an eps – an epsilon value to compensate at little bit.

Sample code:

#include <cmath>
#include <iostream>

struct Quad {
  double a, b, c;
  Quad(double a, double b, double c): a(a), b(b), c(c) { }
  Quad(const Quad&) = default;
  Quad& operator=(const Quad&) = default;
  ~Quad() = default;

  bool solve(double &root1, double &root2) const;
};

std::ostream& operator<<(std::ostream &out, const Quad &quad)
{
  return out << quad.a << "x² + " << quad.b << "x + " << quad.c;
}

static double eps = 1E-10;

/* tries to solve the roots of this quadratic function.
 *
 * return: true (solution exists)
 *         root1 and root2 contain results
 *         false (no solution)
 *         root1 and root2 are indeterminate (unchanged)
 */
bool Quad::solve(double &root1, double &root2) const
{
  double discriminant = b * b - 4 * a * c;
  if (discriminant < 0.0) return false; // square root of a negative!
  if (std::abs(a) < eps) return false; // division by 0! (or close to 0)
  root1 = (-b - std::sqrt(discriminant)) / (2 * a);
  root2 = (-b + std::sqrt(discriminant)) / (2 * a);
  return true;
}

int main()
{
  double root1, root2; // declared but not yet initialized
  Quad quad1(-1, 0, 1); // two solutions
  std::cout << "Roots for " << quad1 << ": ";
  if (!quad1.solve(root1, root2)) std::cout << "none\n";
  else std::cout << root1 << ", " << root2 << '\n';
  Quad quad2(1, -4, 4); // one solution
  std::cout << "Roots for " << quad2 << ": ";
  if (!quad2.solve(root1, root2)) std::cout << "none\n";
  else std::cout << root1 << ", " << root2 << '\n';
  Quad quad3(1, 0, 2); // no solution
  std::cout << "Roots for " << quad3 << ": ";
  if (!quad3.solve(root1, root2)) std::cout << "none\n";
  else std::cout << root1 << ", " << root2 << '\n';
}

Output:

Roots for -1x² + 0x + 1: 1, -1
Roots for 1x² + -4x + 4: 2, 2
Roots for 1x² + 0x + 2: none

Live Demo on coliru

The trick is that Quad::solve() returns true or false (as documented) and updates the variables root1 and root2 in the former case but not in the latter.

In a less compact form written:

  bool valid = quad1.solve(root1, root2);
  if (valid) { // root1, root2 updated
    std::cout << root1 << ", " << root2 << '\n';
  } else { // no result computed -> root1, root2 not updated
    std::cout << "none\n";
  }

I took the sample values from this German site.

Samples from de.serlo.org

like image 109
Scheff's Cat Avatar answered Dec 08 '25 21:12

Scheff's Cat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!