Here's the problem. I've written a code but can't correct the errors in it.Here's the question and the code:
"Write a C++ function named quadratic that given three numbers a, b, c solves quadratic equations using the formula: x^2 = (-(b^2) + b-4ac) / (4a^2)
The function returns
• -1 if the equation is not quadratic (a = 0)
• 0 if the equation has no real solution (b2 – 4ac < 0)
• or the result
You can use the standard library function to compute the square root."
#include<iostream>
#include<cmath>
using namespace std;
float quadratic ( float a, float b, float c )
{
float x, y;
x = (-b + sqrt ((b*b) - (4*a*c)))/(2*a);
y = (-b - sqrt ((b*b) - (4*a*c)))/(2*a);
if((b*b)-(4*a*c)){
return 0;
}
else if(a == 0){
return -1;
}
else{
cout<<"Answer is either "<<x<<" or "<<y<<endl;
return x, y;
}
}
int main()
{
int d, e, f;
cout<<"Enter first number: ";
cin>>d;
cout<<"Enter Second number: ";
cin>>e;
cout<<"Enter third number: ";
cin>>f;
quadratic (d, e, f);
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
float quadratic ( float a, float b, float c )
{
float x, y;
x = (-b + sqrt ((b*b) - (4*a*c)))/(2*a);
y = (-b - sqrt ((b*b) - (4*a*c)))/(2*a);
if(((b*b)-(4*a*c)) < 0){
cout << 0 << "\n";
return 0;
}
else if(a == 0){
cout << -1 << "\n";
return -1;
}
else{
cout<<"Answer is "<<x<<" or "<<y<<endl;
return x, y;
}
}
float main()
{
float a, b, c;
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter Second number: ";
cin>>b;
cout<<"Enter third number: ";
cin>>c;
quadratic (a, b, c);
return 0;}
Try that ;]
When you started manipulating the numbers, you were giving the computer undefined constants, so it didn't really know what to do. You may also have received a warned about converting the integers to floated values, which is solved by just floating the inputted values.
still I'm getting these errors:
3lab4.cpp: In function âfloat quadratic(float, float, float)â:
3lab4.cpp:23: warning: left-hand operand of comma has no effect
3lab4.cpp: At global scope:
3lab4.cpp:26: error: â::mainâ must return âintâ
I got these errors after changing "float main ()" to "int main":
3lab4.cpp: In function âfloat quadratic(float, float, float)â:
3lab4.cpp:20: warning: left-hand operand of comma has no effect
What does the first error mean or imply?
A function can't return more than one value. return x,y is wrong. What the warning (a warning is not an error) is saying is that x in x,y has no effect.