! Read lines of data consisting of three integers, purportedly the sides ! of a triangle. For each line of data, print out the data, and whether ! the data defines a triangle at all (triangle inequality is satisfied). ! If the data defines a triangle, classify the triangle as ! (1) acute (all angles are acute), ! (2) right (has a right angle), ! (3) obtuse (has an obtuse angle), ! (4) equilateral (all sides equal), ! (5) isosceles (at least two sides are equal), ! (6) scalene (no two sides are equal). ! Note that (1)--(6) are not exclusive. For example, do not report that ! an equilateral triangle is also acute and isosceles, but a right ! isosceles triangle should be classified as both right and isosceles. ! ! If the data defines a triangle, report also the largest angle (in ! radians) and the area on a line after the classification. ! PROGRAM TRIANGLE_EXAMPLE INTEGER:: GAP,LARGEST,S,STDIN=5,STDOUT=6 INTEGER, DIMENSION(3):: SIDE REAL:: ANGLE,AREA DO READ (STDIN,*,END=20) SIDE WRITE (STDOUT,FMT='(3I10,A)',ADVANCE='NO') SIDE, " : " IF (ANY(SIDE <= 0)) THEN WRITE (STDOUT,*) "Can't be a triangle." CYCLE END IF ! Make SIDE(1) the largest side. LARGEST=SIDE(1) IF (SIDE(2) > LARGEST) THEN SIDE(1)=SIDE(2) SIDE(2)=LARGEST LARGEST=SIDE(1) END IF IF (SIDE(3) < LARGEST) THEN SIDE(1)=SIDE(3) SIDE(3)=LARGEST END IF ! Check triangle inequality. IF (SIDE(1) + SIDE(2) <= SIDE(3)) THEN WRITE (STDOUT, FMT='(A)') "Can't be a triangle." CYCLE END IF ! Check special case first (equilateral). IF ((SIDE(1) == SIDE(2)) .AND. (SIDE(1) == SIDE(3))) THEN WRITE (STDOUT,FMT='(A)') "Equilateral." GO TO 20 ! Go to angle, area calculation. END IF IF ((SIDE(1) == SIDE(2)) .OR. (SIDE(1) == SIDE(3)) .OR. & (SIDE(2) == SIDE(3))) THEN WRITE (STDOUT,FMT='(A)',ADVANCE='NO') "Isosceles, " ELSE WRITE (STDOUT,FMT='(A)',ADVANCE='NO') "Scalene, " END IF ! Use Pythagorean Theorem to classify as acute, right, or obtuse. GAP = SIDE(1)**2 - SIDE(2)**2 - SIDE(3)**2 IF (GAP == 0) THEN WRITE (STDOUT,FMT='(A)') "right." ELSE IF (GAP < 0) THEN WRITE (STDOUT,FMT='(A)') "obtuse." ELSE WRITE (STDOUT,FMT='(A)') "acute." END IF ! Compute largest angle via law of cosines. 20 ANGLE = ACOS(REAL(GAP)/(-2.0*REAL(SIDE(2)*SIDE(3)))) ! Compute area with Heron's formula. S = REAL(SUM(SIDE)/2) AREA = SQRT(S * PRODUCT(S - REAL(SIDE)) ) WRITE (STDOUT,FMT='(A,F8.5,A,ES14.6,A)') "Largest angle =",ANGLE, & ", area =",AREA,"." END DO 50 STOP END PROGRAM TRIANGLE_EXAMPLE
Also, remember that no late assignments are accepted.