I'm trying out this program to solve quadratics for you, and I got it all to work. But i realized I needed to add some If statements for when a certain part of it is positive or negative. But when I added them in, instead of going to one of the, it skipped them both completely and continued. Here's what I have:
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
int a;
int b;
int c;
char redo;
int main()
{
do
{
system("cls");
cout << "Please enter your 'a' term (ax^2 + bx + c =0)" << endl;
cin >> a;
cout << "Please enter your 'b' term." << endl;
cin >> b;
cout << "Please enter your 'c' term." << endl;
cin >> c;
if(sqrt((b * b) -4 * a * c) < 0)
{
cout << "No x intercepts" << endl;
}
else if(sqrt((b * b) - 4 * a * c) > 0)
{
cout << "x=" << (-b + sqrt((b * b) - 4 * a * c)) / (2 * a) << endl;
cout << "x=" << (-b - sqrt((b * b) - 4 * a * c)) / (2 * a) << endl << endl;
}
cout << "Would you like to try again?(Y/N)" << endl;
cin >> redo;
}
while(redo=='y'||redo=='Y');
return 0;
}
I've never done anything with ifs like this before I've only done simple ones not a big expression so I just need some help. Thanks
#include <locale>
using std::cin;
using std::cout;
using std::endl;
bool Quit = false;
char redo;
while(!Quit) {
//your code here
cout << "Would you like to try again?(Y/N)" << endl;
cin >> redo;
redo = std::toupper(redo);
//user wants to quit
if (redo == 'Y')
Quit = true;
} //end of while loop
Notice how I used the code tags - with the <> formatting button on the right.
I get what your saying and it makes sense, but I'm not quite finding out how to make it work. I need that expression in an if statement saying if its positive or negative (if its possible)
that is what i mean but it still isn't working i'm thinking it might work if i just put b^2 - 4ac in there because thats actually what needs to be positive or negative...
As Disch was alluding to you approach is wrong because sqrt always produces a positive number .
Why not just work out the 2 roots with a function that takes the determinant -> sqrt((b * b) -4 * a * c) as a parameter. Call the function twice with +ve & -ve versions of the determinant.
However @Disch:
if( ( sqrt(b * b) -4 * a * c ) < 0)
is not how the quadratic equation works, and sqrt(b * b ) is just b.
yes i realized that sqrt(b * b) is just b but i was just kind of sticking with the actual equation (x = (-b +- sqrt(b^2 - 4ac)) / (2a)).... but yes i saw that article but didnt see how it really helped
Why not just work out the 2 roots with a function that takes the determinant -> sqrt((b * b) -4 * a * c) as a parameter. Call the function twice with +ve & -ve versions of the determinant.