Challenge 4

Description

The purpose of this challenge is to employ the use of basic functions. This challenge will require writing several functions to calculate various attributes of a right-angled triangle. A right-angled triangle is a triangle that has a 90-degree angle between two adjacent sides. The third side opposite the right angle is called the hypotenuse.

Requirements

  1. Create the following prototypes
    double get_double(string prompt);
    double sroot(double number);
    double calc_hypotenuse(double side1, double side2);
    double calc_area(double side1, double side2);
    double calc_perimeter(double side1, double side2);
    
  2. Create definition stubs for each of the above function prototypes. Each of the functions can be set to return 0. Stubs simply allow the functions to be called and compiled without it having to be completely written out. Specifically, for this exercise, you can type out main() as given below in its entirety. The code will compile as long as the stubs are written out.
  3. For each of the functions below, read each description and replace the stub definition with each specific description as given.
  4. get_double(string prompt) will pass in a string parameter. In get_double(), ask the user to enter a number, but only allow numbers greater than zero (use a do-while loop). prompt will be displayed to the user as the message to receive user input. The return value will be always be a positive number.
  5. sroot(double) takes one double parameter. This function will calculate the square root of the parameter by calling the pow() function. DO NOT USE the sqrt() function to calculate the square root. Use this sroot() function that you created. You will need to include <cmath> to use the pow() function. Remember that raising a number to 0.5 is equivalent to the square root of the number. The sroot() function in this case is simply “wrapping” the pow() function. Wrapping takes an existing function and emulates similar behavior, but usually changes the parameters needed.
  6. calc_hypotenuse(double, double) takes in 2 parameters which represent two sides of the triangle. This function will return a double value. The return value will be the result of the Pythagorean Theorem: hypotenuse = sroot(pow(side1, 2.0) + pow(side2, 2.0))
    a2 = b2 + c2
    
  7. calc_perimeter(double, double) takes two parameters representing two sides. In the body of calc_perimeter(), you will have to call calc_hypotenuse() to determine the length of the third side and calculate the perimeter. The result of the calc_perimeter() function is the perimeter value.
  8. calc_area(double, double) takes two parameters representing two sides. The result of the calc_area() function is the area. Remember that the formula for area of a triangle is Area = 0.5 * side1 * side2
  9. Copy main() below as it is. You don’t need to change it.

Sample Main

// your function prototypes go here

int main()
{
     double side1, side2;
     double perimeter, area;

     side1 = get_double("Enter the length of one side");
     side2 = get_double("Enter the length of the adjacent side");

     perimeter = calc_perimeter(side1, side2);
     area = calc_area(side1, side2);

     cout << “A right triangle with sides “ << side1 << “ and “ << side2
       << “ has a perimiter of “ << perimeter << “ and an area of “ <<
       area << “. The length of the third side is “ << 
       calc_hypotenuse(side1, side2) << endl;
       
  return 0;
}

// your function definitions go here

Sample Interaction / Output

Enter the length of one side: 3
Enter the length of the adjacent side: 4

A right triangle with sides 3 and 4 has a perimeter of 12 and an area of 6. The length of the third side is 5

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0004

Print Requirements