Q. 21. Write a program that builds an item-price
database on a direct-access file then uses it to find
price of an item, given its item number.
Solution.
C MAIN PROGRAM
COMMON UNT, RECSZ, FNAME
INTEGER UNT, RECSZ
CHARACTER*15 FNAME
UNT = 10
RECSZ = 25
FNAME = ‘PRICE DATABASE’
CALL BLDDAT
CALL USEDAT
STOP
END
SUBROUTINE BLDDAT
COMMON UNT, RECSZ, FNAME
INTEGER UNT, RECSZ
CHARACTER*15 FNAME
CHARACTER STAT*3
INTEGER ITEM
REAL PRICE
STAT = ‘NEW’
CALL FOPEN(STAT)
10 READ ( 5, *, END = 99)
ITEM, PRICE
WRITE ( UNT, REC = ITEM) ITEM, PRICE
GO TO 10
99 CLOSE (UNT)
RETURN
END
SUBROUTINE USEDAT
COMMON UNT, RECSZ, FNAME
INTEGER UNT, RECSZ
CHARACTER*15 FNAME
CHARACTER STAT*3
INTEGER ITEM, ITEMNO
REAL PRICE
STAT = ‘OLD’
CALL FOPEN(STAT)
10 PRINT *, ‘ENTER ITEM NUMBER’
READ * , INEMNO
IF (ITEMNO .LE. 0) GO TO 99
READ( UNT, REC = ITEMNO) ITEM, PRICE
PRINT *, ‘PRICE OF ITEM’, ITEM, ‘IS’, PRICE
GO TO 10
99 CLOSE(UNT)
RETURN
END
SUBROUTINE FOPEN(STAT)
CHARACTER*3 STAT
COMMON UNT, RECSZ, FNAME
INTEGER UNT, RECSZ
CHARACTER*15 FNAME
OPEN(UNT,IOSTAT = ISTAT, ERR = 99, FILE = FNAME, STATUS
= STAT,
1 ACCESS = ’DIRECT’, FORM = ’UNFORMMATTED’, RECL =
RECSZ)
RETURN
99 PRINT*, ‘ERROR IN OPENING’,
FNAME, ‘;ERR CODE = ‘ ,ISTAT
STOP
END
Q. 22. Write a program to find out the largest
of the given N numbers.
Solution.
DIMENSION X(100)
REAL X, LARGE
INTEGER I, N
C CAN SELECT FROM MAXIMUM
OF 100 NUMBERS
READ *, N
IF (N .GT. 100) THEN
PRINT *, 'TOO MANY NUMBERS'
STOP
ENDIF
C READ THE NUMBERS
READ *, (X(I), I = 1, N)
C INITIALIZE
LARGE = X(1)
I = 2
C PICK THE LARGEST NUMBER.
10 IF (X(I) .GT. LARGE) LARGE = X(I)
I = I + 1
IF (I .LE. N) GOTO 10
C LARGE HAS THE LARGEST
NAUMBER
PRINT *, LARGE
STOP
END
|