Computers in Engineering WWW Site - Example 7.2

# Example 7.2

#### FORTRAN Version

```!
PROGRAM P72
!
!     DEFINE 3 MATRICES
!     AND AUXILLIARY ARRAYS
!
IMPLICIT NONE
CHARACTER (LEN=3) :: SEASON(4,3)
INTEGER :: NUMBER(4,3),NCOL(3),CMEAN(3)
INTEGER :: J,KS,M,NTOTAL,NAVE,NGRAND,NGAVE,KOL,KROW
REAL :: REVENU(4,3),RCOL(3),DMEAN(3),RTOTAL,RAVE,RGRAND,RGAVE
!
!
PRINT *, 'This is Program >> P72  - Matrix summary'
!
!     Tell program where data for  READ *  is coming from
OPEN(UNIT=5, FILE='P72.DAT')      ! UNIT=5 is the default input
!
!
!     12 LINES IN MONTH ORDER
!
L1:    DO  KS=1,4
L2:      DO  M=1,3
NUMBER(KS,M),REVENU(KS,M)
END DO L2
END DO L1
127  FORMAT(A3,I7,F5.1)
PRINT 1
1  FORMAT(/' SEASONAL SUMMARY')
!
!     DO 4 QUARTER AVERAGE + TOTALS
!
NGRAND=0
RGRAND=0.0
L3:   DO  KS=1,4
NTOTAL=0
RTOTAL=0.0
L4:         DO  J=1,3
NTOTAL=NTOTAL+NUMBER(KS,J)
RTOTAL=RTOTAL+REVENU(KS,J)
END DO L4
NGRAND=NGRAND+NTOTAL
RGRAND=RGRAND+RTOTAL
NAVE=NTOTAL/3
RAVE=RTOTAL/3.0
PRINT 220,(SEASON(KS,J),J=1,3)
PRINT 230,(NUMBER(KS,J),J=1,3),  &
NTOTAL,NAVE
PRINT 240,(REVENU(KS,J),J=1,3),  &
RTOTAL,RAVE
END DO L3
220  FORMAT(/ 3('     ',A3),'   ',  &
'TOTAL','     ','AVE')
230  FORMAT(5I8)
240  FORMAT('  ',5(F6.1,'M\$'))
!
!     DO SAME CALCULATIONS
!     FOR EACH COLUMN
!
L5:   DO  KOL=1,3
NCOL(KOL)=0
RCOL(KOL)=0.0
L6:        DO  KROW=1,4
NCOL(KOL)=NCOL(KOL)+NUMBER(KROW,KOL)
RCOL(KOL)=RCOL(KOL)+REVENU(KROW,KOL)
END DO L6
CMEAN(KOL)=NCOL(KOL)/4
DMEAN(KOL)=RCOL(KOL)/4.0
END DO L5
NGAVE=NGRAND/12
RGAVE=RGRAND/12.0
!
!     OUTPUT COLUMN STATISTICS
!
PRINT 410,NCOL,NGRAND,NGAVE
PRINT 420,RCOL,RGRAND,RGAVE
PRINT 430,CMEAN,DMEAN
410  FORMAT(/ 40('-')/  &
5I8)
420  FORMAT('  ',5(F6.1,'M\$')/)
430  FORMAT(/3I8,'    COLUMN AVERAGE'/  &
'  ',3(F6.1,'M\$')// 40('=')//)
STOP
END PROGRAM P72
```
DATA:
```Jan    672 3.4
Feb    609 3.2
Mar    715 3.7
Apr    803 4.2
May    810 4.8
Jun    831 5.1
Jul    829 5.1
Aug    727 5.1
Sep    780 4.3
Oct    703 3.9
Nov    791 4.2
Dec    783 3.6
```
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 >> P72  - Matrix summary

SEASONAL SUMMARY

Jan     Feb     Mar   TOTAL     AVE
672     609     715    1996     665
3.4M\$   3.2M\$   3.7M\$  10.3M\$   3.4M\$

Apr     May     Jun   TOTAL     AVE
803     810     831    2444     814
4.2M\$   4.8M\$   5.1M\$  14.1M\$   4.7M\$

Jul     Aug     Sep   TOTAL     AVE
829     727     780    2336     778
5.1M\$   5.1M\$   4.3M\$  14.5M\$   4.8M\$

Oct     Nov     Dec   TOTAL     AVE
703     791     783    2277     759
3.9M\$   4.2M\$   3.6M\$  11.7M\$   3.9M\$

---------------------------------------
3007    2937    3109    9053     754
16.6M\$  17.3M\$  16.7M\$  50.6M\$   4.2M\$

751     734     777    COLUMN AVERAGE
4.2M\$   4.3M\$   4.2M\$

=======================================

```

#### Pascal Version

```{\$G256}
{\$P512}
{\$D+}
PROGRAM p72 (input, output);
{
Define 3 matrices and auxiliary arrays
}
VAR
season : ARRAY [ 1..4, 1..3 ] OF string[3];
number : ARRAY [ 1..4, 1..3 ] OF INTEGER;
icol, imean : ARRAY[1..3] OF INTEGER;
revenu : ARRAY [ 1..4, 1..3 ] OF REAL;
col, mean : ARRAY[1..3] OF REAL;
row, column, igrand, itotal, j, iave, igave : INTEGER;
grand, total, ave, gave : REAL;

BEGIN
{
12 lines in month order
}
FOR row := 1 TO 4 DO
FOR column := 1 TO 3 DO
readln ( season [ row, column ], number [ row, column ],
revenu [ row, column ] );
writeln ( ^l );
writeln ( ' Seasonal Summary' );
{
Do 4 quarter average + totals
}
igrand := 0;
grand := 0.0;
FOR row := 1 TO 4 DO
BEGIN
itotal := 0;
total := 0.0;
FOR column := 1 TO 3 DO
BEGIN
itotal := itotal + number [ row, column ];
total := total + revenu [ row, column ]
END; { end for }
igrand := igrand + itotal;
grand := grand + total;
iave := itotal DIV 3;
ave := total/3.0;
FOR j := 1 TO 3 DO
write ( ' ':5, season [ row, j ] );
writeln ( ' ':3, 'total', ' ':5, 'ave' );
FOR j := 1 TO 3 DO
write ( number [ row, j ]:8 );
writeln ( itotal:8, iave:8 );
write ( ' ':2 );
FOR j := 1 TO 3 DO
write ( revenu [ row, j ]:6:1, 'M\$' );
writeln ( total:6:1, 'M\$', ave:6:1, 'M\$' );
writeln
END;
{
Do same calculations for each column
}
FOR column := 1 TO 3 DO
BEGIN
icol[column] := 0;
col[column] := 0.0;
FOR row := 1 TO 4 DO
BEGIN
icol[column] := icol[column] + number [ row, column ];
col[column] := col[column] + revenu [ row, column ]
END; { end for }
imean[column] := icol[column] DIV 4;
mean[column] := col[column] / 4.0
END; { end for }
igave := igrand DIV 12;
gave := grand / 12.0;
{
Output column statistics
}
writeln;
FOR j := 1 TO 50 DO
write ( '-' );
writeln;
writeln ( icol[1]:8, icol[2]:8, icol[3]:8,
igrand:8, igave:8 );
writeln ( ' ':2, col[1]:6:1, 'M\$', col[2]:6:1, 'M\$', col[3]:6:1, 'M\$',
grand:6:1, 'M\$', gave:6:1, 'M\$' );
writeln;
writeln ( imean[1]:8, imean[2]:8, imean[3]:8, ' ':4, 'column average' );
writeln ( ' ':2, mean[1]:6:1, 'M\$', mean[2]:6:1, 'M\$', mean[3]:6:1,
'M\$' );
writeln;
FOR j := 1 TO 50 DO
write ( '=' )
END.

```
DATA:
```jan    672 3.4
feb    609 3.2
mar    715 3.7
apr    803 4.2
may    810 4.8
jun    831 5.1
jul    829 5.1
aug    727 5.1
sep    780 4.3
oct    703 3.9
nov    791 4.2
dec    783 3.6
```