What is a Computer?
A simple computer may look like this:
memory (RAM) -- used to store values during execution of a program,
CPU (Central Processor Unit) -- does the `work',
disc drive -- `permanently' stores files,
keyboard -- allows user to input information,
VDU -- visually outputs data,
What is Hardware and Software?
A computer system comprises hardware and software.
Hardware is the physical medium, for example:
circuit boards
processors
keyboard
Software are computer programs, for example:
operating system
editor
compilers
a Fortran 90 program
Telling a Computer What To Do
To get a computer to perform a specific task it must be given a sequence of unambiguous
instructions or a program.
An everyday example is instructions on how to assemble a bedside cabinet. The instructions
must be followed precisely and in the correct order:
1. insert the spigot into hole `A',
2. apply glue along the edge of side panel,
3. press together side and top panels
4. attach toggle pin `B' to gromit `C'
5. ... and so on
The cabinet would turn out wonky if the instructions were not followed `to the letter'!
Some Basic Terminology
Bit is short for Binary Digit. Bits have value of 1 or 0, (corresponds to true or false),
8 Bits make up 1 Byte,
1024 Bytes make up 1 KByte (1K),
1024 KBytes make up 1 MByte (1M),
all machines have a wordsize -- a fundamental unit of storage, for example, 8-bits, 16-
bits, etc.
the size of a word (in Bytes) differs between machines. A Pentium based machine is 32-
bit.
Why 1024? Because
How Does Computer Memory Work?
Here the wordsize is 8-bits:
A computers memory is addressable,
each memory location will contain some sort of `value',
Each location has a specific `number' (represented as hexadecimal [base-16], e.g., 3F2C),
Fortran 90 allows (English) names to be given to memory locations,
the value of a location can be read from or written to.
The CPU can say, `fetch the contents of memory location 3F2C' or `write this value to location
3AF7'.
Numeric Storage
In general, there are two types of numbers used in Fortran 90 programs INTEGER s (whole
numbers) and REAL s (floating point numbers).
INTEGER s are stored exactly, often in range (-32767, 32767).
REAL s are stored approximately.
o They are partitioned into a mantissa and an exponent,
o Exponent can only take a small range of values.
You can get numeric exceptions:
overflow -- exponent is too big,
underflow -- exponent is too small.
In Fortran 90 you can decide what numeric range is to be supported.
CHARACTER s are stored differently.
Programming Languages
Programming languages must be:
totally unambiguous (unlike natural languages, for example, English),
expressive -- it must be fairly easy to program common tasks,
practical -- it must be an easy language for the compiler to translate,
simple to use.
All programming languages have a very precise syntax (or grammar).
This ensures all syntactically-correct programs have a single meaning.
High-level Programming Languages
Assembler code is a Low-Level Language.
Fortran 90, FORTRAN 77, ADA, C and Java are High-Level Languages.
a program is a series of instructions to the CPU,
could write all programs in assembler code but this is a slow, complex and error-prone
process,
high-level languages are more expressive, more secure and quicker to use,
the high-level program is compiled (translated) into assembler code by a compiler.
An Example Problem
To convert from F (Fahrenheit) to C (Centigrade) we can use the following formula:
To convert from C to K (Kelvin) we add 273.
The program would accept a Fahrenheit temperature as input and produce the Centigrade and
Kelvin equivalent as output.
An Example Program
PROGRAM Temp_Conversion
IMPLICIT NONE
INTEGER :: Deg_F, Deg_C, K
PRINT*, "Please type in the temp in F"
READ*, Deg_F
Deg_C = 5*(Deg_F-32)/9
PRINT*, "This is equal to", Deg_C, "C"
K = Deg_C + 273
PRINT*, "and", K, "K"
END PROGRAM Temp_Conversion
This program, called Temp.f90, can be compiled:
chad2-13{adamm} 26> f90 Temp.f90
NAg Fortran 90 compiler v2.2. New Debugger: 'dbx90'
and run:
chad2-13{adamm} 27> a.out
Please type in the temp in F
45
This is equal to 7 C
and 280 K
Analysis of Program
The code is delimited by PROGRAM ... END PROGRAM statements. Between these there are two
distinct areas.
Specification Part
o specifies named memory locations (variables) for use,
o specifies the type of the variable,
Execution Part
o reads in data,
o calculates the temp in C and K and
o prints out results.
A Closer Look at the Specification Part
IMPLICIT NONE -- this should always be present. Means all variables must be declared.
INTEGER :: Deg_F, Deg_C, K -- declares three INTEGER (whole number) variables.
Other variable types:
o REAL -- real numbers, e.g., 3.1459, ,
o LOGICAL -- take vaules .TRUE. or .FALSE.,
o CHARACTER -- contains single alphanumeric character, e.g., 'a',
o CHARACTER(LEN=12) -- contains 12 alphanumeric characters, a string,
Fortran 90 is not case sensitive.
K is the same as k and INTEGER is the same as integer.
A Closer Look at the Execution Part
This is the part of the program that does the actual `work'.
PRINT*, "Please type in the temp in F" -- writes the string to the screen,
READ*, Deg_F -- reads a value from the keyboard and assigns it to the INTEGER variable
Deg_F,
Deg_C = 5*(Deg_F-32)/9 -- the expression on the RHS is evaluated and assigned to the
INTEGER variable Deg_C,
o * is the multiplication operator,
o - is the subtraction operator,
o / is the division operator, (takes longer than *)
o = is the assignment operator.
PRINT*, "This is equal to", Deg_C, "C" -- displays a string on the screen followed
by the value of a variable (Deg_C) followed by a second string ("C").
By default, input is from the keyboard and output to the screen.
How to Write a Computer Program
There are 4 main steps:
1. specify the problem,
2. analyse and break down into a series of steps towards solution,
3. write the Fortran 90 code,
4. compile and run (i.e., test the program).
It may be necessary to iterate between steps 3 and 4 in order to remove any mistakes.
The testing phase is very important.
A Quadratic Equation Solver - The Algorithm
The problem Write a program to calculate the roots of a quadratic equation of the form:
The roots are given by the following formula
The algorithm
1. READ values of a, b and c,
2. if a is zero then stop as we do not have a quadratic,
3. calculate value of discriminant
4. if D is zero then there is one root: ,
5. if D is > 0 then there are two real roots: and ,
6. if D is < 0 there are two complex roots: and ,
7. PRINT solution.
A Quadratic Equation Solver - The Program
PROGRAM QES
IMPLICIT NONE
INTEGER :: a, b, c, D
REAL :: Real_Part, Imag_Part
PRINT*, "Type in values for a, b and c"
READ*, a, b, c
IF (a /= 0) THEN
! Calculate discriminant
D = b*b - 4*a*c
IF (D == 0) THEN ! one root
PRINT*, "Root is ", -b/(2.0*a)
ELSE IF (D > 0) THEN ! real roots
PRINT*, "Roots are",(-b+SQRT(REAL(D)))/(2.0*a),&
"and", (-b-SQRT(REAL(D)))/(2.0*a)
ELSE ! complex roots
Real_Part = -b/(2.0*a)
! D < 0 so must take SQRT of -D
Imag_Part = (SQRT(REAL(-D))/(2.0*a))
PRINT*, "1st Root", Real_Part, "+", Imag_Part, "i"
PRINT*, "2nd Root", Real_Part, "-", Imag_Part, "i"
END IF
ELSE ! a == 0
PRINT*, "Not a quadratic equation"
END IF
END PROGRAM QES
A Quadratic Equation Solver - The Testing Phase
The output from the program is as follows:
uxa{adamm} 35> a.out
Type in values for a, b and c
1 -3 2
Roots are 2.0000000 and 1.0000000
uxa{adamm} 36> a.out
Type in values for a, b and c
1 -2 1
Root is 1.0000000
uxa{adamm} 37> a.out
Type in values for a, b and c
1 1 1
1st Root -0.5000000 + 0.8660254 i
2nd Root -0.5000000 - 0.8660254 i
uxa{adamm} 38> a.out
Type in values for a, b and c
0 2 3
Not a quadratic equation
Its can be seen that the `test data' used above exercises every line of the program. This is important in
demonstrating correctness.