-2

I want to calculate the root of a quadratic equation ax2 + bx + c = 0. I think my algorithm is correct, but I am new to functions and I think my mistake is with calling them. Could you help me? Thank you in advance. Here is the code.

#include<iostream>
#include<cmath>
using namespace std;
const double EPS = 1.0e-14;
int quadraticEquation(int& a,int& b, int& c)

{
    if(abs(a) < EPS)
    {
        if(abs(b) < EPS)
        {
            cout << "No solution" << endl;
        }
        else
        {
            double x = - c /b ;
            cout << "One root: " << x << endl;
        }
    }
    else
    {
        double delta = b * b - 4 * a * c;
        if(delta < 0)
        {
            cout << "No solution:\n" ;
        }
        else
        {
            double square_delta = sqrt(delta);
            double root_1 = (-b + square_delta) / (2 * a);
            double root_2 = (-b - square_delta) / (2 * a);
            cout << "x1 = " << root_1 << endl;
            cout << "x2 = " << root_2 << endl;
        }
    }

}

int main()
{
    int a = 1;
    int b = 2;
    int c = 3;
    cout << "The solution is: " << quadraticEquation(int a,int b,int c);

    return 0;
}
Jawa
  • 2,312
  • 6
  • 33
  • 36
rcson
  • 3
  • 1
  • 7

3 Answers3

3

It should be:

cout << "The solution is: " << quadraticEquation(a, b, c); // no types in calling a function
erenon
  • 17,952
  • 2
  • 56
  • 83
2

It is not really the question asked, and erenon and Paco Abato did answer the C++ problem.

But there are still some improvement to your algorythm :

  • abs(a) < EPS and abs(b) < EPS - if abc(c) < EPS every x is solution
  • if delta < 0 should be if delta <= (-EPS)
  • you forgot if abs(delta) < EPS) one double solution : double x = - b / a;
Serge Ballesta
  • 121,548
  • 10
  • 94
  • 199
-1

Define the function as:

int quadraticEquation(int a,int b, int c)

Without & signs to pass the parameters by value instead of by reference (althought as juanchopanza states on a comment it is not extrictly required).

And call it as:

cout << "The solution is: " << quadraticEquation( a, b, c);

Without int because in function calls you must not provide the type of the parameter.

Paco Abato
  • 3,524
  • 4
  • 20
  • 42