Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Arrow C++ Help. CAN'T FIGURE IT OUT =(

    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.

    Code:
    #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
    Last edited by odinzu; 10-12-2007 at 06:40 AM.

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    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.

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    alright, updated. Is that ok?

  • #4
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I guess not =\

  • #5
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    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.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    That does not compile in VC++ due to the following:
    1. .h in the includes
    2. math include had to be changed to cmath
    3. missing namespace
    4. 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.

  • #7
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Gox View Post
    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.

  • #8
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Glad you got it working!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •