CS 1206: Assignment 5

Due Tuesday, August 3, 1999 by 5pm

100 Points


Debugging Programs

Just as you cannot expect to write well unless you are widely read, so you cannot learn to write good programs until you learn to read programs. The purpose of this assignment is to find bugs in a program by reading and thinking about the program (this was called ``deskchecking'' before software engineering gave pedantic names to common sense). Below is a syntactically correct Fortran 90 program containing somewhere between one and ten logical errors (the errors are unrelated to Fortran 90 syntax and semantics). You are not to attempt to compile and execute the program, nor attempt to rewrite the program in another language. Find as many bugs as you can by deskchecking --- reading and reasoning about the program.

Triangle classification program

! 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

Part III: Submitting Your Answers

You are to submit your assignment via email to the class account at cs1206@ei.cs.vt.edu.  Your subject line should read "HW 5".  You
should immediately get an acknowledgement back.

Also, remember that no late assignments are accepted.