Lab 4 - Functions

This lab covers topics from Chapter 4. The following sample programs may be helpful: Complete the following programs and email both files to my Helios account. Name your programs lab4_1.cpp and lab4_2.cpp.
  1. Write a program that takes the lengths of two sides of a right triangle and outputs the length of the hypotenuse. Remember the Pythagorean theorem states that if a and b are the sides of the triangle and c is the hypotenuse, then the relationship between the three is a2 + b2 = c2. So the length of the hypotenuse is the square root of a2 + b2. This program will use predefined functions from cmath. The output should appear as follows:
    Enter the lengths of the two sides of the triangle: 5 6
    The length of the hypotenuse is 7.81025.
    
    Another example:
    Enter the lengths of the two sides of the triangle: 3 4
    The length of the hypotenuse is 5.
    
  2. Take the following code and add the function definition to compute the average of four integers. The function declaration and main function are already complete.
    #include <iostream>
    using namespace std;
    
    // Average takes 4 integers and returns the average
    double average(int a, int b, int c, int d);
    
    int main()
    {
      int a, b, c, d;
      double avg;
    
      cout << "Enter 4 integers: ";
      cin >> a >> b >> c >> d;
      avg = average(a, b, c, d);
      cout << "The average is " << avg << ".\n";
      return 0;
    }
    
    When logged into Helios, you can copy a file containing this code by doing
    cp /usr/users/mdanfor/public_html/cs221-f06/lab4_2.cpp .
    You can also view the file here: lab4_2.cpp