CSE 1540.03
Week 2.1January 13, 2014
Read:Chapter 3 and 4 of the textbook.
Prerequisite for success in Computer Programming:
Donald Knuth (perhaps the world's foremost Computer Scientist) put it best in a keynote address for the 11th World Computer Congress in 1989:
"What were the lessons I learned from so many years of intensive work on the practical problem of setting type by computer? One of the most important lessons, perhaps, is the fact that SOFTWARE IS HARD. From now on I shall have significantly greater respect for every successful software tool that I encounter. During the past decade I was surprised to learn that the writing of programs for TeX and METAFONT proved to be much more difficult than all other things I had done (like proving theorems or writing books). The creation of good software demands a significantly higher standard of accuracy than those other things do, and it requires a longer attention span than other intellectual tasks."
Fortran Program Components
Program Units
main program, subprograms (functions, subroutines)
Declaration Statements
provide names and attributes of variables
set aside space the "buckets"
Executable Statements
Manipulative Statements
- manipulate values of variables (do work):
assignment statements, input, etc.
Control Statements
- determine order in which statements are executed:
repetition, selection
the game of "musical buckets"
Comments
- make program easier to understand for humans
- have NO effect on program or computer
Program Structure
program name
implicit none
declarations
executable statements
end
Example:
program Convert
implicit none
! read Fahrenheit temperature and
! print corresponding Celsius value
real f,c
read*, f
c = 5 * (f - 32) / 9
print*, c
end
Notes:
- By convention, lower case is used for everything except named constants, which are in upper case. It is generally accepted that lower case looks more attractive.
- Comments are placed on a separate line.
- All variables must be declared before the first executable statement.
- Fortran 77 is line oriented. (Avoid using the first column.)
to continue a line, end the existing line with & and start
the next line with an &.
A separator is ai)space (blank)
ii)tab
Identifiers (i.e. variable names), keywords, constants, etc. may
not contain a separator.
- The listing appears as it was entered, so paragraphing is the programmer's responsibility.
Comments
! anything
c anything
Placed on a separate line. Comments in traditional Fortran start
with the letter "c" in column one. Comment starting with the
character "!" is a feature of Fortran 90 and can start anywhere on
a line.
Example:
! This is a long comment
! which stretches over
! several lines
Declarations
integerlist of variables
real list of variables
Notes:
1.set aside space in the computer's memory to store data.
2.attach a name to this space.
Analogy: "buckets"
variable = bucket + label stuck to its front
label = identifier (variable name)
data type determines what kind of values the bucket may hold,
i.e. it determines the "shape" of the bucket.
Example:
integer x, y, z
Note: on creation the bucket is empty.
Fortran numeric data types
integer32-bit two's compliment
integer*4−2,147,483,648 to +2,147,483,647
integer*216-bit two's compliment
−32,768 to +32,767
integer*18-bit two's compliment
−128 to +127
real32-bit IEEE754 single precision
real*4positive numbers:
to
double precision64-bit IEEE754 double precision
real*8positive numbers:
to
Identifiers
An identifier is a letter followed by more letters or digits.
(Traditional Fortran only allows 6 characters in an identifier.)
Examples:
x
payroll
taxDeduction
thisIsAVeryLongIdentifier
r2d2
invalid
13a
-g
Simple data types:
integer
real
logical
character*n
Values
The "objects" you put into the "buckets" and manipulate.
TypeExamples
integer1375-63
real2.52e4-5.61e-27
character*n"sequence of characters"
Reading Input
read*, list of variables
Notes:
1.Stream input: acts like a conveyor belt (or water stream or
video tape player)
2.Format of input data:
- use most convenient form for numbers
- values are separated by at least one space or ENTER
3.Automatic Type Conversion: integer data converted to real,
real data truncated to integer part.
Example:
read*, x, y, z
Writing Output
print*, "sum is", x+y+z, 7, a
write(*,*) a, b, c
list of- constants (string, number)
- variables
- expressions
Notes:
1.Stream Output - conveyor belt
2.Format of output:
- automatically constructed according to data type and value
- field just wide enough to hold value (user controllable)
3.print picks up where last one left off (same for read)
4.To control output format: use format statement with write
write(*,12) a, b, c
12 format(1x,I5,F6.1,E10.4)
CSE 1540Week 2.1 – January 13, 2014page 1 of 4