Introduction
Given integers b and a, find the smallest integer h, such that there exists a triangle of height h, base b, having an area of at least a.
Example Input Output
|
|
{: file="Input and Output” }
Explanation:
The task is to find the height of the triangle having base b = 2 and area a = 2. It turns out that the height is h = 2.
|
|
{: file="Input and Output” }
Explanation:
The task is to find the height of the triangle having base b = 17 and area a = 100. It turns out that the height is h = 12 and the triangle has an area of 102.
Process
The area of a triangle is given by the formula:
$$
A = \frac{1}{2}bh
$$
where b is the base and h is the height of the triangle. Manupulating the formula, we get:
$$
h = \frac{2A}{b}
$$
I knew I needed to use the ceiling function to get the smallest integer h that satisfies the condition. So I used the ceil() function from the math.h library to round the calculated value of the height up to the next integer. The formula can be changed to:
$$
h = \lceil\frac{2A}{b} \rceil
$$
We are able to use this formula to calculate the height of the triangle given the base and area.
Code
|
|
{: file="Minimum Height Triangle.c” }
Afterthoughts
Upon applying the manipulated formula, I was able to return the minimum height of the triangle, given the base and the area.
However, All challenges come with their hurdles and I was met with some when I did this.
- I initially used
intfor the height, but using a calculator to check the results, some results return a decimal value andintwill truncate the decimal value. So, I changed the type todoubleand usedceil()to round up the value. - I needed to typecast the
areaandtrianglebasetodoubleto avoid integer division as the result of the division will be an integer. - The return value of the function is an
int, so I had to typecast theheighttoint.
This solution passed all testcases.