
January 11th, 2013, 01:15 AM
#1
Trying to use C++ to solve a math problem
a=40; b=30; c=10;
d=a^2b^2;f=2*c^2*d*sqrt(d^2/27+c^4);
g=d^3/27+2*c^4*d;h=d/3+(g+f)^(1/3)+(gf)^(1/3);
j=sqrt(c^2d+h);k=sqrt((2*c*d+2*c^3)/j+2*c^2dh);
m=(c+j+k)/2;sqrt(b^2m^2)
I had this riddle to solve and I know the answer to be
around ~ 12.31185723778668829963
What steps can i go about to get this to compute this problem without doing the work for this problem and other problems similar to this?
I'm not used to c++ and just need help with initialization and outputting the answer I got.
Its based off the very old riddle if anyone cares :
http://en.wikipedia.org/wiki/Crossed_ladders_problem

January 11th, 2013, 05:25 AM
#2
Re: Trying to use C++ to solve a math problem
You would first turn it from a set of flat mathematical equations/rules into an actual set of logic steps
How would you solve this without a computer and with only a pen and a sheet of paper. That's you're first step. Then you turn the "method" of how you solved it manually into a program.

January 11th, 2013, 07:46 AM
#3
Re: Trying to use C++ to solve a math problem
Originally Posted by OmeshPH12
I'm not used to c++ and just need help with initialization and outputting the answer I got.
Use variables and number literals of type double throughout.
Declare double variables like this,
double a=40.0;
or several on one line
double b=30.0, c=10.0;
All double literals should have a . in them otherwise things may go wrong. For example 1/3 may evaluate to 0 because you may get integer division whereas 1.0/3.0 will evaluate to 0.3333333333..... as it should.
To output a number you can do,
std::cout << a << std::endl;
or several like
std::cout << b << ", " << c << std::endl;
To calculate powers you need to use the pow function (or multiply the number several times with itself like say x^3 = x*x*x).
Make sure to include the necessary header files.
Last edited by nuzzle; January 11th, 2013 at 07:54 AM.

January 11th, 2013, 12:38 PM
#4
Re: Trying to use C++ to solve a math problem
Okay thank OReubens and nuzzle that helped alot.
The only header ill need should be <cmath> and <iostream> since its just calculations and outputting.

January 12th, 2013, 10:58 AM
#5
Re: Trying to use C++ to solve a math problem
Originally Posted by OmeshPH12
I had this riddle to solve and I know the answer to be
around ~ 12.31185723778668829963
Note that the precision of a double is only about 17 digits (including all digits before and after the decimal dot). Specifying more digits is superfluous.
Anyway what's that number above and how did you arrive at it?
Looking at the site you referred to you have these relations,
1/h = 1/A + 1/B
A^2 = b^2  w^2
B^2 = a^2  w^2
Solved for h you get this formula,
h = 1 / (1/sqrt(b^2  w^2) + 1/sqrt(a^2  w^2))
where a and b are the ladder lengths, h is the height above the alley floor up to where the ladders cross, and w is the alley width.
I suppose the number you posted is w, isn't it? When it's entered into the formula,
Code:
double a=40.0, b=30.0, w=12.311857237786688;
double h = 1.0 / (1.0/std::sqrt(b*bw*w) + 1.0/std::sqrt(a*aw*w));
std::cout << h << std::endl;
the result isn't 10 (as I guess it should be because c=10). So your solution doesn't seem correct.
Last edited by nuzzle; January 13th, 2013 at 01:28 AM.
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 is a CodeGuru survey question.
Featured
