Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Simple Quadratic Equation Calculator using Quadratic Formula

Writer Olivia Zamora

I was wondering if anyone could help me with this (beginner) problem. I am creating a very basic quadratic equation calculator. I have listed my code below, alongside comments (the ones at the top of the code snippet explain what i have to do). I've looked online at multiple solutions as well as tried myself but it seems that i keep getting incorrect x1 and x2 values. If anyone could guide me I would be more than happy. Cheers.

/*
create program to calculate x for quadratic equation. (a, b and c)
1) create function which prints out roots of quad equation.
2) throw exception if b2 - 4ac is less than 0.
3) call the function from main.
*/
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
double roots() { //return type, function name, parameters and body of function. double a = 0, b = 0, c = 0, x1, x2; double discriminant = (b * b) - 4 * a * c; cout << "Enter roots of quad equation (in order: a, b, c) " << endl; cin >> a >> b >> c; if (discriminant >= 0) { cout << "Your quadratic equation is: " << a << "x squared + " << b << "x + " << c << " = 0 " << endl; x1 = -b + sqrt(discriminant) / (2 * a); x2 = -b - sqrt(discriminant) / (2 * a); cout << "x1 = " << x1 << endl; cout << "x2 = " << x2 << endl; } else { cout << "Negative value returned from (b2 - 4ac), please try again! " << endl; exit(1); }
}
int main() { cout << "Quadratic Equation Calculator " << endl; roots(); return 0;
}
1

3 Answers

It's a simple error. You just placed the lines of code in the wrong order. This should fix it:

/*
create program to calculate x for quadratic equation. (a, b and c)
1) create function which prints out roots of quad equation.
2) throw exception if b2 - 4ac is less than 0.
3) call the function from main.
*/
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
double roots() { //return type, function name, parameters and body of function. double a = 0, b = 0, c = 0, x1, x2; cout << "Enter roots of quad equation (in order: a, b, c) " << endl; cin >> a >> b >> c; double discriminant = (b * b) - 4 * a * c; // restructed this line if (discriminant >= 0) { cout << "Your quadratic equation is: " << a << "x squared + " << b << "x + " << c << " = 0 " << endl; x1 = -b + sqrt(discriminant) / (2 * a); x2 = -b - sqrt(discriminant) / (2 * a); cout << "x1 = " << x1 << endl; cout << "x2 = " << x2 << endl; } else { cout << "Negative value returned from (b2 - 4ac), please try again! " << endl; exit(1); }
}
int main() { cout << "Quadratic Equation Calculator " << endl; roots(); return 0;
}
5

The issue you're having with the discriminant might teach you to write a function to calculate the discriminant, something like:

double f_discriminant(double a,b,c){ return b*b-4*a*c;
}

Like this, your piece of code becomes (based on the improvements by PranavaGande):

...
double roots() { //return type, function name, parameters and body of function. double a = 0, b = 0, c = 0, x1, x2; cout << "Enter roots of quad equation (in order: a, b, c) " << endl; cin >> a >> b >> c; double discriminant = f_discriminant(a, b, c); // restructed this line
...

The advantage here is that, if ever you need to calculate the discriminant again, you might just use the function you have written for it. (For this example it's obvious, but you will encounter functions which are harder to develop)

1

The problem is the line that computes the discriminant:

double discriminant = (b * b) - 4 * a * c;

This line comes before you assign non-zero values to a, b and c. At the time this executes all those variables are equal to zero and so the discriminant is also zero. To calculate the discriminant using the values from the user, that line needs to go after this line:

cin >> a >> b >> c;

The reason is that the arithmetic calculation is performed just once, immediately on the line it's written; after that, the = operator (assignment) puts that result into the variable. After that, the variable has no memory of how the value got there and no way to "refresh" itself using new values in the old expression. You could approximate that by making discriminant a function of a, b and c.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy