r/tinycode • u/Elbynerual • Jun 19 '18
I need help shortening my C++ homework...
If I can write this using only one loop (while, do-while, or for), I get extra credit!
I need to divide 1 by 1, and then by 2 and so on, incrementally. 1/2, 1/3, 1/4, etc... up to 100,000,000.
Each time the math is done it has to display the answer as a double AND again as a float.
When it hits 100,000,000, it has to do the math and double/float results back down again all the way to zero.
0
Upvotes
16
u/tamat Jun 19 '18
it sounds like an exercise you have been requested by your teacher and you do not want to do
4
u/mensink Jun 19 '18
So you're really too lazy to figure this out for yourself?
2
u/SGMN Jun 19 '18
Loop to 200,000,000 but when the number is >100,000,000 do (200,000,000 - n)?
2
u/jtsiomb Jun 19 '18
You don't have to get extra credit. Do the homework to the extend of your knowledge and understanding, and then ask your teacher to show you how it can be done with one loop.
1
u/turtlepot Jun 19 '18
There's a pretty straightforward approach using recursion, if you have studied that concept yet.