Computers in Engineering WWW Site - Example 4.7

Example 4.7


FORTRAN Version

!
      PROGRAM P47
!
!     CLASS LIST PROGRAM
!
      IMPLICIT NONE
      CHARACTER (LEN=20) :: NAME
      REAL :: MARK,AVE
      INTEGER :: I,N
!
      PRINT *, 'This is Program >> P47  - Class List program'
!
!     Tell program where data for  READ *  is coming from
      OPEN(UNIT=5, FILE='P47.DAT')      ! UNIT=5 is the default input
!
      PRINT 1
1     FORMAT(//'CLASS LIST:' &
      ///' NAME','                   ','MARK')
!
      !   The /// give 2 blank lines
!
      READ *,N
      AVE=0.0
L1:   DO I=1,N
         READ 10,NAME,MARK
10       FORMAT(A20,F5.2)     ! Real data entered with decimal point
         AVE=AVE+MARK
         PRINT 20,NAME,MARK
20       FORMAT(' ',A20,F7.1)
      END DO L1
!
      AVE=AVE/N
      PRINT 30,AVE
30    FORMAT(' ',27('-')/ &  ! 27 dashes for a separator
      ' AVERAGE','             ',F7.1)   ! This is on the line below
!
      PRINT 56
   56 FORMAT(' ')   ! Print a blank line
      STOP
      END PROGRAM P47
DATA:
5
Mickey Mouse          85.0
Goofy                100.0
Pluto                 82.0
Dumbo                100.0
Cinderella             5.0
OUTPUT:

              +--------------------------------------------------+
              |     32-bit Power for Lahey Computer Systems      |
              |   Phar Lap's 386|DOS-Extender(tm) Version 7.0    |
              |  Copyright (C) 1986-94 Phar Lap Software, Inc.   |
              |           Available Memory = 14880 Kb            |
              +--------------------------------------------------+


This is Program >> P47  - Class List program


LASS LIST:


NAME                   MARK
Mickey Mouse           85.0
Goofy                 100.0
Pluto                  82.0
Dumbo                 100.0
Cinderella              5.0
---------------------------
AVERAGE                74.4


Pascal Version

PROGRAM p47 (input, output);
{
     Class list program
}
VAR
  name : string[20];
  mark, ave : REAL;
  i, n : INTEGER;
BEGIN
  writeln ( ^l );
  writeln ( ' Class List: ' );
  writeln;
  writeln;
  writeln ( ' ':8, 'name', ' ':9, 'mark' );
  writeln;
  writeln;

  readln ( n );
  ave := 0.0;
  FOR i := 1 to n DO
    BEGIN
      readln ( name, mark );
      ave := ave + mark;
      writeln ( name:20, mark:5:2 )
    END;

  ave := ave/n;
  writeln ( '-------------------------' );
  writeln ( 'average', ' ':13, ave:5:2 );

  writeln ( ^l )
END.
DATA:
5
Mickey Mouse          85.0
Goofy                100.0
Pluto                 82.0
Dumbo                100.0
Cinderella             5.0

Last modified: 21/07/97