Something went wrong. Try again.
A Tour of C++ for experienced programmers, as if C++26 is the only version that ever existed.
Something went wrong. Try again.
12345678910111213141516171819202122#include <print>#include <tuple>#include <cmath>
// Solve a*x^2 + b*x + c = 0 and return the two roots.// For the equation x^2 - 3*x + 2 = 0 the roots are 2 and 1.std::tuple<double, double> solve_quadratic(double a, double b, double c) { double discriminant = b * b - 4.0 * a * c; double sqrt_disc = std::sqrt(discriminant); double root1 = (-b + sqrt_disc) / (2.0 * a); double root2 = (-b - sqrt_disc) / (2.0 * a); return {root1, root2};}
int main() { // Structured binding decomposes the tuple into two distinct names. auto [root1, root2] = solve_quadratic(1.0, -3.0, 2.0); // Print exactly "roots: 2, 1". std::println("roots: {}, {}", root1, root2); return 0;}