CSCI 297 Assignment D
Subroutines, Functions and a File
This program is concerned with geometry. The program will read a file of information about triangles and classify them as equilateral, isosceles, etc. It will also calculate the perimeter and area of each triangle.
To do this, we will use functions and a subroutine.
We will read the data from a file rather than use input redirection.
We will open a new file and write our output into it rather than use output redirection.
What does the data look like?
Each line in the data contains 3 REAL numbers A, B and C in F5.2 format, separated by 4 spaces between numbers. These are the lengths of the sides of a triangle. For instance:
30.00 40.00 50.00
14.80 14.80 14.80
02.00 01.41 01.41
00.00 07.50 0.750
11.00 02.00 04.50
Copy the data file into your directory. You can find it here.
What processing do we need?
Your program will read the data, one line at a time, and determine whether the triangle is POSSIBLE.
If it is not a POSSIBLE triangle, print a line containing the values of the input values A, B and C, and the word IMPOSSIBLE.
If it is a POSSIBLE triangle, decide whether it has each of these properties:
COLLAPSED -- at least one of the three sides is 0
RIGHT -- one of the angles is a right angle
EQUILATERAL -- the three sides are equal
ISOCELES -- two of the three sides are equal
SCALENE -- no two of the three sides are equal
For each triangle that is POSSIBLE, calculate the perimeter and the area.
Notice that for each triangle you may be printing several lines of data.
Properties of triangles
To be the sides of a POSSIBLE triangle, the three lengths A, B and C must satisfy all three of these inequalities:
A + B >= C
B + C >= A
C + A >= B
To be the sides of a RIGHT triangle, the three lengths A, B and must satisfy one of the following three equations:
A**2 + B**2 = C**2
B**2 + C**2 = A**2
C**2 + A**2 = B**2
The PERIMETER of a triangle with sides A, B, and C is:
P = A + B + C
To find the AREA of the triangle, we can use the following formula: S = (A + B + C) / 2.0
Area = square root of (S * (S - A) * (S - B) * (S - C))
Functions and Subroutines
Your program should use a REAL-valued function to calculate the perimeter of the triangle. It has three arguments: A, B and C.
Your program should use a REAL-valued function to calculate the area of the triangle. It has three arguments: A, B and C.
Your program should use LOGICAL-valued functions to check on each of the kinds of triangles listed above. Each of these has three arguments: A, B and C.
Use a subroutine to print the output. Its arguments may include a line counter, a page counter and the values to be printed. (You will need to decide what arguments it needs. This is the most elaborate of all the subprograms needed.)
You will also need a few library functions such as ABS and SQRT.
Using the files
You will need to OPEN the input file, assigning it a UNIT number (not 5 or 6) and providing an IOSTAT variable, as in:
OPEN (UNIT=8, FILE="dataD.txt", STATUS="OLD", &
ACTION="READ",IOSTAT=IOVALUE)
where IOVALUE is an INTEGER variable.
When you READ from the file, you will need to provide the unit number and the IOSTAT variable, as in:
READ (UNIT=8, FMT=*, IOSTAT=IOVALUE) A, B, C
The loop to read through the file will end when the IOSTAT variable is set to a nonzero value.
When you are done with the file, CLOSE it, as in:
CLOSE(UNIT=8)
Likewise, you will need to OPEN the output file, assigning it a UNIT number (not 5 or 6 or whatever you used for the input file). For the output file, use STATUS="NEW" and ACTION="WRITE".
When you WRITE to the file, you will need to provide the unit number, as in:
WRITE (UNIT=9, FMT=200) X, Y, Z
where 200 is the label of a FORMAT statement.
When you are done with the file, be sure to CLOSE it.
Programming Notes
In investigating equations in this assignment, consider the two numbers equal if the absolute value of their difference is less than 0.1.
If a triangle is IMPOSSIBLE, do not calculate the perimeter or area, and do not bother with the other possible properties.
The output should be centered on the page. Print a page heading and column headings at the top of the output. Triple-space between the page heading and the column heading. Double-space between lines of output. Include the page number in the page heading. Print reports on no more than 6 triangles per page. When you start a new pages, skip 4 lines to indicate this.
Your program should be well structured. You may use more subroutines or functions as well as those listed above, but don't overdo it.
Your program should be properly documented. For each function or subroutine, provide a box of documentation (comments) providing the name of the function or subroutine, listing what arguments it expects to receive (including their types) and describing what it does.
You may or may not want to use LOGICAL variables in this program.
You will need to declare the types of your functions in the main program. For instance:
LOGICAL :: POSSIBLE
where the code for the function POSSIBLE is listed below after the end of the main program.
Your program and each subprogram should include the IMPLICIT NONE statement.
Your program should use the IOSTAT clause to find the end of the input file.
You may find it necessary to continue some statements over two or more lines. To do this, end the first line with an ampersand ('&') and continue on the next line. If you need to break up a character string in this fashion, place another ampersand before the first character on the next line. Here are examples:
200 FORMAT (///, 15X, 'This', 12X, 'is an', 12X, 'Example', &
12X, 'of continuing', 13X, 'a FORTRAN line.')
ANSWER = ((A .LT. B) .OR. &
(B .LT. C) .OR. &
(C .LT. A))
PRINT *, 'Suppose this is a very long, extremely &
&long, painfully long string.'
What should be turned in?
As in the preceding assignments, you need to compile and link your program, correcting any errors. Name your program "progD.f90". When you run the program, you should create an output file. Call it "output7.txt".