PDA

View Full Version : abc formule c++ code

magreet3
01-10-2012, 03:14 PM
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

abduraooft
01-10-2012, 03:24 PM
Have a look at the use of sqrt() function at http://www.cplusplus.com/reference/clibrary/cmath/sqrt/

magreet3
01-10-2012, 05:25 PM
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;
}

oracleguy
01-10-2012, 05:38 PM
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:

Vd = sqrt (d);

magreet3
01-10-2012, 06:42 PM
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;
}

magreet3
01-10-2012, 08:01 PM
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);

}

01-10-2012, 08:25 PM
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....

magreet3
01-10-2012, 09:20 PM
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 (: