CSCI 297 Practice 1

You will write a FORTRAN program that will read three numbers from a line. The three numbers are the lengths of the three sides of a triangle. Compute the perimeter and the area of the triangle and print the results.


What to do

Start with the basic outline, from "PROGRAM PRAC1 at the top to "END PROGRAM PRAC1" at the bottom.

You need the line "IMPLICIT NONE".

You need several REAL variables.

You need to ask the user to provide three REAL values A, B, C, as in:

Please type three real numbers, the lengths of the sides of a triangle.

You need to read the input values, as in "READ *, A, B, C".

You need to do some arithmetic. At some point you will need to find the square root of a number. To do this you can us the SQRT function, as in:

        X = SQRT(Y)

You will eventually need to print the results on lines something like this:

 The lengths of the sides are:
 A =    40.0000000    
 B =    50.0000000    
 C =    60.0000000    
 The perimeter is    150.000000    
 The area is         992.156738

(Notice the effect of the round-off errors in the Area calculation.)


Notes

We are not going to worry about whether this is a possible triangle. You may assume the three numbers are non-negative and do make sense as the lengths of the sides of a triangle.

To calculate the perimeter and area of the triangle, you can use these formulas. You will need an extra variable S.

     Perimeter = A + B + C
     S = Perimeter / 2.0
     Area = SQRT(S * (S - A) * (S - B) * (S - C))

The formula used here for the area is "Heron's Formula", named for Hero of Alexandria, a 1st Century scholar.


Save your program with the name "prac1.f90".

You now need to compile, link and run your program.

When you run the program, it will ask you to provide numbers. Try using "4.0 4.0 4.0". Try running it again using other triples of numbers such as "0.0 20.0 20.0", "30.0 40.0 50.0" or "27.0 10.0 27.0".

Once the program is running correctly, you can obtain the output as a file with "output redirection". Use this command at the prompt:

          prac1 > prac1.out

The file "prac1.out" will be created in your current folder.