Lab 4 - void Functions

In this lab, you will be writing a function that computes the hypotenuse of a triangle. The function will be called from a do-while loop in main(). You may find sections 5.5 (Program 5.7 in particular) and 6.2 of the book useful for completing this lab.

Complete the following program and email the source code to my Helios account. Name your program lab4.cpp

First, code a void function called compute_hypotenuse. The body of this function will ask the user for the lengths of the two sides of the triangle, verify that the lengths are positive numbers and then compute the length of the hypotenuse. Recall from the Pythagorean theorem that c2 = a2 + b2 where a and b are the lengths of the sides of the triangle and c is the length of the hypotenuse. So to calculate the length of the hypotenuse, you'll need to take the square root of a2 + b2. Use the cmath library to do so (refer to section 3.11 of the book). Once you have calculated the length of the hypotenuse, print that to the screen.

Second, code the main function. There will be a do-while loop in main that will call compute_hypotenuse and then ask the user if they wish to calculate values for another triangle. If the user enters 'n' or 'N' in response, the do-while loop will exit. Refer to Program 5.7 in the book for how to code a do-while loop that prompts the user to continue.

Note: The do-while loop should ONLY exit when the user hits 'n' or 'N'. If they accidently hit another letter, such as 'u', the loop should continue. This is different than how Program 5.7 works, where it continues only if the user hits 'y' or 'Y'.

The code should resemble the following when run:

Enter the length of side a of the triangle: 3
Enter the length of side b of the triangle: 4
The length of the hypotenuse is 5.
Do you wish to enter data for another triangle? (Y/n) y

Enter the length of side a of the triangle: 0
The length must be positive. Please enter a new length: -1
The length must be positive. Please enter a new length: 6
Enter the length of side b of the triangle: 9
The length of the hypotenuse is 10.8167.
Do you wish to enter data for another triangle? (Y/n) u

Enter the length of side a of the triangle: 2
Enter the length of side b of the triangle: 3
The length of the hypotenuse is 3.60555.
Do you wish to enter data for another triangle? (Y/n) n