

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Math 248 Computers and Numerical Algorithms–Pruett LABORATORY ASSIGNMENT Reading from and Writing to Multiple Files
So far, all our computer programs have used only one input source (stan- dard input = keyboard) and one output file (standard output = video dis- play). However, many, if not most, real-world computer programs need data from multiple sources and may also produce multiple output files. For addi- tional input sources or output files, we will need the Fortran 90 statements OPEN and CLOSE in addition to the familiar READ and WRITE state- ments. The syntax for these new statements was discussed in class.
C Enter, compile, and run the demonstration program adding machine on the reverse side of this sheet. You may exclude comments if you wish.
C Change all occurences of READ (,) to READ (5,) and all occurences of WRITE (,) to WRITE (6,). Recompile, rerun, and describe how these changes affect the execution or output of your program.
B Make the necessary changes to the program so that, in addition to the output that goes to the screen, a second copy of the output goes to a file named paper tape. Recompile the demo program and rerun. Look at the contents of paper tape by typing in the CMD window the following:
type paper tape
A Make the necessary changes to the demo program so that your program reads from a file name data.in. Create file data.in using the SciTE editor with no extension, recompile the program, and rerun. In this latter case, both the input and the output are non-standard.
A+ Write from scratch a program that reads from file bit string a binary integer written as a column of bits, with the highest order bit at the top and the zeroth-order bit at the bottom (as shown below) and converts the binary integer to a decimal integer via Horner’s algorithm (ALG 1.1). Here is a sample input file:
1 0 1 1 1 0 0 1
PROGRAM adding_machine ! !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! Mimics an old-fashioned adding machine !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! IMPLICIT NONE REAL :: number1, sum INTEGER :: status_variable! For input error check
WRITE (,) ’ Enter the real numbers you wish to add, ’ WRITE (,) ’ each on a different line followed by ENTER ’ WRITE (,)! Blank line for clarity WRITE (,) ’ When completed, enter any alphabetic CHARACTER instead ’ WRITE (,)! Blank line for clarity
sum1 = 0.0! Accumulator that holds the running sum
READ (,,IOSTAT=status_variable) number IF (status_variable > 0) EXIT! Exit the loop if input error WRITE (*,’(f12.5)’) number1! Output entry to screen sum1 = sum1 + number1! Accumulate END DO
WRITE (,’(12h------------)’)! Output sum to screen WRITE (,’(f12.5)’) sum
STOP END PROGRAM adding_machine