I'm solving this problem:
https://open.kattis.com/problems/ladder
Problem Summary:
- You need to buy a ladder so you can fix some leaks in your roof.
- The ladder needs to reach h centimeters high.
- The ladder can be at an angle of <= v degrees from the ground.
- How long must this ladder be?
Input:
- h (1-10,000 inclusive) and v (1-89 inclusive)
- Note that I call these wallHeight and angle in my program.
Output:
- Print the min possible length of the ladder in cm rounded UP
to the nearest int.
http://www.cplusplus.com/reference/cmath/sin/
-------------------
So, I don't understand what is wrong with my logic which I've been basing off this site:
https://www.mathsisfun.com/algebra/trig-finding-side-right-triangle.html
- Especially the "Radio Mast" example.
Algorithm Steps: (All completed [aside from #6 using assert()] but program is not working)
1. Ask for user intput (int).
- h (wallHeight)
- v (degrees)
2. We know the height of the wall and the angle/degree of the ground and ladder.
- Think of it as a triangle:
|\
opposite | \ hypotenuse
side | \ (length of ladder. The unknown.)
|---\
^ angle we know.
|
- So, we know the angle and the opposite side and we need to find the
hypotenuse.
- We will use SOHCAHTOA to do this.
- Steps:
https://www.mathsisfun.com/algebra/trig-finding-side-right-triangle.html
* You can use SOH to solve this:
(Below shows the steps to reorganize formula to get it to
= hypotenuse)
sin(angle) = opposite / hypotenuse
(sin(angle)) = opposite / hypotenuse
(sin(angle)) * hypotenuse = opposite
hypotenuse = opposite / (sin(angle)) (FINAL EQUATION. Now just have to fill in)
3. Round up using std::ceil()
4. Print result.
5. Set up kattis automatic testing.
6. Set up at least 3 assert test cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <cmath> // std::ceil(), std::sin()
#include <cstring>
#include <cassert>
int sohcahtoa(const double opposite, const double angle);
void test();
int main(int argc, char* argv[]) {
if (argc > 1 && strncmp(argv[1], "test", 4) == 0) {
test();
}
else {
double wallHeight = 0, angle = 0;
std::cin >> wallHeight >> angle;
std::cout << sohcahtoa(wallHeight, angle) << '\n';
}
return 0;
}
int sohcahtoa(const double opposite, const double angle) {
// hypotenuse = opposite / (sin(angle))
// ladderHeight = wallHeight / (sin(angle))
return ( std::ceil( opposite / (std::sin(angle)) ) );
// ^ returns hypotenuse (ladderLength) rounded up to nearest whole number.
}
void test() {
//double wallHeight = 70.0, angle = 68.0;
//assert(sohcahtoa(wallHeight, angle) == 75.5); // Work in progress.
std::cout << "All test cases passed!\n";
}
|
Example Input / Output:
500 70 // input
647 // output |
What it should Input / Output:
500 70 // input
533 // output |
Help would be greatly appreciated.
Thanks!