Skip to content

Instantly share code, notes, and snippets.

@plugn
Created February 2, 2020 20:46
Show Gist options
  • Save plugn/514d5af8bb158a7ff8c23e5c39edeb0c to your computer and use it in GitHub Desktop.
Save plugn/514d5af8bb158a7ff8c23e5c39edeb0c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <cmath>
std::vector<double > calc_sqrt_results(int a, int b, int c)
{
std::vector<double> results;
double discriminant = b * b - (4 * a * c);
if (discriminant == 0) {
results.push_back( (0 - b) / (2 * a) );
}
else if (discriminant > 0) {
double d_root = sqrt(discriminant);
results.push_back((-b + d_root) / (2 * a));
results.push_back((-b - d_root) / (2 * a));
}
return results;
}
int main() {
std::vector<double> results;
int a, b, c;
std::cin >> a >> b >> c;
results = calc_sqrt_results(a, b, c);
for (int i = 0; i < results.size(); ++i) {
std::cout << results[i] << ' ';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment