
July 19th, 2011, 11:14 PM
#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/
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 a Codeguru.com survey!
OnDemand Webinars (sponsored)
