Computers in Engineering WWW Site - Example 2.2

Example 2.2


FORTRAN Version

!
      PROGRAM P22
!
!     INTRODUCTION TO READ
!
      IMPLICIT NONE
      INTEGER :: I,J,AGE,POUNDS,GRAMS
      REAL :: DAYS
      CHARACTER (LEN=15) :: NAME,ADDRESS
      LOGICAL :: SINGLE
!
!
      PRINT *, 'This is Program >> P22  - Read in some data'
!
! 
!     READ DATA
!
      READ *,I
!      
      DO J=1,I
         READ * ,AGE,POUNDS
         READ * ,NAME,ADDRESS
         READ * ,SINGLE
!
!     EXECUTABLE STATEMENTS
!
         GRAMS=POUNDS*254
         DAYS=AGE*365.25
!
!     PRINT DATA
!
      PRINT *,'NAME AND ADDRESS :'
      PRINT *, NAME,ADDRESS
      PRINT *,'AGE AND NUMBER OF DAYS :'
      PRINT *, AGE,DAYS
      PRINT *,'POUNDS AND GRAMS :'
      PRINT *, POUNDS,GRAMS
      PRINT *,'SINGLE :'
      PRINT *, SINGLE
      PRINT *,' '
!
!    
      END DO
      STOP
      END PROGRAM P22
DATA:
2
19 105
'Jane '              '123 Main street  '
T
36 185
'Michael '           '105 Greene ave'
F
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 >> P22  - Read in some data
NAME AND ADDRESS :
Jane           123 Main street
AGE AND NUMBER OF DAYS :
          19    6939.75    
POUNDS AND GRAMS :
         105       26670
SINGLE :
 T
 
NAME AND ADDRESS :
Michael        105 Greene ave 
AGE AND NUMBER OF DAYS :
          36    13149.0    
POUNDS AND GRAMS :
         185       46990
SINGLE :
 F
 

Pascal Version


{}
{}
{
     Introduction to read
}
PROGRAM p22 ( input, output);
VAR
  age, pds, single_tmp : INTEGER;
  days, grams : REAL;
  name , adrs : STRING[15];
  single : BOOLEAN;
{
     Read data
}
BEGIN
  WHILE ( NOT eof ) DO
    BEGIN
      readln ( age, pds);
      readln ( name, adrs);
      {
      since we cannot read in a boolean from the keyboard
      we read an integer 1 or 0 and convert to boolean
      }
      readln ( single_tmp );
      IF single_tmp = 0 THEN single := FALSE;
      IF single_tmp = 1 THEN single := TRUE;
    {
         Executable statements
    }
      grams := pds * 454.0;
      days := age * 365.25;
    {
         Print data
    }
      writeln (name, ' ', adrs);
      writeln (age, days);
      writeln (pds, grams);
      writeln (single)
    END { end of while loop }
END.


DATA:
19 105
Jane               123 Main street
1
36 185
Michael            105 Greene ave
0

Last modified: 21/07/97