|
The Fortran Adventure
Silly as it may sound, I had this great urge of looking into a
language called Fortran. If you haven't heard of it, then here is
some historical data on it:
This language was devised first for an
IBM 704 computer, in
1957. It was developed by a group led by
John Backus. FORTRAN stands for
Formula Tran slation. It
was mainly developed to solve mathematical and scientific
problems. Fortran was one of the very first high level
languages. It has a rich set of control
structures and input / output statements. (I was able to create a
simple address book application in 2 minutes after trying Fortran
for the first time for about 20 minutes !).
If you have programmed with BASIC, then, you already know about interpreters
and compilers. Fortran is compiled so you get executables
which run at blazing speeds. Back to histroy, by 1963, most
major computer vendors provided Fortran compilers for their hardware.
However, the development of these compilers were not checked
so each vendor ended up adding his own proprietary features which
started a whole lot of compatibility problems. In 1966, an ANSI
(American National Standards Institute) standard for Fortran was
formed. This helped in bringing about some order.
1977, a need was felt
for standardizing Fortran even more for sake of source compatibility,
thus the Fortran 77 standard was developed. It is in use even
today. This standard was later adopted by the ISO.
It defined better character/string handling & improved I/O. This made
Fortran a great language for programming a wide range of
applications.
Subsequently, the Fortran 77 standard was replaced by the Fortran
90 standard. However, Fortran 77 never truly died. People all
over the world still love and use Fortran 77.
Recently, there has been some work going on for developing
an object oriented version of Fortran called Fortran 2000.
Recenlty, a project called FORCE 2.0 is under development.
This project aims at developing a freeware Fortran compiler
and editor. They have a full fledged freeware
Fortran compiler for download. It also comes with a beautiful
editor. You can read more about it at their hoempage, http://www.forceproject.hpg.com.br
I also have spent a few hours trying to download tutorials for Fortran.
I must say that good Fortran tutorials are pretty hard to come
by, even on the Internet. I have managed to find two great
tutorials. They are available here for download:
The Professional Programmer's Guide to Fortran 77 by Clive G.
Page, University of Leicester, UK
This tutorial
is actually the digital version of the book which was published by Pitman in 1988. It makes an excellent tutorial. I
think this tutorial is all you'll ever need to make great programs in Fortran. Reading this tutorial has taken me to yet another adventure, which is, Adventure of the PostScript.
If you use Windows, no doubt, you must have used Microsoft Word atleast
once. Word is a pretty good editor. But, Word is not very
famous in the academic cirlces where the only words you hear are
Unix, Tex, Vi and Emacs ! They use something called PostScript
format for creating printable documents. PostScript was developed a
long time back, when there was no DOS, by Adobe,
famous for their PhotoShop product. Anyway, what I wanted
to convey was that the tutorial is in PostScript format so you will
not be able to view it right away. You must first download a
viewer.
Thankfully, some kind souls have developed a great
PostScript reading software for Windows. It is called
GhostScript. Not only does GhostScript understand
PostScript (.ps) files, it can also read PDFs and convert one file
format to another and many more things, detials of which I have been
unable to explore due to lack of time. You can download GhostScript
here: ftp://mirror.cs.wisc.edu/pub/mirrors/ghost/gnu/
Oh yes, another thing, GhostScript has a shell like interface
which will take time getting used to, so, you'll also have to
download a GUI interface for reading the tutorial. Mercifully, even
this is avaibale for free. The software is called
GhostView. You can download it at: http://www.cs.wisc.edu/~ghost/gv
Well, now that you have the compiler and the tutorial, start
writing some exciting Fortran code. If you want to add some comments
or experiences with Fortran, please mail me at siddharth_b@yahoo.com
Source code for 3 programs are ready for
download:
Example #1 Report
Print - this program uses simple Input/Output routines to
print records from a file. The source code is given below:
C NAME : fileio
C AUTHOR : Siddharth Barman
C DESCRIPTION : Program to read a text file and print records
C DATE : 09-05-2002
C/n
PROGRAM fileio
Character cFile*20
Integer nRecs
Character cData*(20)
Integer nMarks, nCnt, nP, nC, nM
Real rPer
Write (*,*) 'Enter the file name with path:'
Read (*,*) cFile
* Read the number of records from header
Open(Unit=1, File=cFile, Status='old')
Read(Unit=1, FMT=*) cData, nRecs
Write (*,*) 'Mark List'
Write (*,*) 'Name Physics Chemistry Maths Total Percent'
Write (*,*) '-------------------'
Do 10, nCnt=1, nRecs
Read(Unit=1, FMT=*) cData, nP, nC, nM
nMarks = nP + nC + nM
rPer = 1.0 * nMarks / 300
Write (*,*) cData, nP, nC, nM, nMarks, rPer
10 Continue
Close(1)
End
|
Explanation: The source is
actually quite simple to understand. Every FORTRAN program begins
with a PROGRAM statement that acts as a way for you to give a name
to your program.
All lines which begin with a 'c' or a '*' in the first column is
treated as a comment.
I have used expicit variable declaration which is NOT a necessity
in FORTRAN. However, since I come from a C++ background, this comes
to me naturally. If you will notice, I have declared cData as
Character cData*20. This means that this variable will hold
character data (or a string if you may) and the maximum permitable
length of the string is 20. Other variable declarations are
self-explanatory.
In order to write to a file or the screen, FORTRAN makes use of
"UNITS" which is specified in the "WRITE" command. Each file can be
opened and given a unique number starting from 0. In case you want
to write to a standard device i.e. the screen, you can simply say
'*' instead of using a number. This is what I have done with the
Write (*,*) 'Enter the file name with path:' command.
In order to interact with the user, I make use of the "READ"
command. This too works on the principle of UNITS. Here the user
enters some value by punching in the data using the keyboard. This
value is read stored in a variable called cFile. The WRITE and READ
commands also allow you to specify the format in which data is
written to or read from a device. How to specify the format is shown
in the 3rd program that deals with Sorting and Report
Printing.
In order to read/write to a file stored on the disk or tape, we
have to first open a file and assign a UNIT number to it. This is
done using the "OPEN" command. Status='OLD' means that the file is
already existing in the disk. In case you want to create a new file,
then, you must specify STATUS='NEW'.
After opening the file, we first read how many records or lines
are present in the file by making use of some header data present in
the file. This data has to be entered correctly by us while creating
this file. You can create this file in Vi or Emacs or Notepad if you
are a Windows user. Save it simply as a TEXT file. The data of the
file will look something like this:
RECORDS, 3
Sid, 78, 92, 81
Mina, 90, 87, 94
Yogi, 78, 80, 88
For iterating through all the lines in the
file, we make use of a LOOP. To create a loop, we need a line
marker, a counter variable, a start value and an end value. For us,
the looping statement is declared as:
Do 10, nCnt=1, nRecs
Here, 10 is the line-marker, nCnt is the
counter variable, 1 is the start value and nRec is the end value.
The value of nRecs is what is to be specified in the header of the
file i.e. the line RECORDS, 3. Therefore, nRecs will have the value
3. Inside the loop, we simple print out the values are being read
and also add up the marks to get the total and print it out.
Simple ? I thought so
too...
Example #2 Sorting and
formatted Report Print with file
save - this program
uses extensive Input/Output routines, arrays
and format specifiers to display a
sorted Mark List and
store it into a file. The source code is given below:
* Program to illustrate usage of Array
* This pgogram will ask user to enter marks for n number of students
* which will be stored in arrays
* This array will then be sorted according to the marks entered
* The data will also be saved in a file specified by the user
C
C NAME : entermarks.f
C AUTHOR : Siddharth Barman
C DESCRIPTION : As above
C DATE : 15-05-2002
C/n
PROGRAM Marks
INTEGER nStuds, nMaths, nChem, nPhys
CHARACTER cName*20
CHARACTER cFile*50
INTEGER aMaths(50), aChem(50), aPhys(50)
INTEGER i,j
INTEGER nTot1, nTot2, nStat
CHARACTER aName(50) * 20
WRITE(*,*) 'Enter the number of students:'
READ(*,*) nStuds
IF (nStuds .LE. 0) THEN
WRITE (*,*) 'Quitting program.'
STOP
END IF
DO 15, nCount=1, nStuds
WRITE (*,*) 'Enter marks for student number', nCount
WRITE (*,*) 'Student name:'
READ (*,*) cName
WRITE (*,*) 'Maths:'
READ (*,*) nMaths
WRITE (*,*) 'Physics:'
READ (*,*) nPhys
WRITE (*,*) 'Chemistry:'
READ (*,*) nChem
aName(nCount) = cName
aMaths(nCount) = nMaths
aChem(nCount) = nChem
aPhys(nCount) = nPhys
15 CONTINUE
DO 20, i=1, nStuds - 1
DO 30, j=i+1, nStuds
nTot1 = aMaths(j) + aPhys(j) + aChem(j)
nTot2 = aMaths(i) + aPhys(i) + aChem(i)
IF (nTot1 > nTot2) THEN
nMaths = aMaths(i)
nChem = aChem(i)
nPhys = aPhys(i)
cName = aName(i)
aMaths(i) = aMaths(j)
aChem(i) = aChem(j)
aPhys(i) = aPhys(j)
aName(i) = aName(j)
aMaths(j) = nMaths
aChem(j) = nChem
aPhys(j) = nPhys
aName(j) = cName
END IF
30 CONTINUE
20 CONTINUE
WRITE (*,*) 'Mark Listing'
WRITE (*,*) '------------'
WRITE (UNIT=*, FMT=99)
DO 40, i=1, nStuds
WRITE (UNIT=*, FMT=100)
$ i,' ' // aName(i), aMaths(i), aPhys(i), aChem(i)
40 CONTINUE
WRITE(*,*) ''
50 WRITE(*,*) 'Save data to file (y/n)?'
READ(*,*) cFile
IF (cFile .NE. 'y' .AND. cFile .NE. 'n') THEN
WRITE (*,*) 'Illegal choice, please re-enter'
GO TO 50
ELSE
IF (cFile .EQ. 'y') THEN
WRITE (*,*) 'Enter file name:'
READ (*,*) cFile
OPEN (UNIT=1, FILE=cFile, STATUS='OLD', IOSTAT=nStat)
IF (nStat .NE. 0) THEN
OPEN (UNIT=1, FILE=cFile, STATUS='NEW')
END IF
DO 60, i=1, nStuds
WRITE (UNIT=1, FMT=*) aName(i), aMaths(i), aPhys(i)
$ , aChem(i)
60 CONTINUE
CLOSE(1)
Write (*,*) 'Date written to file ', cFile
END IF
END IF
99 FORMAT (' Roll Name Maths Physics Chemistry')
100 FORMAT (1X, I4, T7, A, T24, I3, T33, I3, T44, I3)
END
|
Explanation: This program
makes extensive use of arrays. The program starts out by asking the
user to enter the number of students there are.
It then lets the user enter
the name, and marks of 3 subjects for each student.
It stores all this information is arrays. We then use a simple
sorting algorith to sort the arrays sothat the data present in them is present
in descending order of the total marks of the 3
subjects.
The data is displayed using another loop.
After this, the user is asked if he wants to save this data
in a file. If he enters 'y', then he is asked for a filename. On
entering the filename (e.g. c:\myfile.txt), the program checks if
the file already exists, in case it does not the IOSTAT variable
nStat return a non-zero number. We then open the file using
STATUS='NEW' to create a new file. Using a loop, we then write all
the data to the file.
We also make use of FORMATTING. For this, we make use of the
FORMAT statment. For e.g. line numbered 100 has a FORMAT
statement where we have defined the format that is to be used. The
format is specified as a string. Each character has a special
meaning in the format string. 1X is used for proper new-line
formatting, I4 means that we are outputting an Integer value whose
width is to be 4, T7 means we want to transfer the next output to
column 7 on the device (screen for us) and so
on.
Example #3 Text to XML Conversion - All the
world talks about today is XML. XML stands for
Extensible Markup Language. This program reads data from a file
created with the previous program and converts it into an
XML document so that you can read this data using any other language
such as Java or C++. The source code is given below:
* Program to convert a text file to XML format
* Program assumes that a particular structure exists in the file
* The format is as follows: name, physics_marks,
* chemistry_marks, maths_marks per line
PROGRAM Text2XML
CHARACTER cFile*50, cName*20, cOut*50
INTEGER rPhys, rChem, rMath
INTEGER nStatus
WRITE (*,*) 'Enter the data file path:'
READ (*,*) cFile
WRITE (*,*) 'Enter the destination xml file name:'
READ (*,*) cOut
OPEN(UNIT=1, FILE=cFile, STATUS='OLD', IOSTAT=nStatus)
IF (nStatus .NE. 0) THEN
WRITE (*,*) 'Could not open file for processing.'
WRITE (*,*) 'Terminating program.'
STOP
END IF
OPEN(UNIT=2, FILE=cOut, STATUS='OLD', IOSTAT=nStatus)
IF (nStatus .NE. 0) THEN
OPEN(UNIT=2, FILE=cOut, STATUS='NEW')
END IF
WRITE(UNIT=2, FMT=*) ''
WRITE(UNIT=2, FMT=*) ''
50 READ(UNIT=1, FMT=*, IOSTAT=nStatus) cName, rPhys, rChem, rMath
IF (nStatus .NE. 0) THEN
GO TO 100
END IF
WRITE (*,*) cName, rPhys, rChem, rMath
WRITE (UNIT=2, FMT=*) ''
WRITE (UNIT=2, FMT=*) '', cName, ''
WRITE (UNIT=2, FMT=*) '', rPhys, ''
WRITE (UNIT=2, FMT=*) ''
GO TO 50
100 WRITE (UNIT=2, FMT=*) ''
Write (*,*) 'Finished processing'
CLOSE (1)
CLOSE (2)
END
|
Explanation: Apart from the use of XML tags, the rest of the program is
pretty easy To more information on XML, visit http://www.xml.org
.
|