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 to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    abc formule c++ code

    hi coders
    well i just started coding in c/c++ and having some problems
    i want to make a program which will solve the abc-formule
    the normal abc formule is -b(-or+)√b^2-4*a*c/2a
    but i cant get square root to work this is what i have till now
    plz help me im kinda noob at coding :P
    i changed the root square too V

    # include <iostream.h>
    # include <stdio.h>
    # include <math.h>
    main()
    {
    double a, b, c, d;
    double ant1, ant2;
    cout << "please enter the value of a here:9";
    cin >> a;
    cout << " please enter the value of b here:";
    cin >> b;
    cout << " please enter the value of c here:: ";
    cin >> c;
    d = ( b * b ) - ( 4 * ( a * c ) );
    ant1= ( - b ) - ( Vd ) / ( 2 * a );
    ant1= ( - b ) + ( Vd ) / ( 2 * a );
    cout << ant1 ant2;

    }
    plz help me
    thanx in advance

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,855
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Have a look at the use of sqrt() function at http://www.cplusplus.com/reference/clibrary/cmath/sqrt/
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    magreet3 (01-10-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Have a look at the use of sqrt() function at http://www.cplusplus.com/reference/clibrary/cmath/sqrt/
    yeah i saw that page but i still dont know how to use it,
    is it somthing like this:

    # include <iostream.h>
    # include <stdio.h>
    # include <math.h>
    main()
    {
    double a, b, c, d;
    double ant1, ant2;
    double Vd
    cout << "please enter the value of a here:";
    cin >> a;
    cout << " please enter the value of b here:";
    cin >> b;
    cout << " please enter the value of c here:: ";
    cin >> c;
    d = ( b * b ) - ( 4 * ( a * c ) );
    param = d;
    result = sqrt (Vd);

    ant1 = ( - b ) - ( Vd ) / ( 2 * a );
    ant1 = ( - b ) + ( Vd ) / ( 2 * a );
    cout << ant1, ant2;
    }

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Close but you are performing the square root on the variable Vd but you never assign a value to the variable first. And you haven't declared the result variable.

    Based on your other code you'd want to change it to:
    Code:
    Vd = sqrt (d);
    OracleGuy

  • Users who have thanked oracleguy for this post:

    magreet3 (01-10-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    alright ty so now its all runing and stuff without errors but when i run it it shows the answers for like a milisecond and then leaves the program, if i use the code to wait till i hit a certain key, it do waits till i hit the key but isnt showing the result anymore
    whats the good code to get the effect im looking for?
    this is what i have now

    # include <iostream.h>
    # include <stdio.h>
    # include <math.h>
    main()
    {
    double a, b, c, d;
    double ant1, ant2;
    cout << "please enter the value of a here:";
    cin >> a;
    cout << " please enter the value of b here:";
    cin >> b;
    cout << " please enter the value of c here:";
    cin >> c;
    d = ( b * b ) - ( 4 * ( a * c ) );
    ant1 = ( - b ) - ( sqrt(d )) / ( 2 * a );
    ant1 = ( - b ) + ( sqrt(d )) / ( 2 * a );
    cout << ant1, ant2;
    }

  • #6
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    okay now i have given it a delay at the end but its still not doing its job
    it gives a hole different answer then it should be ):
    can someone with some abc formule experience at the code for me


    # include <iostream.h>
    # include <stdio.h>
    # include <math.h>
    # include <windows.h>


    main()
    {
    double a, b, c, d;
    double ant1, ant2;
    cout << "please enter the value of a here:";
    cin >> a;
    cout << " please enter the value of b here:";
    cin >> b;
    cout << " please enter the value of c here:";
    cin >> c;
    d = ( b * b ) - ( 4 * ( a * c ) );
    ant1 = ( - b ) - ( sqrt(d )) / ( 2 * a );
    ant2 = ( - b ) + ( sqrt(d )) / ( 2 * a );
    cout << ant1;
    cout << ant2;
    Sleep(10000);

    }

  • #7
    New Coder
    Join Date
    Nov 2011
    Location
    Pakistan
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Square root issue.....

    d=b*b-4ac;
    double sqrtd; // this is variable which will save the calculation of d after taking Sqr Root
    sqrtd=sqrt(d);


    i think so enough....
    U r already close enough.....

  • #8
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i dont think thats the problem cause its working now i saw the problem myself
    when doing ant1 = ( - b ) - ( sqrt(d )) / ( 2 * a );
    it first divided (2*a) with the sqrt and then removed it from -b well its should do it the other way, first -b- sqrt and then divided by (2 * a)
    when i saw it i was rlly @facepalm, it was so simple
    but heres my final code

    # include <iostream.h>
    # include <stdio.h>
    # include <math.h>
    # include <windows.h>


    main()

    {
    double a, b, c, d, e;
    double ant1, ant2, ant3, ant4;
    cout << " please enter the value of A here:";
    cin >> a;
    cout << " please enter the value of B here:";
    cin >> b;
    cout << " please enter the value of C here:";
    cin >> c;
    d = ( b * b ) - ( 4 * ( a * c ) );
    ant3 = ( - b ) - ( sqrt(d ));
    ant4 = ( - b ) + ( sqrt(d ));
    ant1 = ant3 / ( 2 * a );
    ant2 = ant4 / ( 2 * a );
    cout << ant1;
    cout << " and ";
    cout << ant2;
    cout << " and now thanks brent :P";
    cout << " D was:";
    cout << d;
    Sleep(20000);
    }

    thanks anybody who helped me (:


  •  

    Posting Permissions

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