+ 1
Hello please I have a school course work on c++ please I don't know if anyone can help
The question is Write a program that can solve the quadratic equation we call the almighty formula
10 Respostas
+ 7
We're not here to write code for you from scratch, but make some kind of attempt first and we'll be happy to help you improve and debug it
+ 2
Good place to start is knowing what the quadratic equation is and how to solve it. Once you understand that, you can break it down into C++ code following the same logic. If they're asking you to do that, then I assume they've already taught you the stuff to put it together. Post up your code if you run into issues, as Orin said, and we'll help out.
Resource for understanding the quadratic equation:
https://en.wikipedia.org/wiki/Quadratic_equation
+ 1
Well sure, most linear equations do have a x-intercept lol; but the equation is also no longer quadratic in that case, so an error isn't uncalled for either. I think either option is a defensible choice.
+ 1
Supplementally, if you do decide to handle the linear case, don't forget to handle the constant case, a == b == 0 as well (no solutions for c != 0, infinite solutions for c == 0)
0
at least practice your google skill
https://www.programiz.com/cpp-programming/examples/quadratic-roots
0
#include <iostream>
#include <cmath>
int main() {
double a, b, c;
std::cout << "Enter the coefficient a: ";
std::cin >> a;
std::cout << "Enter the coefficient b: ";
std::cin >> b;
std::cout << "Enter the coefficient c: ";
std::cin >> c;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
std::cout << "Root 1: " << root1 << std::endl;
std::cout << "Root 2: " << root2 << std::endl;
} else if (discriminant == 0) {
double root = -b / (2 * a);
std::cout << "Root: " << root << std::endl;
} else {
std::cout << "The quadratic equation has no real roots." << std::endl;
}
return 0;
}
0
This is one of them
0
Seems to be working; only thing I'd really add is throwing an error if a==0
0
Orin Cook
a = 0 shouldn't throw an error... there is a real number solution for x.
Most online quadratic solvers will not work for a = 0, but if you try Wolfram Alpha,
https://www.wolframalpha.com/input?i=quadratic+formula+calculator&assumption=%7B%22F%22%2C+%22QuadraticEquation%22%2C+%22c%22%7D+-%3E%221%22&assumption=%7B%22FS%22%7D+-%3E+%7B%7B%22QuadraticEquation%22%2C+%22x%22%7D%7D&assumption=%7B%22F%22%2C+%22QuadraticEquation%22%2C+%22b%22%7D+-%3E%222%22&assumption=%7B%22F%22%2C+%22QuadraticEquation%22%2C+%22a%22%7D+-%3E%220%22
It gives you the proper solution.
Maybe solving for x when a = 0 should be added to the code?
0
#include <iostream>
int main() {
double x, y;
std::cout << "Enter the value of x: ";
std::cin >> x;
std::cout << "Enter the value of y: ";
std::cin >> y;
double sum = x + y;
double difference = x - y;
double product = x * y;
double quotient = x / y;
std::cout << "Sum of x and y: " << sum << std::endl;
std::cout << "Difference of x and y: " << difference << std::endl;
std::cout << "Product of x and y: " << product << std::endl;
std::cout << "Quotient of x and y: " << quotient << std::endl;
return 0;
}