CS 1206: Assignment 5

Due Friday, March 27, 1998 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
  END IF
  IF (SIDE(3) > LARGEST) THEN
    SIDE(1)=SIDE(3)
    SIDE(3)=LARGEST
  END IF
! Check triangle inequality.
  IF (SIDE(2) + SIDE(3) <= SIDE(1)) 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(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

Submitting Your Answers

You are to hand in your assignment by sending an email message to the address cs1206@ei.cs.vt.edu. To receive credit, your mail message must be received by this account by the time and date listed above. It is your responsibility to successfully submit your assignment via email (remember that you will receive an auto-reply by the system when your message has been received successfully). Also, send yourself a copy for future reference. The messsage must be a plain ASCII text file, with no attachments, and in exactly the format defined below. Do not insert any extra notes or explanations.

The body of your message must be a plain ASCII text file that contains the following.

Note that you will lose points on your homework assignment for failing to follow these instructions--i.e., using an incorrect subject, failing to include your name, or failing to include any of the other required elements listed above. Also, remember that no late assignments are accepted.




Layne T. Watson <ltw@cs.vt.edu>
Last modified: Wed Jan 28 21:10:48 EST 1998