C ASSIGNMENT #2 SAMPLE SOLUTION C IN FORTRAN IV C C CS 152-05 PROGRAMMING LANGUAGE PARADIGMS C SPRING 2014 C RON MAK C DEPARTMENT OF COMPUTER SCIENCE C SAN JOSE STATE UNIVERSITY C INTEGER COUNT COUNT = 0 C C WRITE THE COLUMN HEADERS. WRITE (6, 10) 10 FORMAT(/' COUNT', 10X, 'A', 14X, 'B', 14X, 'C', 11X, 'X1 REAL', \$ 8X, 'X1 IMG', 8X, 'X2 REAL', 8X, 'X2 IMG'/) C C START OF LOOP. C C READ AN INPUT CARD. 100 READ (5, 110) A, B, C 110 FORMAT (3F10.0) C C TEST FOR THE LAST CARD. IF (A .EQ. 0.0) GO TO 200 C C COMPUTE AND PRINT. COUNT = COUNT + 1 CALL SOLVE(A, B, C, X1REAL, X1IMAG, X2REAL, X2IMAG) CALL OUTPUT(COUNT, A, B, C, X1REAL, X1IMAG, X2REAL, X2IMAG) C GO TO 100 C C END OF LOOP. C 200 WRITE (6, 220) COUNT 220 FORMAT (/' END OF PROGRAM:', I3, ' CARDS READ.') C STOP END C C ---------------------------------------------------------------------- C SUBROUTINE SOLVE(A, B, C, X1REAL, X1IMAG, X2REAL, X2IMAG) C TWOA = A + A C C COMPUTE AND TEST THE DISCRIMINANT. DISC = B*B - 4.0*A*C IF (DISC) 10, 20, 30 C C DISCRIMINANT NEGATIVE: COMPLEX ROOTS 10 X1REAL = -B/TWOA X2REAL = X1REAL X1IMAG = SQRT(-DISC)/TWOA X2IMAG = -X1IMAG RETURN C C DISCRIMINANT ZERO: EQUAL REAL ROOTS 20 X1REAL = -B/TWOA X2REAL = X1REAL X1IMAG = 0.0 X2IMAG = 0.0 RETURN C C DISCRIMINANT POSITIVE: UNEQUAL REAL ROOTS 30 S = SQRT(DISC) X1REAL = (-B + S)/TWOA X2REAL = (-B - S)/TWOA X1IMAG = 0.0 X2IMAG = 0.0 RETURN C END C C ---------------------------------------------------------------------- C SUBROUTINE OUTPUT(COUNT, A, B, C, X1REAL, X1IMAG, X2REAL, X2IMAG) C INTEGER COUNT C C THE FORM OF THE ROOTS DETERMINES THE OUTPUT FORMAT. IF (X1IMAG .EQ. 0.0) GO TO 100 IF ((X1REAL .EQ. 0.0) .AND. (X2REAL .EQ. 0.0)) GO TO 200 C C NEITHER PURE REAL NOR PURE REAL: WRITE ALL PARTS. WRITE (6, 10) COUNT, A, B, C, X1REAL, X1IMAG, X2REAL, X2IMAG 10 FORMAT (I4, ': ', 1P7E15.4) RETURN C C PURE REAL: WRITE BLANKS FOR THE IMAGINARY PARTS. 100 WRITE (6, 110) COUNT, A, B, C, X1REAL, X2REAL 110 FORMAT(I4, ': ', 1P4E15.4, 15X, 1PE15.4) RETURN C C PURE IMAGINARY: WRITE BLANKS FOR THE REAL PARTS. 200 WRITE (6, 220) COUNT, A, B, C, X1IMAG, X2IMAG 220 FORMAT(I4, ': ', 1P3E15.4, 15X, 1PE15.4, 15X, 1PE15.4) RETURN C END