
July 20th, 2011, 12:14 AM
#1
Quadraatic Formula Program Problem
Okay, I'm developing a program that will solve quadratic equations for users. I have them plug in values for a, b, and c in the following formula: ax^2 + bx +c. This formula probably looks familiar to you guys from highschool. Anyways, for some reason my code outputs completely inaccurate results. Here is my code:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
//variable declarations
int aval;
int bval;
int cval;
int root;
int preroot;
double sol1;
double sol2;
//a user inputs an integer for each of the specified values
cout<< "a = ";
cin >> aval;
cout<<"b = ";
cin>>bval;
cout<<"c = ";
cin>>cval;
//number crunching
sol1 = (bval + sqrt(bval*bval  4*aval*cval))/(2*aval);
sol2 = (bval  sqrt(bval*bval  4*aval*cval))/(2*aval);
//output
cout<<"Your solutions are: x = ";
cout<<sol1;
cout<<" or x = ";
cout<<sol2<< endl;
system("PAUSE");
return 0;
}
For example, the equation 4x^2 + 5x + 1 comes up with x = 4 and x = 16 when the answers are clearly 1 and 1/4. I'm probably making some really stupid mistake or maybe i messed up the formula but i can't seem to figure it out! Anyways, some help would be nice. And please be gentle i'm new to this. Thanks!
PS. I'm plugging the user input into the quadratic formula just in case you were confused. The quadratic formula can be found here: http://mste.illinois.edu/exner/ncsa/quad/

July 20th, 2011, 01:23 AM
#2
Re: Quadraatic Formula Program Problem
A possible problem is that ints don't divide very well. E.g. 1 divided by 2 will give you zero (0.5 with the .5 chopped off).
Try it with floats as the input and output.
Also a good principle when doing any maths in C++ is for the answer and every input to all be the same datatype, otherwise you will often get unexpected results.

July 20th, 2011, 02:35 AM
#3
Re: Quadraatic Formula Program Problem
As has been suggested the problem is integer division. Switch to doubles throughout.
And note that 2 is an integer whereas 2.0 is a double.
Another problem is that you must make sure that sqrt isn't called with a negative parameter. This happens when the quadratic formula has imaginary solutions.
Last edited by nuzzle; July 20th, 2011 at 08:10 AM.

July 20th, 2011, 02:37 PM
#4
Re: Quadraatic Formula Program Problem
Thank you very much. I changed everything to doubles including the non variable values as well and i got the correct answers. This should make math homework go by a lot faster Thanks to both of you! Cheers!
MiniatureBeast

July 20th, 2011, 02:43 PM
#5
Re: Quadraatic Formula Program Problem
Originally Posted by MiniatureBeast
This should make math homework go by a lot faster
Don't forget that some teachers will take off points if you don't write down every step.

July 20th, 2011, 04:27 PM
#6
Re: Quadraatic Formula Program Problem
Originally Posted by Lindley
Don't forget that some teachers will take off points if you don't write down every step.
And, if you miss something in one step, but carry the issue forward, some teachers will give you partial credit (i.e. not penalize you full points because you wrote a '8' down instead of a '7').
Viggy

July 20th, 2011, 05:34 PM
#7
Re: Quadraatic Formula Program Problem
Originally Posted by MiniatureBeast
the non variable values
They're called literals.
Good luck.

July 21st, 2011, 01:20 AM
#8
Re: Quadraatic Formula Program Problem
Honestly I didn't read much of what people said, I just read part of your first post, BUT
there is something I'd like to say
since sqrt(bval*bval  4*aval*cval))/(2*aval) is needed to find x AND y
you should do something like
d = sqrt(bval*bval  4*aval*cval))/(2*aval)
x = bval + d
y = bval  d
just copy pasted parts of your code, didn't check them for syntax / semantical errors
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
