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;
}