calcHypotenuse
function from
Midterm 1. You will also validate the input given to the calcHypotenuse
function. This function calculates the length of a right triangle from the
lengths of the two sides. You may find sections 5.5 (particularly Program 5.7),
6.7 (particularly Program 6.11) and 6.16 useful for completing this lab.
First, code the calcHypotenuse
function from Midterm 1. This
function returns a double. It takes two doubles as arguments, one for the
length of each side of the triangle. NEW: validate that the length
of each side is greater than 0. If either side is not greater than 0, return
0. Otherwise, calculate the length of the hypotenuse and return that.
Second, code the driver routine in main
. This is a do-while loop
which will ask the user for the lengths of the two sides, read in the lengths,
call calcHypotenuse
with those lengths, print the return value
from calcHypotenuse
and ask the user if they wish to enter data
for another triangle. The pseudocode for this driver routine is:
do prompt user for the lengths of both sides read data from keyboard into a and b call calcHypotenuse(a,b) and print return value to the screen ask the user if they wish to continue read from keyboard into response while response is Y or yThis driver routine is a loop that allows the user to keep calling the
calcHypotenuse
function with different values. This allows the
user to test calcHypotenuse
and be sure it behaves correctly
with all forms of input. For example, the user could give a negative length
for a and see if calcHypotenuse
returns 0 (as it should). Then
on the next iteration they could test if it calculates a 3-4-5 right triangle
appropriately. The do-while loop in main
only exits when the user
indicates that they do not wish to continue. Refer to Program 5.7 in the book
to see how to code such a do-while loop.