Homework Assignment 5

Assigned: Monday,Feb 17, 1997.
Due: Monday, Feb 24, 1997.

Program:

  1. Heron.C: Given a triangle with edge lengths a, b, c, the area of the triangle is given by:
        area = square-root( s (s-a) (s-b) (s-c) )
    
    where s is the semi-perimeter:
        s = (a + b + c ) / 2
    
    This formula is attribued to Heron, a greek mathematician who lived in Alexandria around 300 AD. Several of his books are still in existence.

    Write a program which prompts for and inputs the 3 side lengths of a triangle, and uses Heron's formula to compute the area. Use the double varible type. Check for a negative double under the square-root. We know that a negative will make the square-root subroutine crash, but is it possible that the product would be negative? What would that mean about the data that the user entered? Make your error message as meaningful as possible. That is, to a user of your program, the message:

       Err ... s(s-a)(s-b)(s-c)<0. Have a nice day.
    
    might not be meaningful.

  2. Euclidean.C: In a previous homework assignment, you wrote a program to determine the relative sizes of two fractions. To accomplish this task, you needed to write down in computer language the steps well-known to every child of how to manipulate fractions. As simple as this task is, the statment of the algorithm, that is, the exact steps stated in mathematics which accomplish this task, can be a surprising challenge.

    In this program we will solve the problem of placing an integer in reduced form. Recall that a fraction i/j is in reduced form if there are no common divisors in i and j. So 2/5 is in reduced form, but 2/6 is not. The fraction 2/6 is equal to 1/3, and 1/3 is in reduced form. The algorithm for this problem was known 2500 year ago and is today called the Euclidean Algorithm. It is a considerable challenge for someone to come up with this algorithm all by themselves!

    Write the program Euclidean.C which accepts two integers called i and j and finds the greatest common divisor of the two. This is what they would each have to be divided by to place i/j in reduced form. Check that i is not negative and j is neither negative nor zero, and exit with a message to the user if this is not true. For example:

    Enter integer i: 85
    Enter integer j: 153
    The lowest common divisor is 17
    

    Euclid was another Greek mathematician, also from Alexandria. He lived 600 years before Heron, at 300 BC. He wrote The Elements, an introduction to geometry. It is said that Abraham Lincoln taught himself mathematics by reading Euclid's Elements.