CIS 150 Programming Project: Functions 2

Objectives

  • Create user-defined functions
  • Use user-defined functions
  • Format output

General program requirements

The challenge in this assignment is to write a program that asks the user for the lengths of the three sides of a triangle, determine whether the input is valid, and if it is, display the area of the triangle. There are three required functions you have to create to help streamline the mainline code.

  1. Include documentation comments at the start of the file.
  2. Follow the course style guidelines including indentation, naming conventions, spaces instead of tabs, etc.

Requirements for getDouble function

  1. Accept three arguments: a string prompt, a double minimum value, and a double maximum value.
  2. Prompt the user to enter a value.
  3. Get a double value from the user.
  4. Display an error message if the value the user entered was out of range.
  5. Continue prompting for input and accepting input until the input is valid. That means most of the above steps must be in a loop.
  6. Once a valid double has been entered, return that double value.

Requirements for isValidTriangle function

  1. Accept three arguments. All three are doubles representing the three lengths of the sides of a triangle.
  2. Return true if the sides represent a valid triangle, and false otherwise.
  3. A triangle is valid if the two smallest sides add up to more than the largest side. It can be a little tricky to figure out how to test this. It is suggested that you first find the longest side. Use a variable to hold the value of the longest side. Start by assuming side 1 is the longest and set the variable to that side's length. Then compare that value to each of the other sides and change the value if needed. When you are done, the "longest" variable will hold the length of the longest side. Then you can just check to see if twice the longest side is less than the sum of all the sides added together. If it is, the triangle is valid. Otherwise, the triangle is invalid.
  4. This function must not display anything.

Requirements for calcArea function

  1. Accept three arguments (all doubles representing the three side lengths).
  2. Use the isValidTriangle function to determine if the triangle is valid.
  3. If the triangle is not valid, then return -1.
  4. If the triangle is valid then return the area of the triangle.
  5. The area of the triangle can be calculated using Heron's formula:
    area = (s (s - a) (s - b) (s - c)) ^ 0.5
    where ^ 0.5 indicates square root, and
    s = half the perimeter of the triangle, and
    a, b, and c are the three side lengths
    Note: The formula above is presented as math notation - NOT program code. Multiplication is sometimes not explicitly shown. Multiplication must be explicitly shown in program code.
    Note: The perimeter is the sum of the sides (a + b + c)
    Note: x ^ 0.5 means the square root of x
    You can get the square root of x in C++ using the cmath library function: sqrt(x)
  6. This function must not display anything.

Requirements for main function

  1. Display an introductory message: This program calculates the area of a triangle
  2. Use the getDouble function to get the lengths (between 1 and 100) of the three sides from the user. This will require three calls to getDouble.
  3. Use the calcArea function to get the area of the triangle.
  4. If calcArea returns a negative value for the area, then that indicates the triangle is invalid. If the triangle is invalid, then display an error message; otherwise display the area with one digit after the decimal point.

Rubric

  1. 3 points for proper documentation comments and for correctly following coding conventions (indentation, naming rules, etc.)
  2. 4 points for correct operation of the main function
  3. 1 point for correct output formatting of the area
  4. 7 points for correct operation of getDouble function
  5. 8 points for correct operation of isValidTriangle function
  6. 7 points for correct operation of calcArea function

Sample runs

*** first sample run *** This program calculates the area of a triangle Enter length of first side (1-100): 3 Enter length of second side (1-100): 4 Enter length of third side (1-100): 20 That is not a valid triangle *** second sample run *** This program calculates the area of a triangle Enter length of first side (1-100): 3 Enter length of second side (1-100): 4 Enter length of third side (1-100): 5 The area of the triangle is 6.0 *** third sample run *** This program calculates the area of a triangle Enter length of first side (1-100): 5 Enter length of second side (1-100): 4 Enter length of third side (1-100): 3 The area of the triangle is 6.0 *** fourth sample run *** This program calculates the area of a triangle Enter length of first side (1-100): -3 Error: Number below minimum value Enter length of first side (1-100): 300 Error: Number above maximum value Enter length of first side (1-100): 3 Enter length of second side (1-100): 5 Enter length of third side (1-100): 4 The area of the triangle is 6.0 *** fifth sample run *** This program calculates the area of a triangle Enter length of first side (1-100): 10 Enter length of second side (1-100): 30 Enter length of third side (1-100): 15 That is not a valid triangle *** sixth sample run *** This program calculates the area of a triangle Enter length of first side (1-100): 10 Enter length of second side (1-100): 20 Enter length of third side (1-100): 30 That is not a valid triangle *** seventh sample run *** This program calculates the area of a triangle Enter length of first side (1-100): 10 Enter length of second side (1-100): 21 Enter length of third side (1-100): 30 The area of the triangle is 54.5