to Computers and the Fortran LanguageComputer Languages Each computer has its own machine language Assembly used to execute very basic operations.. to Computers and the Fortran Language
Trang 1Fortran 95-2003 for Scientists and
Engineers
Transparencies prepared by Anthony
T Chronopoulos
Trang 3Chapter 1 Intro to Computers and the Fortran Language
Input Devices (e.g keyboard, tapes etc)
Output Devices (e.g display, monitor, tapes etc).
Trang 4The CPU
The Central Processing Unit is "heart“ (or better “brain”) of a computer
The CPU consists of three main parts:
The Control Unit - coordinates activities of the computer
The Arithmetic Logic Unit (ALU) - performs the calculations
Registers - store a small amount of data and instructions
Trang 5Chapter 1 Intro to Computers and the Fortran Language
• Main Memory (RAM)
It is larger than the Registers and smaller than the hard drive.
It temporarily stores the program currently being run by the computer and also the data required by the programs.
RAM is volatile, i.e when power is interrupted, then what was stored in RAM is lost.
Trang 6Secondary Memory
It is a permanent (non-volatile) storage.
The hard drive is the best example, but also USB, CDs, tapes, etc The size of hard drives is larger than that of RAM However, accessing data stored on a hard drive (or other secondary memory) takes much longer (5-10 times) than from RAM.
Trang 7Chapter 1 Intro to Computers and the Fortran Language
Computer Languages
Each computer has its own machine language (Assembly) used to execute very basic
operations Operations are: load and store (data, to and from memory), and add, subtract, multiply, or divide
The problem with a machine language is that it is very difficult to program and use, and also
it can be unique for a computer.
Trang 8Thus, computer scientists design high-level language
that are easy to program and use The programs must
be converted to a machine-language (by compilers and
linkers) for the computer to run them Examples are
Fortran, C, C++, Java etc
The benefit of a high-level language (e.g Fortran) is that a program and can be compiled on any machine that has a Fortran compiler
Trang 9Chapter 1 Intro to Computers and the Fortran Language
Data Representation in a Computer
Data is represented by millions of switches, each of which can be either ON (1) or OFF (0) 0 and 1 are the two binary digits (bits).
We use a combination of 0's and 1's to represent the other numbers (and characters as
well).The smallest common combination of bits (0's and 1's) is called a byte A byte is a group of 8 bits A word is a group of 2, 4, or 8 bytes Our PCs have 4-byte words
Trang 10• The Binary Number System
The number system used by humans is the decimal system The decimal system is a base=10 system.
There are 10 digits (0-9) Each digit in a number represents a power of 10.
The number 221 is:
2 * 10^2+ 2 * 10^1 + 1 * 10^0
(where 10^i is 10 raised to exponent i =0,1,2, )
Trang 11Chapter 1 Intro to Computers and the Fortran Language
Similarly, converting a number from binary (base 2) to decimal (base 10).
The number 101:
1 * 2^2+ 0 * 2^1 + 1 * 2^0 = 4 +0+ 1 = 5
If n bits are available, then those bits can represent 2^n possible values.
e.g One byte (n=8 bits) can represent 256 possible values Two bytes (n=16 bits) can represent 65,536 possible values.
Trang 12Which of the following ranges would best describe the possible values of a byte?
(1) 0 , , 255 (2) 1 , , 256 (3) -127 , , 128
(4) -128 , , 127 The answer is (4)
The reason is that the computer must store both
positive and negative integers The following
example illustrates how this can be achieved
Trang 13Chapter 1 Intro to Computers and the Fortran Language
Example from the car odometer/milometer (Note: content not in Book
will not be used in Exams)
Milometer readings during travel.
Trang 14e.g Storage of 8-digit integers, in base=10 system:
Trang 15Chapter 1 Intro to Computers and the Fortran Language
e.g Storage of (4-bit) integers:
When using the binary system, the effect is that
if the first binary digit (or bit) is a one then the number is negative,
while if it is zero the number is positive.
Trang 16• Two other number systems that are also useful are octal (base 8) and hexadecimal (base 16).
• Table 1-1 shows the conversion between these systems for the decimals: 0,1, …,15
Trang 17Chapter 1 Intro to Computers and the Fortran Language
Types of Data
Three common types of data are stored in a computer's memory: character, integer, real Each type has unique characteristics and takes up a different amount of memory.
Trang 18Character Data
A typical system for representing character data may include the following:
Letters: A through Z and a through z
Digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Miscellaneous symbols, e.g (", ', ?, , <, >, =, %, &)
Trang 19Chapter 1 Intro to Computers and the Fortran Language
• In the past, it has been common to use just one byte of memory to represent a maximum of
256 characters (ASCII)
• To represent characters found in many languages, one can use use 2 bytes of memory which allows 65,536 possible characters (Unicode)
Trang 20Integer Data
Integer data ( e.g -1, -355, 0, 1993) are represented exactly on computers However, only a finite number can be stored Most machines will use 4 bytes of memory to represent integers
Smallest n-bit integer: -2^(n-1)
Largest n-bit integer: 2^(n-1) - 1
e.g n=32 for 4-byte numbers.
Trang 21Chapter 1 Intro to Computers and the Fortran Language
With 4 bytes of memory we can represent numbers in the approx range [-2.15*billion, 2.15*billion].
Outside this range we get arithmetic overflow.
Integer data limitations: (1) No fractional parts.
(2) It is not possible to represent very large positive integers or very small negative integers.
Trang 22Real Data
The real data type stores numbers in a format similar to scientific notation.
For example, the speed of light in a vacuum is about 299,800,000 meters per second The
number in scientific notation would be 2.998 * 10^8 (m/s) meters per second.
Trang 23Chapter 1 Intro to Computers and the Fortran Language
A format similar to scientific notation will be used to represent real numbers in FORTRAN Real numbers occupy 4 bytes of memory These 4 bytes will be divided as follows:
3 byte for the fraction (or mantissa)
1 byte exponent
The mantissa will be a number between -1 and 1.
The exponent will contain the power of 2 required to scale the number to its actual value.
Trang 24The real numbers are converted Consider any non-zero real number as
a fraction lying between 0.1 and 1.0, called the mantissa, which is multiplied
or divided by 10 a certain number of times, where this number is called the
exponent e.g 17877.0 (in fraction, base=10 form): 0.17877x10^5
10b
exponent mantissa
The same technique as was used for integers to distinguish positive and
negative numbers will be used for both the mantissa and the exponent.
Trang 25Chapter 1 Intro to Computers and the Fortran Language
Precision and Range
Precision refers to the number of significant digits that can be preserved in a number (on a computer).
Based on the number of bits (or bytes) in the mantissa,
3 byte mantissa gives about 7 significant digits (between 1.0 and -1.0) e.g 12345678.4 is stored
as
12345678.0 The difference (i.e 0.4) is called the round-off error
Trang 26Range is the difference between the largest and smallest numbers that can be represented The number of bits in the exponent e.g
1-byte exponent (with the 3-byte mantissa) allows a range of approximately 10^-38 to 10^38 (i.e [10^-38 , 10^38] )
Trang 27Chapter 1 Intro to Computers and the Fortran Language
Evolution of Fortran:
Fortran I: Fig 1-5
Fortran 77: Fig 1-6
Fortran 90/2003: Fig 1-7
Trang 28• Recommended Problems (not to be handed in)
• Page 12 , Quiz 1 (answers are on the back):
1(a),2(a),6,7
Trang 29CHAPTER 2
CS1073
Trang 302.2 The Fortran Character Set (Table 2-1)
The following are valid in a Fortran 90/95 program:
alpha-numeric: a-z, A-Z, 0-9, and _ (the underscore);
Trang 31Chapter 2: Basic Elements of Fortran
2.3 Structure of a FORTRAN Statement
A program consists of a series of statements designed to accomplish the goal to be
accomplished.
There are two basic types of statements:
Executable statements describe the actions taken by the program (additions, subtractions,
multiplications, divisions).
Non-executable statements provide information necessary for proper operation of the
program.
Trang 32Rules on Fortran statements:
Each line may be up to 132 characters long.
A statement too long to fit in a single line may be continued on the next line by ending the
current line with an & (ampersand) e g
output = input1 + input2 ! sum the inputs
output = input1 & ! Also, sum the inputs + input2
Trang 33Chapter 2: Basic Elements of Fortran
The above two statements are equivalent.
Commenting your code is very important To comment in FORTRAN, one uses the
exclamation point (!).
All comments after the ! are ignored by the compiler
Trang 34One can use labels in some statements A label can be any number between 1 and 99999 Statement labels are less common in modern FORTRAN
Trang 35Chapter 2: Basic Elements of Fortran
2.4 Structure of a FORTRAN Program
A FORTRAN program can be divided into three sections:
Declarations - This section consists of a group of non-executable statements at the start of the
program.
Execution - This section consists of one or more statements describing the actions to be
performed by the program.
Termination - This section consists of a statement (or statements) telling the computer to
stop/end running the program.
Trang 36The program (in Fig 2-1) reads two numbers as input, multiplies them, and prints out the result PROGRAM my_first_program
! Purpose:
! To illustrate some of the basic features of a Fortran program.
!
! Declare the variables used in this program.
INTEGER :: i, j, k ! All variables are integers
! Get two values to store in variables i and j
WRITE (*,*) 'Enter the numbers to multiply: '
READ (*,*) i, j
Trang 37Chapter 2: Basic Elements of Fortran
! Multiply the numbers together
Trang 38Discussion of Program Above
The first statement of this program begins with the word PROGRAM This is a
non-executable statement that specifies the name of the program to the FORTRAN
compiler.
The name may be up to 31 characters long and be any combination of alphabetic characters,
digits, and the underscore.
The first character must be a letter.
The PROGRAM statement must be the first line of the program.
Trang 39Chapter 2: Basic Elements of Fortran
The Declaration Section
This section begins with a comment stating that variable declarations are to follow.
The declaration begins with the data type (INTEGER) followed by two colons and then the
Trang 40The Execution Section
The first statement in this section is the WRITE statement that tells the user to enter the input The second statement will read the input and assign the values to the corresponding variables The third statement multiplies the two variables and the product is assigned to a third
variable
The last executable statement prints the product to the screen.
Trang 41Chapter 2: Basic Elements of Fortran
The Termination Section
The STOP statement tells the computer to stop running the program
The use of the STOP command is optional here
The END PROGRAM statement informs the compiler that no more statements exist
Trang 42Compiling and Executing the FORTRAN Program
Before a program can be run (executed) it must be compiled into an executable program.
In this process the code may also be linked to various system libraries.
Trang 43Chapter 2: Basic Elements of Fortran
2.5 Constants and Variables
A constant is a data object that is defined before a program is executed and it does/can not
change during the execution of the program.
Constants are used in developing a good/correct programs in solving math problems (e.g the circle constant PI ).
Trang 44A variable is a data object that can change value during the execution of a program Referring to the sample program above:
The data objects i,j,k are variables.
Each variable (or constant) must have a unique name inside the program.
Trang 45Chapter 2: Basic Elements of Fortran
The names may contain up to 31 characters and any combination of alphabetic characters,
digits, and the underscore
The first character must always be alphabetic.
• The name we assign a variable (or constant) should be meaningful in terms of the
purpose that it is used to solve the problem (e.g.
• time, distance, grade etc).
Trang 47Chapter 2: Basic Elements of Fortran
Integers
Integers are numbers without a decimal point
e.g -999 , +17, 1234 (not allowed: 1,234 , +17 )
No commas may be embedded within an integer constant.
If the number is positive, the sign + is optional, but the - is required to for negative.
Integer variables contain the value of an integer data type.
There is a maximum and a minimum value that an integer can take The range is determined
by how much memory is (in terms of no of bytes) given to a variable of the integer type.
Trang 48Real Numbers
Real (or floating-point) numbers represent values with a fraction
Real constants can be written with or without an exponent e.g 10 , -999.9, 1.0E-3 (i.e 0.001
or 001 )
Not allowed: 1,000 , 111E3, -12.0E1.5, 1.0x
10^-3
Trang 49Chapter 2: Basic Elements of Fortran
The mantissa should contain a decimal point A real variable is a variable that contains the
value of a real data type
There is a maximum and a minimum value that a real var/constant can take The range is
determined by how much memory is (in terms of no of bytes) given to a variable of the real type.
Trang 50A character constant is a string of characters enclosed in a single or double quotes.
Any characters representable on a computer (not just the Fortran characters) are legal in a
character context (i.e if enclosed in quotes).
e.g ‘this is’, ‘ ‘, ‘[^]’, “3.1345”
Trang 51Chapter 2: Basic Elements of Fortran
Mismatching or using an odd number of quotes are common mistakes in programming e.g not correct character: ‘this is , ‘this is”
A character variable is a variable containing a value of character data type e.g c = ‘this is’ Character variables/constants with more than one character are often referred to as strings The character constant '8' is different from the integer constant 8.
Trang 522.5.4, 2.10 Variables and the IMPLICIT NONE
Checking a constant (e.g.7, 3.14156, 'John'), it is easy to determine which type it may be
However, for a variable, we must assign a type to that variable Assigning a type reserves the memory needed to store the data expected (e.g.4 bytes for: 7 , 3.14156 and
2 bytes/letter for: 'John')
Trang 53Chapter 2: Basic Elements of Fortran
There is a default type given to all variables for which a type is not explicitly given This is
used in Fortran versions before Fortran 90 In this case, the first letter of the names of variables or constants determines the type
Prior to Fortran 90: Var/const names with letters i,j,k,l,m,n as first letter imply INTEGER ,
where as the rest of the letters imply REAL
Note: Fortran upper/lower case is the same We use lower case for var’s names e.g i, x, c and upper case for key-words (e.g READ(*,*), STOP).
Trang 54In Fortran 90/95, 2003 we declare the type of
The REAL, INTEGER for all var/constants e.g.
INTEGER :: height
REAL :: second
The IMPLICIT NONE used after the keyword PROGRAM means that we must specify a
type for each variable you declare