...

View Full Version : C++ Help. CAN'T FIGURE IT OUT =(



odinzu
10-12-2007, 05:50 AM
I have been working on this simple program for hours now and still can't figure out my problem. Could someone please lend me some help? Thanks in advance.

Instructions:
Write a complete C++ program which will prompt the user for the three coefficients of a quadratic polynomial, and will then compute and display the two zeros of that polynomial. Use the quadratic formula.


#include <iostream.h>
#include <math.h> //Find the 2 zeros using the quadratic formula.
void main (void)
{
float a = 0.0;
float b = 0.0;
float c = 0.0;
float quadraticFormula;
float quadraticFormula2;

system("cls");

cout << "Enter 1st Coefficient: ";
cin >> a;
cout << "Enter 2nd Coefficient: ";
cin >> b;
cout << "Enter 3rd Coefficient: ";
cin >> c;

if (a != 0 & b*b-4*a*c > 0)


{
quadraticFormula = -b + sqrt(b*b-4*a*c) / (2* a); //Calculates the + of the zero
quadraticFormula2 = -b - sqrt(b*b-4*a*c) / (2 * a); //Calculates the - of the zero
cout << "First Zero (+): " << quadraticFormula << endl;
cout << "Second Zero (-): " << quadraticFormula2 << endl;

}
else

{
system("PAUSE");
cout << endl;
cout << "Error! Press Any Key to Terminate...";
}//end if

return;
} //end main

Gox
10-12-2007, 06:16 AM
Rather than forcing people to guess at the purpose of your program and why it doesn't work it'd be helpful if you told us what you're trying to do, whether you're getting compile errors or runtime errors etc.

Help us help you.

odinzu
10-12-2007, 06:40 AM
alright, updated. Is that ok?

odinzu
10-12-2007, 09:14 AM
I guess not =\

Gox
10-12-2007, 07:13 PM
You still haven't stated what the problem is. Is it not calculating properly? Is it not compiling? Is crashing when you run it?

At first glance, try checking your equations.
i.e. -b + sqrt((b*b)-(4*a*c)) / (2* a)
Some bracketing may help (like above) to make sure the order of computation is being done correctly.

Inigoesdr
10-12-2007, 09:05 PM
That does not compile in VC++ due to the following:

.h in the includes
math include had to be changed to cmath
missing namespace
missing second ampersand in if()

If you're compiling with GCC you might not need to do 1, or 2, though. Even after it compiles the formula doesn't seem to do anything, but I might not be entering numbers that you're going to be using. Follow Gox's suggestion for using parenthesis to be sure the equation is executed in the proper order.

odinzu
10-12-2007, 11:50 PM
You still haven't stated what the problem is. Is it not calculating properly? Is it not compiling? Is crashing when you run it?

At first glance, try checking your equations.
i.e. -b + sqrt((b*b)-(4*a*c)) / (2* a)
Some bracketing may help (like above) to make sure the order of computation is being done correctly.

It crashes when I run it. I think I have finally fixed it. The problem was my equation parenthesis and where I put my equation. I had to put it in the if statement because it would take the sqrt and give me a domain error to start off.

Gox
10-13-2007, 06:47 AM
Glad you got it working!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum