Skip to content

Instantly share code, notes, and snippets.

@Journeyman1337
Last active March 25, 2021 19:08
Show Gist options
  • Save Journeyman1337/cf994f34f86c82f9ec023cb1d9722eac to your computer and use it in GitHub Desktop.
Save Journeyman1337/cf994f34f86c82f9ec023cb1d9722eac to your computer and use it in GitHub Desktop.
Messing around with math...
#include <cmath>
#include <iostream>
int main()
{
double a, b, c;
std::cout << "Quadratic formula solver.\nGive A:" << std::endl;
std::cin >> a;
std::cout << "Give B:" << std::endl;
std::cin >> b;
std::cout << "Give C:" << std::endl;
std::cin >> c;
double descriminant = (b * b) - (4 * a * c);
if (descriminant < 0)
{
std::cout << "The answers are imaginary." << std::endl;
double vertex_x = (-b) / (2 * a);
double vertex_y = (a * (vertex_x * vertex_x)) + (b * vertex_x) + c;
const char* direction = (a>0) ? "upward." : "downward.";
std::cout << "The direction is " << direction << std::endl;
std::cout << "The vertex is (" << vertex_x << ", " << vertex_y << ")" << std::endl;
std::cout << "The y-intercept is (0," << c << ")" << std::endl;
}
else if (descriminant == 0)
{
std::cout << "There is one real answer." << std::endl;
double answer = (-b) / (2 * a);
const char* direction = (a>0) ? "upward." : "downward.";
std::cout << "The answer is: " << answer << std::endl;
std::cout << "The direction is " << direction << std::endl;
std::cout << "The vertex is (" << answer << ", 0)" << std::endl;
std::cout << "The y-intercept is (0," << c << ")" << std::endl;
}
else
{
std::cout << "There are two real answers." << std::endl;
double answer_a = (-b - sqrt(descriminant)) / (2 * a);
double answer_b = (-b + sqrt(descriminant)) / (2 * a);
double vertex_x = (-b) / (2 * a);
double vertex_y = (a * (vertex_x * vertex_x)) + (b * vertex_x) + c;
const char* direction = (a>0) ? "upward." : "downward.";
std::cout << "The answers are: { " << answer_a << ", " << answer_b << "}" << std::endl;
std::cout << "The direction is " << direction << std::endl;
std::cout << "The vertex is (" << vertex_x << ", " << vertex_y << ")" << std::endl;
std::cout << "The y-intercept is (0," << c << ")" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment