CS 1206: Assignment 5

Due Friday, April 2, 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 hand in your assignment by using the Automated Grader application. To receive credit, your assignment must be submitted before the time and date listed above. It is your responsibility to successfully submit your assignment via the Automated Grader. Only one submission is allowed per student. Brief instructions are given below on how to use the Grader, but it will be helpful to visit the Automated Grader's web page.

After connecting to the Internet, run the Grader program with the command:

Acceptor_UI

A dialog box will then appear. If the title of the dialog box says "CS 1206 Grader Client" then you have the correct application; otherwise you are running the wrong client application on your local machine. Fill in your original PID, e-mail password, and Student ID. Click inside the Course Index field and select the lab section you are enrolled in. Type in HW5 for the Project Number. Enter the full path of the file to be submitted, or click on Browse to find it. After all the information is typed in, click on Submit. If your submission was successful, a window will appear stating "Your file has been submitted. A confirmation will be sent to you via e-mail." If your submission did not succeed, then a dialog box will appear describing the problem. Correct the information and resubmit. If the submission was successful, click the Quit button. After a few minutes (or sometimes hours!), you should receive an e-mail message verifying the file was received.

Also, remember that no late assignments are accepted.




Layne T. Watson <ltw@cs.vt.edu>
Last modified: Sun Mar 7 15:08:42 EST 1999