| kgcsinister7 (13) | |
|
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 | |
|
|
|
| TheIdeasMan (1759) | |||
|
You are comparing a double value (sqrt produces a double) with an integer, which will never work. Google C++ floating point comparisons to see how to do it properly. Also rather than using the do loop, use a while loop with the end condition controlled by a bool variable:
Notice how I used the code tags - with the <> formatting button on the right. HTH | |||
|
Last edited on
|
|||
| kgcsinister7 (13) | |
| 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) | |
|
|
|
| TheIdeasMan (1759) | |
| Did you google and read some articles? | |
|
|
|
| Disch (8623) | |||||
Umm.. look at your parenthesis placement:
The square root of an expression will never be less than zero. You probably meant this:
| |||||
|
Last edited on
|
|||||
| kgcsinister7 (13) | |
| yeah i did couldnt find it for some reason | |
|
|
|
| kgcsinister7 (13) | |
| 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... | |
|
|
|
| kgcsinister7 (13) | |
| ok what i tried there worked completely fine. I had a big feeling it was something small like that that i had just overlooked.... | |
|
|
|
| TheIdeasMan (1759) | |||
Why is it that people don't seem to know how to use google?
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.HTH | |||
|
|
|||
| kgcsinister7 (13) | |
| 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 | |
|
|
|
| Disch (8623) | |
| Doh yeah I didn't actually see that. Blargg | |
|
|
|
| TheIdeasMan (1759) | ||
That was the helpful suggestion I was making. | ||
|
|
||