fortran 90 course notes (278p)

Upload: eng-w-ea

Post on 05-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Fortran 90 Course Notes (278P)

    1/278

    T H E U N I V E R S I T Y

    L I V E R P O O L

    F o r t r a n 9 0 C o u r s e N o t e s

    | | |

    A C M a r s h a l l w i t h h e l p f r o m J S M o r g a n a n d J L S c h o n f e l d e r .

    T h a n k s t o P a d d y O ' B r i e n .

    c

    U n i v e r s i t y o f L i v e r p o o l , 1 9 9 7

  • 8/2/2019 Fortran 90 Course Notes (278P)

    2/278

    C o n t e n t s

    1 I n t r o d u c t i o n t o C o m p u t e r S y s t e m s 1

    1 . 1 W h a t i s a C o m p u t e r ? : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1

    1 . 2 W h a t i s H a r d w a r e a n d S o f t w a r e ? : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2

    1 . 3 T e l l i n g a C o m p u t e r W h a t T o D o : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3

    1 . 4 S o m e B a s i c T e r m i n o l o g y : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3

    1 . 5 H o w D o e s C o m p u t e r M e m o r y W o r k ? : : : : : : : : : : : : : : : : : : : : : : : : : : : 4

    1 . 6 N u m e r i c S t o r a g e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5

    2 W h a t A r e C o m p u t e r P r o g r a m s 5

    2 . 1 P r o g r a m m i n g L a n g u a g e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5

    2 . 2 H i g h - l e v e l P r o g r a m m i n g L a n g u a g e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6

    2 . 3 A n E x a m p l e P r o b l e m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6

    2 . 4 A n E x a m p l e P r o g r a m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6

    2 . 5 A n a l y s i s o f T e m p e r a t u r e P r o g r a m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7

    2 . 5 . 1 A C l o s e r L o o k a t t h e S p e c i c a t i o n P a r t : : : : : : : : : : : : : : : : : : : : : 8

    2 . 5 . 2 A C l o s e r L o o k a t t h e E x e c u t i o n P a r t : : : : : : : : : : : : : : : : : : : : : : : 8

    2 . 6 H o w t o W r i t e a C o m p u t e r P r o g r a m : : : : : : : : : : : : : : : : : : : : : : : : : : : 9

    2 . 7 A Q u a d r a t i c E q u a t i o n S o l v e r : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9

    2 . 7 . 1 T h e P r o b l e m S p e c i c a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9

    2 . 7 . 2 T h e A l g o r i t h m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0

    2 . 7 . 3 T h e P r o g r a m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0

    2 . 8 T h e T e s t i n g P h a s e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1

    2 . 8 . 1 D i s c u s s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1

    2 . 9 S o f t w a r e E r r o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2

    2 . 9 . 1 C o m p i l e - t i m e E r r o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2

    2 . 9 . 2 R u n - t i m e E r r o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3

    2 . 1 0 T h e C o m p i l a t i o n P r o c e s s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3

    2 . 1 1 C o m p i l e r S w i t c h e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4

    3 I n t r o d u c t i o n 1 6

    3 . 1 T h e C o u r s e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6

    i

  • 8/2/2019 Fortran 90 Course Notes (278P)

    3/278

    4 F o r t r a n E v o l u t i o n 1 6

    4 . 1 A B r i e f H i s t o r y o f F o r t r a n 7 7 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6

    4 . 2 D r a w b a c k s o f F o r t r a n 7 7 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8

    4 . 3 N e w F o r t r a n 9 0 F e a t u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9

    4 . 4 A d v a n t a g e s o f A d d i t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1

    5 L a n g u a g e O b s o l e s c e n c e 2 1

    5 . 1 O b s o l e s c e n t F e a t u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2

    5 . 1 . 1 A r i t h m e t i c I F S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2

    5 . 1 . 2 A S S I G N S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2

    5 . 1 . 3 A S S I G N e d G O T O S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2

    5 . 1 . 4 A S S I G N e d F O R M A T S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3

    5 . 1 . 5 H o l l e r i t h F o r m a t S t r i n g s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3

    5 . 1 . 6 P A U S E S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3

    5 . 1 . 7 R E A L a n d D O U B L E P R E C I S I O N D O - l o o p V a r i a b l e s : : : : : : : : : : : : : : : : 2 3

    5 . 1 . 8 S h a r e d D O - l o o p T e r m i n a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4

    5 . 1 . 9 A l t e r n a t e R E T U R N : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4

    5 . 1 . 1 0 B r a n c h i n g t o a n E N D I F : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4

    5 . 2 U n d e s i r a b l e F e a t u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4

    5 . 2 . 1 F i x e d S o u r c e F o r m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5

    5 . 2 . 2 I m p l i c i t D e c l a r a t i o n o f V a r i a b l e s : : : : : : : : : : : : : : : : : : : : : : : : : 2 5

    5 . 2 . 3 C O M M O N B l o c k s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5

    5 . 2 . 4 A s s u m e d S i z e A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5

    5 . 2 . 5 E Q U I V A L E N C E S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5

    5 . 2 . 6 E N T R Y S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5

    6 O b j e c t O r i e n t e d P r o g r a m m i n g 2 5

    6 . 1 F o r t r a n 9 0 ' s O b j e c t O r i e n t e d F a c i l i t i e s : : : : : : : : : : : : : : : : : : : : : : : : : : 2 7

    6 . 1 . 1 D a t a A b s t r a c t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 7

    6 . 1 . 2 D a t a H i d i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 8

    6 . 1 . 3 E n c a p s u l a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 8

    6 . 1 . 4 I n h e r i t a n c e a n d E x t e n s i b i l i t y : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 8

    i i

  • 8/2/2019 Fortran 90 Course Notes (278P)

    4/278

    6 . 1 . 5 P o l y m o r p h i s m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 8

    6 . 1 . 6 R e u s a b i l i t y : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 9

    6 . 2 C o m p a r i s o n s w i t h C + + : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 9

    7 F o r t r a n 9 0 P r o g r a m m i n g 3 0

    7 . 1 E x a m p l e o f a F o r t r a n 9 0 P r o g r a m : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 0

    7 . 2 C o d i n g S t y l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 2

    8 L a n g u a g e E l e m e n t s 3 3

    8 . 1 S o u r c e F o r m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 3

    8 . 2 F r e e F o r m a t C o d e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 3

    8 . 3 C h a r a c t e r S e t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 4

    8 . 4 S i g n i c a n t B l a n k s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 5

    8 . 5 C o m m e n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 6

    8 . 6 N a m e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 7

    8 . 7 S t a t e m e n t O r d e r i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 7

    9 D a t a O b j e c t s 4 0

    9 . 1 I n t r i n s i c T y p e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 0

    9 . 2 L i t e r a l C o n s t a n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 1

    9 . 3 I m p l i c i t T y p i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 1

    9 . 4 N u m e r i c a n d L o g i c a l D e c l a r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 2

    9 . 5 C h a r a c t e r D e c l a r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 2

    9 . 6 C o n s t a n t s ( P a r a m e t e r s ) : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 3

    9 . 7 I n i t i a l i s a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 4

    9 . 8 E x a m p l e s o f D e c l a r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 5

    1 0 E x p r e s s i o n s a n d A s s i g n m e n t 4 7

    1 0 . 1 E x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 7

    1 0 . 2 A s s i g n m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 7

    1 0 . 3 I n t r i n s i c N u m e r i c O p e r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 8

    1 0 . 4 R e l a t i o n a l O p e r a t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 9

    1 0 . 5 I n t r i n s i c L o g i c a l O p e r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 0

    1 0 . 6 I n t r i n s i c C h a r a c t e r O p e r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 1

    i i i

  • 8/2/2019 Fortran 90 Course Notes (278P)

    5/278

    1 0 . 6 . 1 C h a r a c t e r S u b s t r i n g s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 1

    1 0 . 6 . 2 C o n c a t e n a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 1

    1 0 . 7 O p e r a t o r P r e c e d e n c e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 2

    1 0 . 8 P r e c e d e n c e E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 3

    1 0 . 9 P r e c i s i o n E r r o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 4

    1 1 C o n t r o l F l o w 5 6

    1 1 . 1 I F S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 7

    1 1 . 2 I F C o n s t r u c t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 8

    1 1 . 3 N e s t e d a n d N a m e d I F C o n s t r u c t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 1

    1 1 . 4 C o n d i t i o n a l E x i t L o o p s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 2

    1 1 . 5 C o n d i t i o n a l C y c l e L o o p s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 3

    1 1 . 6 N a m e d a n d N e s t e d L o o p s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 4

    1 1 . 7 D O W H I L E L o o p s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 5

    1 1 . 8 I n d e x e d D O L o o p : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 5

    1 1 . 8 . 1 E x a m p l e s o f L o o p C o u n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 7

    1 1 . 9 S c o p e o f D O V a r i a b l e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 8

    1 1 . 1 0 S E L E C T C A S E C o n s t r u c t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 8

    1 2 M i x i n g O b j e c t s o f D i e r e n t T y p e s 7 1

    1 2 . 1 M i x e d N u m e r i c T y p e E x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 1

    1 2 . 2 M i x e d T y p e A s s i g n m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 2

    1 2 . 3 I n t e g e r D i v i s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 3

    1 3 I n t r i n s i c P r o c e d u r e s 7 4

    1 3 . 1 T y p e C o n v e r s i o n F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 5

    1 3 . 2 M a t h e m a t i c a l I n t r i n s i c F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 6

    1 3 . 3 N u m e r i c I n t r i n s i c F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 7

    1 3 . 4 C h a r a c t e r I n t r i n s i c F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 0

    1 4 S i m p l e I n p u t / O u t p u t 8 1

    1 4 . 1 P R I N T S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 1

    1 4 . 2 R E A D S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 3

    i v

  • 8/2/2019 Fortran 90 Course Notes (278P)

    6/278

    1 5 A r r a y s 8 4

    1 5 . 1 A r r a y T e r m i n o l o g y : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 5

    1 5 . 2 D e c l a r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 5

    1 5 . 3 A r r a y C o n f o r m a n c e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 7

    1 5 . 4 A r r a y E l e m e n t O r d e r i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 9

    1 5 . 5 A r r a y S y n t a x : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 0

    1 5 . 6 W h o l e A r r a y E x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 1

    1 5 . 7 V i s u a l i s i n g A r r a y S e c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 2

    1 5 . 8 A r r a y S e c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 4

    1 5 . 9 P r i n t i n g A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 5

    1 5 . 1 0 I n p u t o f A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 6

    1 5 . 1 0 . 1 A r r a y I / O E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 6

    1 5 . 1 1 A r r a y I n q u i r y I n t r i n s i c s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 7

    1 5 . 1 2 A r r a y C o n s t r u c t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 8

    1 5 . 1 3 T h e R E S H A P E I n t r i n s i c F u n c t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 9

    1 5 . 1 4 A r r a y C o n s t r u c t o r s i n I n i t i a l i s a t i o n S t a t e m e n t s : : : : : : : : : : : : : : : : : : : : : : 1 0 1

    1 5 . 1 5 A l l o c a t a b l e A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0 2

    1 5 . 1 6 D e a l l o c a t i n g A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0 2

    1 5 . 1 7 M a s k e d A s s i g n m e n t | W h e r e S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : 1 0 4

    1 5 . 1 8 M a s k e d A s s i g n m e n t | W h e r e C o n s t r u c t : : : : : : : : : : : : : : : : : : : : : : : : : 1 0 4

    1 5 . 1 9 V e c t o r - v a l u e d S u b s c r i p t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0 7

    1 6 S e l e c t e d I n t r i n s i c F u n c t i o n s 1 0 9

    1 6 . 1 R a n d o m N u m b e r I n t r i n s i c : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0 9

    1 6 . 2 V e c t o r a n d M a t r i x M u l t i p l y I n t r i n s i c s : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 0

    1 6 . 3 M a x i m u m a n d M i n i m u m I n t r i n s i c s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 2

    1 6 . 4 A r r a y L o c a t i o n I n t r i n s i c s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 3

    1 6 . 5 A r r a y R e d u c t i o n I n t r i n s i c s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 4

    1 7 P r o g r a m U n i t s 1 2 1

    1 7 . 1 M a i n P r o g r a m S y n t a x : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 2

    1 7 . 1 . 1 M a i n P r o g r a m E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 3

    v

  • 8/2/2019 Fortran 90 Course Notes (278P)

    7/278

    1 7 . 2 P r o c e d u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 3

    1 7 . 3 S u b r o u t i n e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 4

    1 7 . 4 F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 5

    1 7 . 5 A r g u m e n t A s s o c i a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 7

    1 7 . 6 L o c a l O b j e c t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 7

    1 7 . 7 A r g u m e n t I n t e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 8

    1 7 . 8 S c o p e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 9

    1 7 . 8 . 1 H o s t A s s o c i a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 9

    1 7 . 8 . 2 E x a m p l e o f S c o p i n g I s s u e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 0

    1 7 . 9 S A V E A t t r i b u t e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 2

    1 7 . 1 0 K e y w o r d A r g u m e n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 3

    1 7 . 1 1 O p t i o n a l A r g u m e n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 4

    1 7 . 1 1 . 1 O p t i o n a l A r g u m e n t s E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 4

    1 8 P r o c e d u r e s a n d A r r a y A r g u m e n t s 1 3 6

    1 8 . 1 E x p l i c i t - s h a p e A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 6

    1 8 . 2 A s s u m e d - s h a p e A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 7

    1 8 . 3 A u t o m a t i c A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 8

    1 8 . 4 S A V E A t t r i b u t e a n d A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 9

    1 8 . 5 E x p l i c i t L e n g t h C h a r a c t e r D u m m y A r g u m e n t s : : : : : : : : : : : : : : : : : : : : : : 1 3 9

    1 8 . 6 A s s u m e d L e n g t h C h a r a c t e r D u m m y A r g u m e n t s : : : : : : : : : : : : : : : : : : : : : 1 4 0

    1 8 . 7 A r r a y - v a l u e d F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 0

    1 8 . 8 C h a r a c t e r - v a l u e d F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 1

    1 8 . 9 S i d e E e c t F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 2

    1 8 . 1 0 R e c u r s i v e P r o c e d u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 3

    1 8 . 1 0 . 1 R e c u r s i v e F u n c t i o n E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 4

    1 8 . 1 0 . 2 R e c u r s i v e S u b r o u t i n e E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 4

    1 9 O b j e c t O r i e n t a t i o n 1 4 5

    1 9 . 1 S t a c k S i m u l a t i o n E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 5

    1 9 . 1 . 1 S t a c k E x a m p l e P r o g r a m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 6

    1 9 . 2 R e u s a b i l i t y | M o d u l e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 7

    v i

  • 8/2/2019 Fortran 90 Course Notes (278P)

    8/278

    1 9 . 3 R e s t r i c t i n g V i s i b i l i t y : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 0

    1 9 . 4 T h e U S E R e n a m e s F a c i l i t y : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 1

    1 9 . 5 U S E O N L Y S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 2

    2 0 M o d u l e s 1 5 2

    2 0 . 1 M o d u l e s | G e n e r a l F o r m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 3

    2 1 P o i n t e r s a n d T a r g e t s 1 5 6

    2 1 . 1 P o i n t e r S t a t u s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 7

    2 1 . 2 P o i n t e r D e c l a r a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 7

    2 1 . 3 T a r g e t D e c l a r a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 8

    2 1 . 4 P o i n t e r M a n i p u l a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 8

    2 1 . 5 P o i n t e r A s s i g n m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 9

    2 1 . 5 . 1 P o i n t e r A s s i g n m e n t E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 9

    2 1 . 6 A s s o c i a t i o n w i t h A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 0

    2 1 . 7 D y n a m i c T a r g e t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 2

    2 1 . 8 A u t o m a t i c P o i n t e r A t t r i b u t i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 2

    2 1 . 9 A s s o c i a t i o n S t a t u s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 4

    2 1 . 1 0 P o i n t e r D i s a s s o c i a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 4

    2 1 . 1 1 P o i n t e r s t o A r r a y s v s . A l l o c a t a b l e A r r a y s : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 5

    2 1 . 1 2 P r a c t i c a l U s e o f P o i n t e r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 5

    2 1 . 1 3 P o i n t e r s a n d P r o c e d u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 7

    2 1 . 1 4 P o i n t e r V a l u e d F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 7

    2 1 . 1 5 P o i n t e r I / O : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6 9

    2 2 D e r i v e d T y p e s 1 6 9

    2 2 . 1 S u p e r t y p e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 0

    2 2 . 2 D e r i v e d T y p e A s s i g n m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 1

    2 2 . 3 A r r a y s a n d D e r i v e d T y p e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 2

    2 2 . 4 D e r i v e d T y p e I / O : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 3

    2 2 . 5 D e r i v e d T y p e s a n d P r o c e d u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 3

    2 2 . 6 D e r i v e d T y p e V a l u e d F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 4

    2 2 . 7 P O I N T E R C o m p o n e n t s o f D e r i v e d T y p e s : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 6

    v i i

  • 8/2/2019 Fortran 90 Course Notes (278P)

    9/278

    2 2 . 8 P o i n t e r s a n d L i s t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 7

    2 2 . 8 . 1 L i n k e d L i s t E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 8

    2 2 . 9 A r r a y s o f P o i n t e r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8 1

    2 3 M o d u l e s | T y p e a n d P r o c e d u r e P a c k a g i n g 1 8 4

    2 3 . 1 D e r i v e d T y p e C o n s t r u c t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8 5

    2 3 . 2 G e n e r i c P r o c e d u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8 6

    2 3 . 3 G e n e r i c I n t e r f a c e s | C o m m e n t a r y : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8 7

    2 3 . 4 D e r i v e d T y p e I / O : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8 8

    2 3 . 5 O v e r l o a d i n g I n t r i n s i c P r o c e d u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8 8

    2 3 . 6 O v e r l o a d i n g O p e r a t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 0

    2 3 . 6 . 1 O p e r a t o r O v e r l o a d i n g E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 0

    2 3 . 7 D e n i n g N e w O p e r a t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 2

    2 3 . 7 . 1 D e n e d O p e r a t o r E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 2

    2 3 . 7 . 2 P r e c e d e n c e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 4

    2 3 . 8 U s e r - d e n e d A s s i g n m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 4

    2 3 . 8 . 1 D e n e d A s s i g n m e n t E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 4

    2 3 . 8 . 2 S e m a n t i c E x t e n s i o n E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 5

    2 3 . 8 . 3 Y e t A n o t h e r E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 6

    2 3 . 8 . 4 M o r e o n O b j e c t O r i e n t e d P r o g r a m m i n g b y J . S . M o r g a n : : : : : : : : : : : : 2 0 0

    2 3 . 9 S e m a n t i c E x t e n s i o n M o d u l e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0 5

    2 3 . 9 . 1 S e m a n t i c E x t e n s i o n E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0 5

    2 4 C o m p l e x D a t a T y p e 2 0 9

    2 4 . 1 C o m p l e x I n t r i n s i c s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1 0

    2 5 P a r a m e t e r i s e d I n t r i n s i c T y p e s 2 1 5

    2 5 . 1 I n t e g e r D a t a T y p e b y K i n d : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1 6

    2 5 . 2 C o n s t a n t s o f S e l e c t e d I n t e g e r K i n d : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1 6

    2 5 . 3 R e a l K I N D S e l e c t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1 7

    2 5 . 4 K i n d F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1 7

    2 5 . 5 E x p r e s s i o n E v a l u a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1 8

    2 5 . 6 L o g i c a l K I N D S e l e c t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1 8

    v i i i

  • 8/2/2019 Fortran 90 Course Notes (278P)

    10/278

    2 5 . 7 C h a r a c t e r K I N D S e l e c t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 1 9

    2 5 . 8 K i n d s a n d P r o c e d u r e A r g u m e n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2 0

    2 5 . 9 K i n d s a n d G e n e r i c I n t e r f a c e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2 0

    2 6 M o r e I n t r i n s i c s 2 2 6

    2 6 . 1 B i t M a n i p u l a t i o n I n t r i n s i c F u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2 6

    2 6 . 2 A r r a y C o n s t r u c t i o n I n t r i n s i c s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2 7

    2 6 . 2 . 1 M E R G E I n t r i n s i c : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2 8

    2 6 . 2 . 2 S P R E A D I n t r i n s i c : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2 9

    2 6 . 2 . 3 P A C K I n t r i n s i c : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2 9

    2 6 . 2 . 4 U N P A C K I n t r i n s i c : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3 0

    2 6 . 3 T R A N S F E R I n t r i n s i c : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3 1

    2 7 I n p u t / O u t p u t 2 3 3

    2 7 . 1 O P E N S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3 3

    2 7 . 2 R E A D S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3 5

    2 7 . 3 W R I T E S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3 6

    2 7 . 4 F O R M A T S t a t e m e n t / F M T = S p e c i e r : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3 6

    2 7 . 5 E d i t D e s c r i p t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3 7

    2 7 . 6 O t h e r I / O S t a t e m e n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 3 8

    2 8 E x t e r n a l P r o c e d u r e s 2 4 0

    2 8 . 1 E x t e r n a l S u b r o u t i n e S y n t a x : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 1

    2 8 . 1 . 1 E x t e r n a l S u b r o u t i n e E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 2

    2 8 . 2 E x t e r n a l F u n c t i o n S y n t a x : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 2

    2 8 . 2 . 1 F u n c t i o n E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 3

    2 8 . 3 P r o c e d u r e I n t e r f a c e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 4

    2 8 . 3 . 1 I n t e r f a c e E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 6

    2 8 . 4 R e q u i r e d I n t e r f a c e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 6

    2 8 . 5 P r o c e d u r e A r g u m e n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 7

    2 8 . 5 . 1 T h e I N T R I N S I C A t t r i b u t e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 7

    2 8 . 5 . 2 T h e E X T E R N A L A t t r i b u t e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 8

    2 8 . 5 . 3 P r o c e d u r e A r g u m e n t s E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4 9

    i x

  • 8/2/2019 Fortran 90 Course Notes (278P)

    11/278

    2 9 O b j e c t I n i t i a l i s a t i o n 2 5 0

    2 9 . 1 D A T A S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 0

    2 9 . 1 . 1 D A T A S t a t e m e n t E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 0

    2 9 . 2 D a t a S t a t e m e n t | I m p l i e d D O L o o p : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 1

    3 0 H a n d l i n g E x c e p t i o n s 2 5 2

    3 0 . 1 G O T O S t a t e m e n t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 2

    3 0 . 1 . 1 G O T O S t a t e m e n t E x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 2

    3 0 . 2 R E T U R N a n d S T O P S t a t e m e n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 3

    3 1 F o r t r a n 9 5 2 5 4

    3 1 . 1 R a t i o n a l e ( b y C r a i g D e d o ) : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 4

    3 1 . 1 . 1 F O R A L L : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 4

    3 1 . 1 . 2 N e s t e d W H E R E C o n s t r u c t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 5

    3 1 . 1 . 3 P U R E P r o c e d u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 5

    3 1 . 1 . 4 E l e m e n t a l P r o c e d u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 5

    3 1 . 1 . 5 I m p r o v e d I n i t i a l i s a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 5

    3 1 . 1 . 6 A u t o m a t i c D e a l l o c a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 5

    3 1 . 1 . 7 N e w I n i t i a l i s a t i o n F e a t u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 6

    3 1 . 1 . 8 R e m o v e C o n i c t s W i t h I E C 5 5 9 : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 6

    3 1 . 1 . 9 M i n i m u m W i d t h E d i t i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 7

    3 1 . 1 . 1 0 N a m e l i s t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 7

    3 1 . 1 . 1 1 C P U T I M E I n t r i n s i c S u b r o u t i n e : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 7

    3 1 . 1 . 1 2 M A X L O C a n d M I N L O C I n t r i n s i c s : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 7

    3 1 . 1 . 1 3 D e l e t e d F e a t u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 7

    3 1 . 1 . 1 4 N e w O b s o l e s c e n t F e a t u r e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 8

    3 1 . 1 . 1 5 L a n g u a g e T i d y - u p s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 8

    3 2 H i g h P e r f o r m a n c e F o r t r a n 2 5 8

    3 2 . 1 C o m p i l e r D i r e c t i v e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 5 9

    3 2 . 2 V i s u a l i s a t i o n o f D a t a D i r e c t i v e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 6 0

    3 3 A S C I I C o l l a t i n g S e q u e n c e 2 6 5

    x

  • 8/2/2019 Fortran 90 Course Notes (278P)

    12/278

    R e f e r e n c e s 2 6 6

    x i

  • 8/2/2019 Fortran 90 Course Notes (278P)

    13/278

    M o d u l e 1 :

    F u n d a m e n t a l s O f C o m p u t e r

    P r o g r a m m i n g

    1 I n t r o d u c t i o n t o C o m p u t e r S y s t e m s

    1 . 1 W h a t i s a C o m p u t e r ?

    T h e f o l l o w i n g s c h e m a t i c d i a g r a m g i v e s t h e l a y o u t o f a P e r s o n a l C o m p u t e r ( P C ) , m o s t s i n g l e u s e r

    s y s t e m s f o l l o w t h i s g e n e r a l d e s i g n :

    VDU

    Disc drive

    CPU

    Main memory

    Keyboard

    T h e c o m p o n e n t s p e r f o r m t h e f o l l o w i n g t a s k s :

    2 C P U ( C e n t r a l P r o c e s s o r U n i t ) | d o e s t h e ` w o r k ' , f e t c h e s , s t o r e s a n d m a n i p u l a t e s v a l u e s

    t h a t a r e s t o r e d i n t h e c o m p u t e r s m e m o r y . P r o c e s s o r s c o m e i n a l l d i e r e n t ` s h a p e s a n d s i z e s ' |

    t h e r e a r e m a n y d i e r e n t t y p e s o f a r c h i t e c t u r e s w h i c h a r e s u i t e d t o a v a r i e t y o f d i e r e n t t a s k s .

    W e d o n o t c o n s i d e r a n y p a r t i c u l a r t y p e o f C P U i s t h i s c o u r s e .

    2 M a i n m e m o r y ( R A M | R a n d o m A c c e s s M e m o r y ) | u s e d t o s t o r e v a l u e s d u r i n g

    e x e c u t i o n o f a p r o g r a m . I t c a n b e w r i t t e n t o a n d r e a d f r o m a t a n y t i m e .

    1

  • 8/2/2019 Fortran 90 Course Notes (278P)

    14/278

    2 1 . I n t r o d u c t i o n t o C o m p u t e r S y s t e m s

    2 D i s c d r i v e ( h a r d o r o p p y ) | ` p e r m a n e n t l y ' s t o r e s l e s ( p r o g r a m s a n d d a t a ) . H a r d d i s c s

    a r e g e n e r a l l y l o c a t e d i n s i d e t h e m a c h i n e a n d c o m e i n a v a r i e t y o f d i e r e n t s i z e s a n d s p e e d s .

    T h e y d o n o t , i n f a c t , s t o r e l e s p e r m a n e n t l y | t h e y o f t e n g o w r o n g a n d s o m u s t u n d e r g o a

    b a c k - u p a t r e g u l a r i n t e r v a l s . T h e o p p y d i s c d r i v e a l l o w s a u s e r t o m a k e h i s o r h e r o w n b a c k

    u p o f i m p o r t a n t l e s a n d d a t a . I t i s v e r y i m p o r t a n t t o k e e p b a c k - u p s . D o n o t b e c a u g h t o u t |

    y o u m a y w e l l l o s e a l l y o u r w o r k !

    2 K e y b o a r d | a l l o w s u s e r t o i n p u t i n f o r m a t i o n . N o w a d a y s , m o s t k e y b o a r d s h a v e m o r e o r l e s s

    t h e s a m e f u n c t i o n a l i t y .

    2 V D U ( V i s u a l D i s p l a y U n i t ) | v i s u a l l y o u t p u t s d a t a . T h e r e a r e n u m e r o u s t y p e s o f V D U

    d i e r i n g i n t h e r e s o l u t i o n ( d o t s p e r i n c h ) a n d t h e n u m b e r o f c o l o u r s t h a t c a n b e r e p r e s e n t e d .

    2 P r i n t e r | a l l o w s a h a r d c o p y t o b e m a d e . A g a i n , t h e r e a r e m a n y d i e r e n t t y p e s o f p r i n t e r s

    a v a i l a b l e , f o r e x a m p l e , l i n e p r i n t e r s , d o t - m a t r i x p r i n t e r s , b u b b l e j e t p r i n t e r s a n d l a s e r p r i n t e r s .

    T h e s e a l s o d i e r i n t h e i r r e s o l u t i o n a n d c o l o u r p a l e t t e .

    T h e l a s t f o u r a r e k n o w n a s p e r i p h e r a l d e v i c e s

    A g o o d P C c o u l d c o n t a i n :

    2 I n t e l P e n t i u m P 1 6 6 C P U

    2 3 2 M B y t e s R A M ( m a i n m e m o r y )

    2 2 . 1 G B y t e h a r d d i s c

    2 S V G A m o n i t o r

    2 I B M P C k e y b o a r d

    I n a d d i t i o n a s y s t e m m a y i n c l u d e ,

    2 p r i n t e r , f o r e x a m p l e , a n H P L a s e r J e t

    2 s o u n d c a r d a n d s p e a k e r s

    2 C D R O M d r i v e ( R e a d O n l y M e m o r y )

    2 S C S I ( ` s c u z z y ' ) d i s c ( f a s t ) ,

    2 o p p y d i s c d r i v e ( f o r b a c k i n g u p d a t a )

    2 n e t w o r k c a r d

    1 . 2 W h a t i s H a r d w a r e a n d S o f t w a r e ?

    A c o m p u t e r s y s t e m i s m a d e u p f r o m h a r d w a r e a n d s o f t w a r e

    H a r d w a r e i s t h e p h y s i c a l m e d i u m , f o r e x a m p l e :

    2 c i r c u i t b o a r d s

    2 p r o c e s s o r s

    S e p t e m b e r 2 , 1 9 9 7 U n i v e r s i t y o f L i v e r p o o l

  • 8/2/2019 Fortran 90 Course Notes (278P)

    15/278

    1 . 3 . T e l l i n g a C o m p u t e r W h a t T o D o 3

    2 k e y b o a r d

    A p i e c e o f s o f t w a r e i s a c o m p u t e r p r o g r a m , f o r e x a m p l e :

    2 a n o p e r a t i n g s y s t e m

    2 a n e d i t o r

    2 a c o m p i l e r

    2 a F o r t r a n 9 0 p r o g r a m

    T h e s o f t w a r e a l l o w s t h e h a r d w a r e t o b e u s e d . P r o g r a m s v a r y e n o r m o u s l y i n s i z e a n d c o m p l e x i t y .

    1 . 3 T e l l i n g a C o m p u t e r W h a t T o D o

    T o g e t a c o m p u t e r t o p e r f o r m a s p e c i c t a s k i t m u s t b e g i v e n a s e q u e n c e o f u n a m b i g u o u s i n s t r u c t i o n s

    o r a p r o g r a m

    W e m e e t m a n y e x a m p l e s o f p r o g r a m s i n e v e r y d a y l i f e , f o r e x a m p l e , i n s t r u c t i o n s o n h o w t o a s s e m b l e a

    b e d s i d e c a b i n e t . T h e s e i n s t r u c t i o n s a r e g e n e r a l l y n u m b e r e d , m e a n i n g t h a t t h e r e i s a s p e c i c o r d e r t o

    b e f o l l o w e d , t h e y a r e a l s o ( s u p p o s e d t o b e ) p r e c i s e s o t h a t t h e r e i s n o c o n f u s i o n a b o u t w h a t i s i n t e n d e d :

    1 . i n s e r t t h e s p i g o t i n t o h o l e ` A ' ,

    2 . a p p l y g l u e a l o n g t h e e d g e o f s i d e p a n e l ,

    3 . p r e s s t o g e t h e r s i d e a n d t o p p a n e l s

    4 . a t t a c h t o g g l e p i n ` B ' t o g r o m i t ` C '

    5 . . . . a n d s o o n

    I f t h e s e i n s t r u c t i o n s a r e n o t f o l l o w e d ` t o t h e l e t t e r ' , t h e n t h e c a b i n e t w o u l d t u r n o u t w o n k y .

    1 . 4 S o m e B a s i c T e r m i n o l o g y

    I t i s n e c e s s a r y t o c o v e r s o m e t e r m i n o l o g y . H o p e f u l l y , m u c h o f i t w i l l b e f a m i l i a r | y o u w i l l h e a r m a n y

    o f t h e t e r m s u s e d t h r o u g h o u t t h e c o u r s e .

    2 B i t i s s h o r t f o r B i n a r y D i g i t . B i t s h a v e v a l u e o f 1 o r 0 , ( o r o n o r o , o r , t r u e o r f a l s e ) ,

    2 8 B i t s m a k e u p 1 B y t e ,

    1 0 2 4 B y t e s m a k e u p 1 K B y t e ( 1 K i l o B y t e o r 1 K ) , ( \ W h y 1 0 2 4 ? " I h e a r y o u a s k . B e c a u s e

    2

    1 0

    = 1 0 2 4

    1 0 2 4 K B y t e s m a k e u p 1 M B y t e ( 1 M a g a B y t e o r 1 M ) ,

    1 0 2 4 M B y t e s m a k e u p 1 G B y t e ( 1 G i g a B y t e o r 1 G ) ,

    2 a l l m a c h i n e s h a v e a w o r d s i z e | a f u n d a m e n t a l u n i t o f s t o r a g e , f o r e x a m p l e , 8 - b i t s , 1 6 - b i t s , e t c .

    T h e s i z e o f a w o r d ( i n B y t e s ) d i e r s b e t w e e n m a c h i n e s . A P e n t i u m b a s e d m a c h i n e i s 3 2 - b i t .

    F o r t r a n 9 0 C o u r s e N o t e s F i l e : C o u r s e N o t e s . p s

  • 8/2/2019 Fortran 90 Course Notes (278P)

    16/278

    4 1 . I n t r o d u c t i o n t o C o m p u t e r S y s t e m s

    2 a o p i s a o a t i n g p o i n t o p e r a t i o n p e r s e c o n d . A o a t i n g p o i n t o p e r a t i o n o c c u r s w h e n t w o r e a l

    n u m b e r s a r e a d d e d . T o d a y , w e t a l k o f m e g a o p s o r e v e n g i g a o p s .

    2 p a r a l l e l p r o c e s s i n g o c c u r s w h e n t w o o r m o r e C P U s w o r k o n s o l u t i o n o f t h e s a m e p r o b l e m a t

    t h e s a m e t i m e .

    1 . 5 H o w D o e s C o m p u t e r M e m o r y W o r k ?

    I n t h i s h y p o t h e t i c a l e x a m p l e , w o r d s i z e i s t a k e n t o b e 8 - b i t s :

    3F29 3F2A

    3F2B 3F2C

    1 0 1 0 1 1 1 0

    A c o m p u t e r s m e m o r y i s a d d r e s s a b l e . E a c h l o c a t i o n i s g i v e n a s p e c i c ` n u m b e r ' w h i c h i s o f t e n r e p r e -

    s e n t e d i n h e x a d e c i m a l b a s e - 1 6 ] , f o r e x a m p l e , 3 F 2 C . ( H e x a d e c i m a l d i g i t s a r e a s f o l l o w s : 0 , 1 , 2 , 3 , . . . ,

    9 , A , B , C , D , E , F , 1 0 , 1 1 , . . . , 1 9 , 1 A , 1 B , . . . , 1 F , 2 0 , . . . ) . T h e C P U i s a b l e t o r e a d t o a n d w r i t e f r o m

    a s p e c i e d m e m o r y l o c a t i o n a t w i l l . G r o u p s o f m e m o r y l o c a t i o n s c a n b e t r e a t e d a s a w h o l e t o a l l o w

    m o r e i n f o r m a t i o n t o b e s t o r e d . U s i n g t h e c r y p t i c h e x a d e c i m a l i d e n t i e r s f o r m e m o r y l o c a t i o n s i s v e r y

    a w k w a r d s o F o r t r a n 9 0 a l l o w s ( E n g l i s h ) n a m e s t o b e u s e d i n s t e a d , t h i s a l l o w s p r o g r a m s t o m a k e s e n s e

    t o a c a s u a l r e a d e r .

    E v e n w h e n t h e c o m p u t e r h a s j u s t b e e n t u r n e d o n , e a c h m e m o r y l o c a t i o n w i l l c o n t a i n s o m e s o r t o f

    ` v a l u e ' . I n t h i s c a s e t h e v a l u e s w i l l b e r a n d o m . I n t h e g e n e r a l c a s e t h e v a l u e s w i l l b e t h o s e t h a t r e m a i n

    f r o m t h e p r e v i o u s p r o g r a m t h a t u s e d t h e m e m o r y . F o r t h i s r e a s o n i t i s v e r y i m p o r t a n t t o i n i t i a l i s e

    m e m o r y l o c a t i o n s b e f o r e t r y i n g t o u s e t h e v a l u e s t h a t t h e y a r e s t o r i n g .

    A l l C P U s h a v e a n i n s t r u c t i o n s e t ( o r l a n g u a g e ) t h a t t h e y u n d e r s t a n d . E v e n t u a l l y a l l F o r t r a n 9 0

    p r o g r a m s m u s t b e t r a n s l a t e d ( o r c o m p i l e d ) i n t o i n s t r u c t i o n s f r o m t h i s s e t . R o u g h l y s p e a k i n g , a l l

    p r o c e s s o r s h a v e t h e s a m e s o r t o f i n s t r u c t i o n s a v a i l a b l e t o t h e m . T h e C P U c a n s a y t h i n g s l i k e , ` f e t c h

    m e t h e c o n t e n t s o f m e m o r y l o c a t i o n 3 F 2 C ' o r ` w r i t e t h i s v a l u e t o l o c a t i o n 3 A F 7 ' . T h i s i s t h e b a s i s o f

    h o w a l l p r o g r a m s w o r k .

    C o n s i d e r t h e f o l l o w i n g s e q u e n c e o f a s s e m b l e r c o d e i n s t r u c t i o n s :

    L D A ' 3 F 2 C ' l o a d ( f e t c h ) t h e c o n t e n t s o f 3 F 2 C

    A D D ' 3 F 2 9 ' a d d t o t h i s t h e c o n t e n t s o f 3 F 2 9

    S T O ' 3 F 2 A ' s t o r e t h e v a l u e i n l o c a t i o n 3 F 2 A

    T h e s e q u e n c e o f i n s t r u c t i o n s , w h i c h i s m e a n t o n l y f o r i l l u s t r a t i v e p u r p o s e s , e e c t i v e l y a d d s t w o n u m b e r s

    t o g e t h e r a n d s t o r e s t h e r e s u l t i n a s e p a r a t e m e m o r y l o c a t i o n . U n t i l 1 9 5 4 w h e n t h e r s t d i a l e c t o f F o r t r a n

    w a s d e v e l o p e d , a l l c o m p u t e r p r o g r a m s w e r e w r i t t e n u s i n g a s s e m b l e r c o d e . I t w a s J o h n B a c k u s , t h e n

    w o r k i n g f o r I B M , w h o p r o p o s e d t h a t a m o r e e c o n o m i c a l a n d e c i e n t m e t h o d o f p r o g r a m m i n g t h e i r

    c o m p u t e r s h o u l d b e d e v e l o p e d . T h e i d e a w a s t o d e s i g n a l a n g u a g e t h a t m a d e i t p o s s i b l e t o e x p r e s s

    m a t h e m a t i c a l f o r m u l a e i n a m o r e n a t u r a l w a y t h a n t h a t c u r r e n t l y s u p p o r t e d b y a s s e m b l e r l a n g u a g e s .

    T h e r e s u l t o f t h e i r e x p e r i m e n t w a s F o r t r a n ( s h o r t f o r I B M M a t h e m a t i c a l F o r m u l a T r a n s l a t i o n S y s t e m ) .

    T h i s n e w l a n g u a g e a l l o w e d t h e a b o v e a s s e m b l e r i n s t r u c t i o n s t o b e w r i t t e n l e s s c r y p t i c a l l y a s , f o r

    e x a m p l e :

    K = I + J

    S e p t e m b e r 2 , 1 9 9 7 U n i v e r s i t y o f L i v e r p o o l

  • 8/2/2019 Fortran 90 Course Notes (278P)

    17/278

    1 . 6 . N u m e r i c S t o r a g e 5

    A c o m p i l e r w o u l d t h e n t r a n s l a t e t h e a b o v e a s s i g n m e n t s t a t e m e n t i n t o s o m e t h i n g t h a t l o o k s l i k e t h e

    a s s e m b l e r c o d e g i v e n a b o v e .

    1 . 6 N u m e r i c S t o r a g e

    I n g e n e r a l , t h e r e a r e t w o t y p e s o f n u m b e r s u s e d i n F o r t r a n 9 0 p r o g r a m s I N T E G E R s ( w h o l e n u m b e r s )

    a n d R E A L s ( o a t i n g p o i n t n u m b e r s ) .

    I N T E G E R s a r e s t o r e d e x a c t l y , u s u a l l y i n r a n g e ( - 3 2 7 6 7 , 3 2 7 6 7 ) , h o w e v e r , t h e y a r e n o t s t o r e d i n t h e

    f o r m a t h i n t e d a t i n t h e p r e v i o u s s e c t i o n b u t i n a s p e c i a l w a y t h a t a l l o w s a d d i t i o n a n d s u b t r a c t i o n t o

    b e p e r f o r m e d i n a s t r a i g h t f o r w a r d w a y .

    R E A L s a r e s t o r e d a p p r o x i m a t e l y a n d t h e s p a c e t h e y o c c u p y i s p a r t i t i o n e d i n t o a m a n t i s s a a n d a n

    e x p o n e n t . ( I n t h e f o l l o w i n g n u m b e r : 0 : 3 1 4 5 9 1 0

    1

    , t h e m a n t i s s a i s 0 : 3 1 4 5 9 a n d t h e e x p o n e n t i s 1 )

    I n a R E A L n u m b e r , t h e e x p o n e n t c a n o n l y t a k e a s m a l l r a n g e o f v a l u e s | i f t h e e x p o n e n t i s e x p e c t e d

    t o b e l a r g e t h e n i t m a y b e n e c e s s a r y t o u s e a n u m e r i c s t o r a g e u n i t t h a t i s c a p a b l e o f r e p r e s e n t i n g l a r g e r

    v a l u e s , f o r e x a m p l e D O U B L E P R E C I S I O N v a l u e s .

    I t i s p o s s i b l e f o r a p r o g r a m t o t h r o w a n u m e r i c e x c e p t i o n s u c h a s o v e r o w w h i c h o c c u r s w h e n

    n u m b e r i s o u t s i d e o f s u p p o r t e d r a n g e ( f o r e x a m p l e , a r e a l n u m b e r w h e r e e x p o n e n t i s t o o b i g ) o r

    u n d e r o w w h i c h i s t h e o p p o s i t e , t h e n u m b e r i s t o o c l o s e t o z e r o f o r r e p r e s e n t a t i o n ( f o r e x a m p l e , a

    r e a l n u m b e r w h e r e t h e e x p o n e n t i s t o o s m a l l ) .

    I n F o r t r a n 9 0 , t h e K I N D m e c h a n i s m a l l o w s t h e u s e r t o s p e c i f y w h a t n u m e r i c r a n g e i s t o b e s u p p o r t e d .

    T h i s a l l o w s p r o g r a m s t o b e n u m e r i c a l l y p o r t a b l e . D i e r e n t c o m p u t e r s y s t e m s w i l l a l l o w n u m b e r s o f

    d i e r i n g s i z e s t o b e r e p r e s e n t e d b y o n e w o r d . I t i s p o s s i b l e t h a t w h e n a p r o g r a m h a s b e e n m o v e d f r o m

    o n e c o m p u t e r s y s t e m t o a n o t h e r , i t m a y f a i l t o e x e c u t e c o r r e c t l y o n t h e n e w s y s t e m b e c a u s e u n d e r o w

    o r o v e r o w e x c e p t i o n s a r e g e n e r a t e d . T h e K I N D m e c h a n i s m c o m b a t s t h i s s e r i o u s p r o b l e m .

    C H A R A C T E R s v a r i a b l e s a r e s t o r e d d i e r e n t l y .

    2 W h a t A r e C o m p u t e r P r o g r a m s

    2 . 1 P r o g r a m m i n g L a n g u a g e s

    C o m p u t e r s o n l y u n d e r s t a n d b i n a r y c o d e s s o t h e r s t p r o g r a m s w e r e w r i t t e n u s i n g t h i s n o t a t i o n . T h i s

    f o r m o f p r o g r a m m i n g w a s s o o n s e e n t o b e e x t r e m e l y c o m p l e x a n d e r r o r p r o n e s o a s s e m b l e r l a n g u a g e s

    w e r e d e v e l o p e d . S o o n i t w a s r e a l i s e d t h a t e v e n a s s e m b l e r l a n g u a g e s c o u l d b e i m p r o v e d o n . T o d a y , a

    g o o d p r o g r a m m i n g l a n g u a g e m u s t b e :

    2 t o t a l l y u n a m b i g u o u s ( u n l i k e n a t u r a l l a n g u a g e s , f o r e x a m p l e , E n g l i s h | ` o l d w o m e n a n d m e n

    s u c k e g g s ' , d o e s t h i s m e a n t h a t m e n o r o l d m e n s u c k e g g s ? ) .

    2 e x p r e s s i v e | i t m u s t b e f a i r l y e a s y t o p r o g r a m c o m m o n t a s k s ,

    2 p r a c t i c a l | i t m u s t b e a n e a s y l a n g u a g e f o r t h e c o m p i l e r t o t r a n s l a t e ,

    2 s i m p l e t o u s e .

    A l l p r o g r a m m i n g l a n g u a g e s h a v e a v e r y p r e c i s e s y n t a x ( o r g r a m m a r ) . T h i s w i l l e n s u r e t h a t a

    s y n t a c t i c a l l y - c o r r e c t p r o g r a m o n l y h a s a s i n g l e m e a n i n g .

    F o r t r a n 9 0 C o u r s e N o t e s F i l e : C o u r s e N o t e s . p s

  • 8/2/2019 Fortran 90 Course Notes (278P)

    18/278

    6 2 . W h a t A r e C o m p u t e r P r o g r a m s

    2 . 2 H i g h - l e v e l P r o g r a m m i n g L a n g u a g e s

    A s s e m b l e r c o d e i s a L o w - L e v e l L a n g u a g e . I t i s s o - c a l l e d b e c a u s e t h e s t r u c t u r e o f t h e l a n g u a g e

    r e e c t s t h e i n s t r u c t i o n s e t ( a n d a r c h i t e c t u r e ) o f t h e C P U . A p r o g r a m m e r c a n g e t v e r y c l o s e t o t h e

    p h y s i c a l h a r d w a r e . L o w - l e v e l l a n g u a g e s a l l o w v e r y e c i e n t u s e o f t h e m a c h i n e b u t a r e d i c u l t t o u s e .

    F o r t r a n 9 0 , F o r t r a n 7 7 , A D A , C a n d J a v a a r e e x a m p l e s o f H i g h - L e v e l L a n g u a g e s . T h e y p r o v i d e a

    m u c h h i g h e r d e g r e e o f a b s t r a c t i o n f r o m t h e p h y s i c a l h a r d w a r e . T h e y a l s o a l l o w f o r p o r t a b l e p r o g r a m s

    t o b e w r i t t e n , i . e . , p r o g r a m s t h a t w i l l r u n o n a v a r i e t y o f d i e r e n t s y s t e m s a n d w h i c h w i l l p r o d u c e t h e

    s a m e r e s u l t s r e g a r d l e s s o f t h e p l a t f o r m . A s w e l l a s h a v i n g t h e a b o v e b e n e t s , h i g h - l e v e l l a n g u a g e s a r e

    m o r e e x p r e s s i v e a n d s e c u r e a n d a r e m u c h q u i c k e r t o u s e t h a n l o w - l e v e l l a n g u a g e s . I n g e n e r a l , h o w e v e r ,

    a w e l l w r i t t e n a s s e m b l e r p r o g r a m w i l l r u n f a s t e r t h a n a h i g h - l e v e l p r o g r a m t h a t p e r f o r m s t h e s a m e t a s k .

    A t t h e e n d o f t h e d a y , a n e x e c u t a b l e p r o g r a m t h a t r u n s o n a C P U m u s t s t i l l b e r e p r e s e n t e d a s a

    s e r i e s o f b i n a r y d i g i t s . T h i s i s a c h i e v e d b y c o m p i l i n g ( t r a n s l a t i n g ) a h i g h - l e v e l p r o g r a m w i t h a s p e c i a l

    p i e c e o f s o f t w a r e c a l l e d a c o m p i l e r . C o m p i l e r s a r e i n c r e d i b l y c o m p l i c a t e d p r o g r a m s t h a t a c c e p t o t h e r

    p r o g r a m s a s i n p u t a n d g e n e r a t e a b i n a r y e x e c u t a b l e o b j e c t l e a s o u t p u t .

    2 . 3 A n E x a m p l e P r o b l e m

    C o n s i d e r t h e f o l l o w i n g p r o b l e m w h i c h i s s u i t a b l e f o r s o l u t i o n b y c o m p u t e r .

    T o c o n v e r t f r o m

    o

    F ( F a h r e n h e i t ) t o

    o

    C ( C e n t i g r a d e ) w e c a n u s e t h e f o l l o w i n g f o r m u l a :

    c = 5 ( f ; 3 2 ) = 9

    T o c o n v e r t f r o m

    o

    C t o K ( K e l v i n ) w e a d d 2 7 3 .

    A s p e c i c a t i o n o f t h e p r o g r a m c o u l d b e t h a t i t w o u l d p r o m p t t h e u s e r f o r a t e m p e r a t u r e e x p r e s s e d i n

    d e g r e e s F a h r e n h e i t , p e r f o r m t h e n e c e s s a r y c a l c u l a t i o n s a n d p r i n t o u t t h e e q u i v a l e n t t e m p e r a t u r e s i n

    C e n t i g r a d e a n d K e l v i n .

    2 . 4 A n E x a m p l e P r o g r a m

    A p r o g r a m w h i c h f o l l o w s t h e a b o v e s p e c i c a t i o n i s g i v e n b e l o w .

    P R O G R A M T e m p _ C o n v e r s i o n

    I M P L I C I T N O N E

    I N T E G E R : : D e g _ F , D e g _ C , K

    P R I N T * , " P l e a s e t y p e i n t h e t e m p i n F "

    R E A D * , D e g _ F

    D e g _ C = 5 * ( D e g _ F - 3 2 ) / 9

    P R I N T * , " T h i s i s e q u a l t o " , D e g _ C , " C "

    K = D e g _ C + 2 7 3

    P R I N T * , " a n d " , K , " K "

    E N D P R O G R A M T e m p _ C o n v e r s i o n

    T h i s p r o g r a m , i n t h i s c a s e , c a l l e d T e m p . f 9 0 , c a n b e c o m p i l e d u s i n g t h e N A g v 2 . 2 F o r t r a n 9 0 c o m p i l e r .

    T h e c o m p i l e r i s i n v o k e d o n t h e U n i x c o m m a n d l i n e a s f o l l o w s :

    S e p t e m b e r 2 , 1 9 9 7 U n i v e r s i t y o f L i v e r p o o l

  • 8/2/2019 Fortran 90 Course Notes (278P)

    19/278

    2 . 5 . A n a l y s i s o f T e m p e r a t u r e P r o g r a m 7

    c h a d 2 - 1 3 { a d a m m } 2 6 > f 9 0 T e m p . f 9 0

    N A g F o r t r a n 9 0 c o m p i l e r v 2 . 2 . N e w D e b u g g e r : ' d b x 9 0 '

    I f t h e p r o g r a m h a s b e e n w r i t t e n e x a c t l y a s a b o v e t h e n t h e r e w i l l n o t b e a n y e r r o r m e s s a g e s ( o r d i a g -

    n o s t i c s ) . ( T h e c a s e w h e r e a p r o g r a m c o n t a i n s m i s t a k e s w i l l b e c o v e r e d l a t e r . ) T h e c o m p i l e r p r o d u c e s

    e x e c u t a b l e c o d e w h i c h i s s t o r e d ( b y d e f a u l t ) i n a l e c a l l e d a . o u t . I t i s p o s s i b l e t o g i v e t h i s l e a

    d i e r e n t n a m e , t h i s i s d i s c u s s e d l a t e r . T h e e x e c u t a b l e l e c a n b e r u n b y t y p i n g i t s n a m e a t t h e U n i x

    p r o m p t :

    c h a d 2 - 1 3 { a d a m m } 2 7 > a . o u t

    P l e a s e t y p e i n t h e t e m p i n F

    4 5

    T h i s i s e q u a l t o 7 C

    a n d 2 8 0 K

    T h e p r o g r a m w i l l s t a r t e x e c u t i n g a n d w i l l p r i n t o u t P l e a s e t y p e i n t h e t e m p i n F o n t h e s c r e e n ,

    t h e c o m p u t e r w i l l t h e n w a i t f o r a v a l u e t o b e e n t e r e d v i a t h e k e y b o a r d . A s s o o n a s t h e r e q u i r e d i n p u t

    h a s b e e n s u p p l i e d a n d t h e R e t u r n k e y p r e s s e d , t h e p r o g r a m w i l l c o n t i n u e . A l m o s t i n s t a n t a n e o u s l y , t h e

    e q u i v a l e n t v a l u e s i n C e n t i g r a d e a n d K e l v i n w i l l a p p e a r o n t h e s c r e e n .

    2 . 5 A n a l y s i s o f T e m p e r a t u r e P r o g r a m

    T h e c o d e i s d e l i m i t e d b y P R O G R A M E N D P R O G R A M s t a t e m e n t s , i n o t h e r w o r d s t h e s e t w o l i n e s m a r k

    t h e b e g i n n i n g a n d e n d o f t h e c o d e . B e t w e e n t h e s e l i n e s t h e r e a r e t w o d i s t i n c t a r e a s .

    2 S p e c i c a t i o n P a r t

    T h i s i s t h e a r e a o f t h e p r o g r a m t h a t i s s e t a s i d e f o r t h e d e c l a r a t i o n s o f t h e n a m e d m e m o r y l o c a t i o n s

    ( o r v a r i a b l e s ) t h a t a r e u s e d i n t h e e x e c u t a b l e a r e a o f t h e p r o g r a m . A s w e l l a s s u p p l y i n g v a r i a b l e

    n a m e s , i t i s a l s o n e c e s s a r y t o s p e c i f y t h e t y p e o f d a t a t h a t a p a r t i c u l a r v a r i a b l e w i l l h o l d . T h i s

    i s i m p o r t a n t s o t h a t t h e c o m p i l e r c a n w o r k o u t h o w s t o r e t h e v a l u e h e l d b y a v a r i a b l e . I t i s a l s o

    p o s s i b l e t o a s s i g n a n i n i t i a l v a l u e s t o a v a r i a b l e o r s p e c i f y t h a t a p a r t i c u l a r m e m o r y l o c a t i o n h o l d s

    a c o n s t a n t v a l u e ( a r e a d - o n l y v a l u e t h a t w i l l n o t c h a n g e w h i l s t t h e p r o g r a m i s e x e c u t i n g ) . T h e s e

    t e c h n i q u e s a r e v e r y u s e f u l .

    2 E x e c u t i o n P a r t

    T h i s i s t h e a r e a o f c o d e t h a t p e r f o r m s t h e u s e f u l w o r k . I n t h i s c a s e t h e p r o g r a m d o e s t h e

    f o l l o w i n g :

    1 . p r o m p t s t h e u s e r f o r i n p u t ,

    2 . r e a d s i n p u t d a t a ( D e g F ) ,

    3 . c a l c u l a t e s t h e t e m p e r a t u r e i n

    o

    C ,

    4 . p r i n t s o u t t h i s v a l u e ,

    5 . c a l c u l a t e s t h e t e m p e r a t u r e i n K e l v i n a n d

    6 . p r i n t s o u t t h i s v a l u e .

    F o r t r a n 9 0 C o u r s e N o t e s F i l e : C o u r s e N o t e s . p s

  • 8/2/2019 Fortran 90 Course Notes (278P)

    20/278

    8 2 . W h a t A r e C o m p u t e r P r o g r a m s

    2 . 5 . 1 A C l o s e r L o o k a t t h e S p e c i c a t i o n P a r t

    T h e r e a r e a c o u p l e m o r e p o i n t s t h a t s h o u l d b e r a i s e d . T h e I M P L I C I T N O N E s t a t e m e n t s h o u l d a l w a y s

    b e p r e s e n t i n e v e r y F o r t r a n 9 0 p r o g r a m u n i t . I t s p r e s e n c e m e a n s t h a t a l l v a r i a b l e s m e n t i o n e d i n t h e

    p r o g r a m m u s t b e d e c l a r e d i n t h e s p e c i c a t i o n p a r t . A c o m m o n m i s t a k e w h i l s t c o d i n g i s t o m i s t y p e a

    v a r i a b l e n a m e , i f t h e I M P L I C I T N O N E s t a t e m e n t i s n o t p r e s e n t t h e n t h e c o m p i l e r w i l l a s s u m e t h a t t h e

    m i s t y p e d v a r i a b l e n a m e i s a n e w , h i t h e r t o u n m e n t i o n e d , v a r i a b l e a n d w i l l u s e w h a t e v e r v a l u e h a p p e n e d

    t o b e i n t h e m e m o r y l o c a t i o n t h a t t h e v a r i a b l e r e f e r s t o . A n e r r o r s u c h a s t h i s c a u s e d t h e U S S p a c e

    S h u t t l e t o c r a s h .

    T h e t h i r d l i n e o f t h e p r o g r a m d e c l a r e s t h r e e I N T E G E R ( w h o l e n u m b e r ) v a r i a b l e s , t h e r e a r e a n u m b e r o f

    o t h e r v a r i a b l e t y p e s t h a t c a n b e u s e d i n d i e r e n t s i t u a t i o n s :

    2 R E A L | r e a l n u m b e r s , i . e . , n o w h o l e n u m b e r s , e . g . , 3 : 1 4 5 9 , 0 : 3 1 4 5 9 1 0

    1

    R E A L v a r i a b l e s c a n

    h o l d l a r g e r n u m b e r s t h a n I N T E G E R s

    2 L O G I C A L | c a n o n l y t a k e o n e o f t w o v a u l e s : . T R U E . o r . F A L S E . ,

    2 C H A R A C T E R | c o n t a i n s s i n g l e a l p h a n u m e r i c c h a r a c t e r , e . g . , ' a ' ,

    2 C H A R A C T E R ( L E N = 1 2 ) | c o n t a i n s 1 2 a l p h a n u m e r i c c h a r a c t e r s . A n u m b e r o f c o n s e c u t i v e a l p h a n u -

    m e r i c c h a r a c t e r s a r e k n o w n a s a s t r i n g ,

    2 u s e r d e n e d t y p e s | c o m b i n a t i o n o f t h e a b o v e ( s e e l a t e r ) .

    F o r t r a n 9 0 i s n o t c a s e s e n s i t i v e . K i s t h e s a m e a s k

    2 . 5 . 2 A C l o s e r L o o k a t t h e E x e c u t i o n P a r t

    L e t u s n o w l o o k a t t h e p a r t o f t h e p r o g r a m t h a t d o e s t h e a c t u a l ` w o r k ' .

    2 P R I N T * , " P l e a s e t y p e i n t h e t e m p i n F " | w r i t e s t h e s t r i n g ( b e t w e e n t h e q u o t e s ) t o t h e

    V D U s c r e e n ,

    2 R E A D * , D e g F | r e a d s a v a l u e f r o m t h e k e y b o a r d a n d a s s i g n s i t t o t h e I N T E G E R v a r i a b l e D e g F ,

    2 D e g C = 5 * ( D e g F - 3 2 ) / 9 | t h i s i s a n a s s i g n m e n t s t a t e m e n t . T h e e x p r e s s i o n o n t h e

    R H S i s e v a l u a t e d a n d t h e n a s s i g n e d t o t h e I N T E G E R v a r i a b l e D e g C . T h i s s t a t e m e n t c o n t a i n s a

    n u m b e r o f o p e r a t o r s :

    * i s t h e m u l t i p l i c a t i o n o p e r a t o r ,

    - i s t h e s u b t r a c t i o n o p e r a t o r ,

    = i s t h e a s s i g n m e n t o p e r a t o r .

    T h e F o r t r a n 9 0 l a n g u a g e a l s o c o n t a i n s + a n d / o p e r a t o r s f o r a d d i t i o n a n d d i v i s i o n .

    D i v i s i o n i s m u c h h a r d e r t h a n a n y o t h e r o p e r a t i o n f o r a C P U t o p e r f o r m - i t t a k e s l o n g e r s o u s e s

    m o r e r e s o u r c e s , f o r e x a m p l e , i f t h e r e i s a c h o i c e b e t w e e n d i v i d i n g b y 2 . 0 o r m u l t i p l y i n g b y 0 . 5

    t h e l a t t e r s h o u l d b e s e l e c t e d .

    2 P R I N T * , " T h i s i s e q u a l t o " , D e g C , " C " | d i s p l a y s a s t r i n g o n t h e s c r e e n f o l l o w e d b y

    t h e v a l u e o f a v a r i a b l e ( D e g C ) f o l l o w e d b y a s e c o n d s t r i n g ( " C " )

    B y d e f a u l t , i n p u t i s f r o m t h e k e y b o a r d a n d o u t p u t t o t h e s c r e e n .

    S e p t e m b e r 2 , 1 9 9 7 U n i v e r s i t y o f L i v e r p o o l

  • 8/2/2019 Fortran 90 Course Notes (278P)

    21/278

    2 . 6 . H o w t o W r i t e a C o m p u t e r P r o g r a m 9

    2 . 6 H o w t o W r i t e a C o m p u t e r P r o g r a m

    T h e r e a r e f o u r g e n e r a l p h a s e s d u r i n g t h e d e v e l o p m e n t o f a n y c o m p u t e r p r o g r a m :

    1 . s p e c i f y t h e p r o b l e m ,

    2 . a n a l y s e a n d b r e a k d o w n i n t o a s e r i e s o f s t e p s t o w a r d s s o l u t i o n , ( i . e . , d e s i g n a n a l g o r i t h m ) ,

    3 . w r i t e t h e F o r t r a n 9 0 c o d e ,

    4 . c o m p i l e a n d r u n ( i . e . , t e s t t h e p r o g r a m ) .

    W h e n w r i t i n g a c o m p u t e r p r o g r a m i t i s a b s o l u t e l y v i t a l t h a t t h e p r o b l e m t o b e s o l v e d i s f u l l y u n d e r s t o o d .

    A g o o d p e r c e n t a g e o f l a r g e p r o g r a m m i n g p r o j e c t s r u n i n t o t r o u b l e o w i n g t o a m i s u n d e r s t a n d i n g o f w h a t

    i s a c t u a l l y r e q u i r e d . T h e s p e c i c a t i o n o f t h e p r o b l e m i s c r u c i a l . I t i s u s u a l t o w r i t e a d e t a i l e d ` s p e c '

    o f t h e p r o b l e m u s i n g a ` n a t u r a l l a n g u a g e ' ( e . g . , E n g l i s h ) . U n f o r t u n a t e l y , i t i s v e r y e a s y t o w r i t e a n

    a m b i g u o u s s p e c i c a t i o n w h i c h c a n b e i n t e r p r e t e d i n a n u m b e r o f w a y s b y d i e r e n t p e o p l e . I n o r d e r t o

    c o m b a t t h i s , a n u m b e r o f F o r m a l M e t h o d s o f s p e c i c a t i o n h a v e b e e n d e v e l o p e d . T h e s e m e t h o d s a r e

    o f t e n h i g h - l e v e l a b s t r a c t m a t h e m a t i c a l l a n g u a g e s w h i c h a r e r e l a t i v e l y s i m p l e t o c o n v e r t t o h i g h - l e v e l

    p r o g r a m s i n s o m e c a s e s t h i s c a n b e d o n e a u t o m a t i c a l l y . E x a m p l e s o f s u c h l a n g u a g e s a r e Z a n d V D M .

    W e d o n o t p r o p o s e t o c o v e r s u c h s p e c i c a t i o n l a n g u a g e s h e r e . F o r t h e p u r p o s e s o f l e a r n i n g F o r t r a n 9 0 ,

    a s i m p l e n a t u r a l l a n g u a g e s p e c i c a t i o n i s a d e q u a t e .

    O n c e t h e p r o b l e m h a s b e e n s p e c i e d , i t n e e d s t o b e b r o k e n d o w n i n t o s m a l l s t e p s t o w a r d s t h e s o l u t i o n ,

    i n o t h e r w o r d s a n a l g o r i t h m s h o u l d b e d e s i g n e d . I t i s p e r f e c t l y r e a s o n a b l e t o u s e E n g l i s h t o s p e c i f y

    e a c h s t e p . T h i s i s k n o w n a s p s e u d o - c o d e

    T h e n e x t t w o p h a s e s g o h a n d - i n - h a n d , t h e y a r e o f t e n k n o w n a s t h e c o d e - t e s t - d e b u g c y c l e a n d i t i s

    o f t e n n e c e s s a r y t o p e r f o r m t h e c y c l e a n u m b e r o f t i m e s . I t i s v e r y r a r e t h a t a p r o g r a m i s w r i t t e n

    c o r r e c t l y o n t h e r s t a t t e m p t . I t i s c o m m o n t o m a k e t y p o g r a p h i c a l e r r o r s w h i c h a r e u s u a l l y u n e a r t h e d

    b y t h e c o m p i l e r . O n c e t h e t y p o ' s h a v e b e e n r e m o v e d , t h e p r o g r a m w i l l b e a b l e t o b e c o m p i l e d a n d a n

    e x e c u t a b l e i m a g e g e n e r a t e d . A g a i n , i t i s n o t u n c o m m o n f o r e x e c u t i o n t o e x p o s e m o r e e r r o r s o r b u g s .

    E x e c u t i o n m a y e i t h e r h i g h l i g h t r u n - t i m e e r r o r s w h i c h o c c u r w h e n t h e p r o g r a m t r i e s t o p e r f o r m i l l e g a l

    o p e r a t i o n s ( e . g . , d i v i d e b y z e r o ) o r m a y r e v e a l t h a t t h e p r o g r a m i s g e n e r a t i n g t h e w r o n g a n s w e r s . T h e

    p r o g r a m m u s t b e t h o r o u g h l y t e s t e d t o d e m o n s t r a t e t h a t i t i s i n d e e d c o r r e c t . T h e m o s t b a s i c g o a l

    s h o u l d b e t o s u p p l y t e s t d a t a t h a t e x e c u t e s e v e r y l i n e o f c o d e . T h e r e a r e m a n y s o f t w a r e t o o l s t h a t

    g e n e r a t e s t a t i s t i c a l r e p o r t s o f c o d e c o v e r a g e , s u c h a s t h e U n i x t c o v u t i l i t y o r t h e m o r e c o m p r e h e n s i v e

    L D R A T e s t b e d .

    2 . 7 A Q u a d r a t i c E q u a t i o n S o l v e r

    T h i s s e c t i o n s h o w s t h e v a r i o u s s t e p s t h a t a r e n e e d e d w h e n d e s i g n i n g a n d w r i t i n g a c o m p u t e r p r o g r a m

    t o g e n e r a t e t h e r o o t s o f a q u a d r a t i c e q u a t i o n .

    2 . 7 . 1 T h e P r o b l e m S p e c i c a t i o n

    W r i t e a p r o g r a m t o c a l c u l a t e t h e r o o t s o f a q u a d r a t i c e q u a t i o n o f t h e f o r m :

    a x

    2

    + b x + c = 0

    F o r t r a n 9 0 C o u r s e N o t e s F i l e : C o u r s e N o t e s . p s

  • 8/2/2019 Fortran 90 Course Notes (278P)

    22/278

    1 0 2 . W h a t A r e C o m p u t e r P r o g r a m s

    T h e r o o t s a r e g i v e n b y t h e f o l l o w i n g f o r m u l a

    x =

    ; b

    p

    b

    2

    ; 4 a c

    2 a

    2 . 7 . 2 T h e A l g o r i t h m

    1 R E A D t h e v a l u e s o f t h e c o e c i e n t s a , b a n d c f r o m t h e k e y b o a r d .

    2 . i f a i s z e r o t h e n s t o p a s w e d o n o t h a v e a q u a d r a t i c e q u a t i o n ,

    3 . c a l c u l a t e v a l u e o f d i s c r i m i n a n t D = b

    2

    ; 4 a c

    4 . i f D i s z e r o t h e n t h e r e i s o n e r o o t :

    ; b

    2 a

    ,

    5 . i f D i s > 0 t h e n t h e r e a r e t w o r e a l r o o t s :

    ; b +

    p

    D

    2 a

    a n d

    ; b ;

    p

    D

    2 a

    ,

    6 . i f D i s 0 ) T H E N ! r e a l r o o t s

    P R I N T * , " R o o t s a r e " , ( - b + S Q R T ( R E A L ( D ) ) ) / ( 2 . 0 * a ) , &

    " a n d " , ( - b - S Q R T ( R E A L ( D ) ) ) / ( 2 . 0 * a )

    E L S E ! c o m p l e x r o o t s

    R e a l _ P a r t = - b / ( 2 . 0 * a )

    ! S i n c e D < 0 , c a l c u l a t e S R Q T o f - D w h i c h w i l l b e + v e

    I m a g _ P a r t = ( S Q R T ( R E A L ( - D ) ) / ( 2 . 0 * a ) )

    P R I N T * , " 1 s t R o o t " , R e a l _ P a r t , " + " , I m a g _ P a r t , " i "

    P R I N T * , " 2 n d R o o t " , R e a l _ P a r t , " - " , I m a g _ P a r t , " i "

    E N D I F

    E L S E ! a = = 0

    ! a i s e q u a l t o 0 s o . . .

    P R I N T * , " N o t a q u a d r a t i c e q u a t i o n "

    E N D I F

    E N D P R O G R A M Q E S

    S e p t e m b e r 2 , 1 9 9 7 U n i v e r s i t y o f L i v e r p o o l

  • 8/2/2019 Fortran 90 Course Notes (278P)

    23/278

    2 . 8 . T h e T e s t i n g P h a s e 1 1

    2 . 8 T h e T e s t i n g P h a s e

    A s t h e b a s i c g o a l w e w a n t t o e n s u r e t h a t e a c h a n d e v e r y l i n e o f t h e p r o g r a m h a s b e e n e x e c u t e d .

    L o o k i n g a t t h e c o d e w e n e e d t h e f o l l o w i n g t e s t c a s e s :

    1 . d i s c r i m i n a n t g r e a t e r t h a n z e r o : r e a l v a l u e d r o o t s .

    2 . d i s c r i m i n a n t e q u a l s z e r o : s i n g l e r e a l v a l u e d r o o t .

    3 . d i s c r i m i n a n t l e s s t h a n z e r o : c o m p l e x v a l u e d r o o t s .

    4 . c o e c i e n t a i s z e r o : n o t a q u a d r a t i c e q u a t i o n .

    T h e s e f o u r s i t u a t i o n s c a n b e s e e m t o c o r r e s p o n d t o e a c h o f t h e f o u r P R I N T s t a t e m e n t s i n t h e p r o g r a m .

    T h e f o l l o w i n g v a l u e s o f c o e c i e n t s s h o u l d e x e r c i s e e a c h o f t h e s e l i n e s :

    1 a = 1 , b = ; 3 a n d c = 2 ,

    2 a = 1 , b = ; 2 a n d c = 1 ,

    3 a = 1 , b = 1 a n d c = 1 ,

    4 a = 0 , b = 2 a n d c = 3

    B e l o w i s w h a t h a p p e n s w h e n t h e a b o v e t e s t d a t a i s s u p p l i e d a s i n p u t t o o u r p r o g r a m .

    u x a { a d a m m } 3 5 > a . o u t

    T y p e i n v a l u e s f o r a , b a n d c

    1 - 3 2

    R o o t s a r e 2 . 0 0 0 0 0 0 0 a n d 1 . 0 0 0 0 0 0 0

    u x a { a d a m m } 3 6 > a . o u t

    T y p e i n v a l u e s f o r a , b a n d c

    1 - 2 1

    R o o t i s 1 . 0 0 0 0 0 0 0

    u x a { a d a m m } 3 7 > a . o u t

    T y p e i n v a l u e s f o r a , b a n d c

    1 1 1

    1 s t R o o t - 0 . 5 0 0 0 0 0 0 + 0 . 8 6 6 0 2 5 4 i

    2 n d R o o t - 0 . 5 0 0 0 0 0 0 - 0 . 8 6 6 0 2 5 4 i

    u x a { a d a m m } 3 8 > a . o u t

    T y p e i n v a l u e s f o r a , b a n d c

    0 2 3

    N o t a q u a d r a t i c e q u a t i o n

    I t c a n b e s e e n t h a t e v e r y l i n e h a s b e e n e x e c u t e d a n d i n e a c h c a s e t h e c o r r e c t r e s u l t s h a v e b e e n p r o d u c e d .

    2 . 8 . 1 D i s c u s s i o n

    T h e p r e v i o u s p r o g r a m i n t r o d u c e s s o m e n e w i d e a s ,

    F o r t r a n 9 0 C o u r s e N o t e s F i l e : C o u r s e N o t e s . p s

  • 8/2/2019 Fortran 90 Course Notes (278P)

    24/278

    1 2 2 . W h a t A r e C o m p u t e r P r o g r a m s

    2 c o m m e n t s | a n y t h i n g o n a l i n e f o l l o w i n g a ! i s i g n o r e d . C o m m e n t s a r e a d d e d s o l e l y f o r t h e

    b e n e t o f t h e p r o g r a m m e r | o f t e n a p r o g r a m w i l l b e m a i n t a i n e d b y a n u m b e r o f d i e r e n t p e o p l e

    d u r i n g i t s l i f e t i m e . A d d i n g u s e f u l a n d d e s c r i p t i v e c o m m e n t s w i l l b e i n v a l u a b l e t o a n y b o d y w h o

    h a s t o m o d i f y t h e c o d e . T h e r e i s n o t h i n g w r o n g w i t h h a v i n g m o r e c o m m e n t s i n a p r o g r a m t h a n

    t h e r e a r e e x e c u t a b l e l i n e s o f c o d e !

    2 I F c o n s t r u c t | d i e r e n t l i n e s a r e e x e c u t e d d e p e n d i n g o n t h e v a l u e o f t h e B o o l e a n e x p r e s s i o n .

    I F s t a t e m e n t s a n d c o n s t r u c t s a r e e x p l a i n e d m o r e f u l l y i n S e c t i o n s 1 1 . 1 a n d 1 1 . 2 .

    2 r e l a t i o n a l o p e r a t o r s | = = ( ` i s e q u a l t o ' ) o r > ( ` i s g r e a t e r t h a n ' ) . R e l a t i o n a l o p e r a t o r s , ( s e e

    1 0 . 4 , ) a l l o w c o m p a r i s o n s t o b e m a d e b e t w e e n v a r i a b l e s . T h e r e a r e s i x s u c h o p e r a t o r s f o r u s e

    b e t w e e n n u m e r i c v a r i a b l e s , t h e r e m a i n i n g f o u r a r e

    > = | ` g r e a t e r t h a n o r e q u a l t o '

    / = | ` n o t e q u a l t o '

  • 8/2/2019 Fortran 90 Course Notes (278P)

    25/278

    2 . 1 0 . T h e C o m p i l a t i o n P r o c e s s 1 3

    t h e n t h e c o m p i l e r w o u l d g e n e r a t e a s y n t a x e r r o r :

    u x a { a d a m m } 4 0 > f 9 0 Q u a d . f 9 0

    N A g F o r t r a n 9 0 c o m p i l e r v 2 . 2 .

    E r r o r : Q u a d . f 9 0 , l i n e 1 6 :

    I m p l i c i t t y p e f o r R A E L _ P A R T

    d e t e c t e d a t R A E L _ P A R T @ =

    E r r o r : Q u a d . f 9 0 , l i n e 2 4 :

    S y m b o l R E A L _ P A R T r e f e r e n c e d b u t n e v e r s e t

    d e t e c t e d a t Q E S @ < e n d - o f - s t a t e m e n t >

    f 9 0 t e r m i n a t e d - e r r o r s f o u n d b y p a s s 1 ]

    T h e c o m p i l e r i s t e l l i n g u s t h a t R A E L P A R T h a s n o t b e e n g i v e n a n e x p l i c i t t y p e , i n o t h e r w o r d s , i t h a s

    n o t b e e n d e c l a r e d i n t h e s p e c i c a t i o n a r e a o f t h e c o d e . I t i s a l s o t e l l i n g u s t h a t w e a r e t r y i n g t o p r i n t

    o u t t h e v a l u e o f R E A L P A R T b u t t h a t w e h a v e n o t a c t u a l l y a s s i g n e d a n y t h i n g t o i t . A s y o u c a n s e e f r o m

    t h e a b o v e c o m p i l e r o u t p u t , e r r o r m e s s a g e s t e n d t o b e o b t u s e a t t i m e s . A s i g n o f a g o o d c o m p i l e r i s

    o n e t h a t c a n g i v e s p e c i c a n d u s e f u l e r r o r m e s s a g e s r a t h e r t h a n s i m p l y t e l l i n g y o u t h a t y o u r p r o g r a m

    d o e s n ' t m a k e s e n s e !

    2 . 9 . 2 R u n - t i m e E r r o r s

    I f w e h a d m a d e t h e f o l l o w i n g t y p o i n o u r q u a d r a t i c e q u a t i o n s o l v e r ,

    R e a l _ P a r t = - b / ( . 0 * a )

    t h e n t h e p r o g r a m w o u l d c o m p i l e b u t u p o n e x e c u t i o n w e w o u l d g e t a r u n - t i m e e r r o r ,

    u x a { a d a m m } 4 3 > a . o u t

    T y p e i n v a l u e s f o r a , b a n d c

    1 1 1

    * * * A r i t h m e t i c e x c e p t i o n :

    F l o a t i n g d i v i d e b y z e r o - a b o r t i n g

    A b o r t

    t h e p r o g r a m i s t e l l i n g t h e C P U t o d i v i d e b b y z e r o ( . 0 * a ) . A s t h i s i s i m p o s s i b l e , o u r p r o g r a m c r a s h e s

    a n d a n e r r o r m e s s a g e i s s e n t t o t h e s c r e e n . I t i s r u n - t i m e e r r o r s l i k e t h i s t h a t m e a n i t i s a b s o l u t e l y

    e s s e n t i a l t o t h o r o u g h l y t e s t p r o g r a m s b e f o r e c o m m i t t i n g t h e m f o r s e r i o u s u s e .

    I t i s a l s o p o s s i b l e t o w r i t e a p r o g r a m t h a t g i v e s t h e w r o n g r e s u l t s | t h i s i s l i k e l y t o b e a b u g i n t h e

    a l g o r i t h m !

    2 . 1 0 T h e C o m p i l a t i o n P r o c e s s

    T h e N A g F o r t r a n 9 0 c o m p i l e r i s i n v o k e d b y f 9 0 f o l l o w e d b y t h e l e n a m e o f t h e p r o g r a m t o b e c o m p i l e d :

    f 9 0 Q u a d . f 9 0

    T h i s c o m m a n d :

    F o r t r a n 9 0 C o u r s e N o t e s F i l e : C o u r s e N o t e s . p s

  • 8/2/2019 Fortran 90 Course Notes (278P)

    26/278

    1 4 2 . W h a t A r e C o m p u t e r P r o g r a m s

    1 . c h e c k s t h e p r o g r a m s y n t a x

    2 . g e n e r a t e s o b j e c t c o d e

    3 . p a s s e s o b j e c t c o d e t o l i n k e r w h i c h a t t a c h e d l i b r a r i e s ( s y s t e m , I / O , e t c ) a n d g e n e r a t e s e x e c u t a b l e

    c o d e i n a l e c a l l e d a . o u t

    S t e p 3 a t t a c h e s c o d e w h i c h d e a l s w i t h , a m o n g s t o t h e r t h i n g s , m a t h e m a t i c a l c a l c u l a t i o n s a n d i n p u t f r o m

    t h e k e y b o a r d a n d o u t p u t t o t h e s c r e e n . T h i s c o d e , w h i c h i s r e s i d e n t i n a s y s t e m l i b r a r y , i s u s e d w i t h a l l

    a v a i l a b l e c o m p i l e r s , i n o t h e r w o r d s , t h e s a m e I / O l i b r a r y w i l l b e u s e d w i t h C , A D A o r C + + p r o g r a m s .

    T h i s a l l e v i a t e s t h e n e e d f o r e a c h c o m p i l e r t o i n c l u d e a s e p a r a t e i n s t a n c e o f t h e s e I / O r o u t i n e s . I t i s

    p o s s i b l e t h a t t h i s s t e p m a y g i v e a l i n k e r e r r o r i f p r o c e d u r e s u s e d i n t h e c o d e c a n n o t b e f o u n d i n a

    l i b r a r y .

    E x e c u t a b l e c o d e i s p r o c e s s o r s p e c i c . C o d e c o m p l i e d f o r a n I n t e l P e n t i u m w i l l n o t r u n a n a S u n S P A R C

    a n d v i c e - v e r s a . I f a p r o g r a m i s m o v e d t o a d i e r e n t p l a t f o r m t h e n i t w i l l n e e d t o b e r e c o m p i l e d . I f

    t h e p r o g r a m c o n f o r m s t o t h e F o r t r a n 9 0 s t a n d a r d t h e n t h e s o u r c e c o d e w i l l n o t n e e d e d i t i n g a t a l l .

    S o m e c o m p i l e r s a c c e p t e x t e n s i o n s t o t h e F o r t r a n 9 0 s t a n d a r d . I f a n y o f t h e s e e x t e n s i o n s a r e u s e d

    t h e p r o g r a m w i l l b e n o n - s t a n d a r d c o n f o r m i n g a n d m a y n e e d s i g n i c a n t r e w r i t i n g w h e n c o m p i l e d o n a

    d i e r e n t p l a t f o r m o r b y a d i e r e n t c o m p i l e r .

    2 . 1 1 C o m p i l e r S w i t c h e s

    A l l c o m p i l e r s c a n b e i n v o k e d w i t h a n u m b e r o f d i e r e n t o p t i o n s . T h e e e c t o f t h e s e o p t i o n s i s e x p l a i n e d

    i n t h e m a n p a g e s o r i n t h e c o m p i l e r m a n u a l . T h e N A g c o m p i l e r h a s a n u m b e r , f o r e x a m p l e ,

    2 - o : g i v e s e x e c u t a b l e a d i e r e n t n a m e , f o r e x a m p l e , Q u a d

    f 9 0 Q u a d . f 9 0 - o Q u a d

    2 - d r y r u n : s h o w b u t d o n o t e x e c u t e c o m m a n d s c o n s t r u c t e d b y t h e c o m p i l e r .

    2 - h p f : a c c e p t t h e e x t e n s i o n s t o F o r t r a n 9 0 a s s p e c i e d b y t h e H i g h P e r f o r m a n c e F o r t r a n F o r u m .

    2 - i n f o : s e t l e v e l o f i n f o r m a t i o n m e s s a g e s g e n e r a t e d b y t h e c o m p i l e r , f r o m 0 t o 9 .

    2 - t i m e : r e p o r t e x e c u t i o n t i m e s

    f 9 0 Q u a d . f 9 0 - o Q u a d - t i m e

    F o r m o r e i n f o r m a t i o n a b o u t t h e c o m p i l e r t y p e :

    m a n f 9 0

    Q u e s t i o n 1 : C o m p i l a t i o n a n d E d i t i n g

    T h e f o l l o w i n g p r o g r a m c a l c u l a t e s t h e r o o t s o f a q u a d r a t i c e q u a t i o n :

    S e p t e m b e r 2 , 1 9 9 7 U n i v e r s i t y o f L i v e r p o o l

  • 8/2/2019 Fortran 90 Course Notes (278P)

    27/278

    2 . 1 1 . C o m p i l e r S w i t c h e s 1 5

    P R O G R A M Q E S

    I M P L I C I T N O N E

    I N T E G E R : : a , b , c , D

    R E A L : : R e a l _ P a r t , I m a g _ P a r t

    P R I N T * , " T y p e i n v a l u e s f o r a , b a n d c "

    R E A D * , a , b , c

    I F ( a / = 0 ) T H E N

    ! C a l c u l a t e d i s c r i m i n a n t

    D = b * b - 4 * a * c

    I F ( D = = 0 ) T H E N ! o n e r o o t

    P R I N T * , " R o o t i s " , - b / ( 2 . 0 * a )

    E L S E I F ( D > 0 ) T H E N ! r e a l r o o t s

    P R I N T * , " R o o t s a r e " , ( - b + S Q R T ( R E A L ( D ) ) ) / ( 2 . 0 * a ) , &

    " a n d " , ( - b - S Q R T ( R E A L ( D ) ) ) / ( 2 . 0 * a )

    E L S E ! c o m p l e x r o o t s

    R e a l _ P a r t = - b / ( 2 . 0 * a )

    I m a g _ P a r t = ( S Q R T ( R E A L ( - D ) ) / ( 2 . 0 * a ) )

    P R I N T * , " 1 s t R o o t " , R e a l _ P a r t , " + " , I m a g _ P a r t , " i "

    P R I N T * , " 2 n d R o o t " , R e a l _ P a r t , " - " , I m a g _ P a r t , " i "

    E N D I F

    E L S E ! a = = 0

    P R I N T * , " N o t a q u a d r a t i c e q u a t i o n "

    E N D I F

    E N D P R O G R A M Q E S

    1 . U s i n g a n e d i t o r , t y p e t h e a b o v e p r o g r a m i n t o a l e c a l l e d Q u a d S o l v e r . f 9 0

    2 . C o m p i l e a n d r u n t h e p r o g r a m . V e r i f y t h e c o r r e c t n e s s o f t h e c o d e b y s u p p l y i n g t h e f o l l o w i n g t e s t

    d a t a :

    ( a ) a = 1 , b = ; 3 a n d c = 2 ,

    ( b ) a = 1 , b = ; 2 a n d c = 1 ,

    ( c ) a = 1 , b = 1 a n d c = 1 ,

    ( d ) a = 0 , b = 2 a n d c = 3

    3 . C o p y Q u a d S o l v e r . f 9 0 i n t o a n e w l e c a l l e d N e w Q u a d S o l v e r . f 9 0

    4 . E d i t t h i s l e a n d d e c l a r e a n e w R E A L v a r i a b l e c a l l e d o n e o v e r 2 a

    5 . I n t h e e x e c u t a b l e p a r t o f t h e c o d e , s e t o n e o v e r 2 a t o b e e q u a l t o t h e v a l u e o f 1 / ( 2 . 0 * a )

    W h e r e e v e r t h i s e x p r e s s i o n o c c u r s r e p l a c e i t w i t h o n e o v e r a 2 . W h y i s t h i s a g o o d i d e a ?

    6 . D e n e a n o t h e r n e w R E A L v a r i a b l e c a l l e d s q r t D a n d w h e r e a p p r o p r i a t e p r e - c a l c u l a t e S Q R T ( R E A L ( D ) )

    a n d s u b s t i t u t e t h i s n e w v a r i a b l e i n p l a c e o f t h i s e x p r e s s i o n .

    7 . U s e a d i e r e n t s e t o f t e s t d a t a t o t h a t g i v e n a b o v e t o c o n v i n c e y o u r s e l f t h a t N e w Q u a d S o l v e r . f 9 0

    i s a c o r r e c t p r o g r a m .

    8 . C h a n g e t h e n a m e o f t h e p r o g r a m t o b e F i n a l Q u a d S o l v e r . f 9 0 a n d c o m p i l e t h e c o d e t o p r o d u c e

    a n e x e c u t a b l e l e c a l l e d F i n a l Q u a d S o l v e r

    9 . D e l e t e t h e o r i g i n a l p r o g r a m Q u a d S o l v e r . f 9 0 a n d t h e e x e c u t a b l e l e a . o u t

    Q u e s t i o n 2 : T h e H e l l o W o r l d P r o g r a m

    W r i t e a F o r t r a n 9 0 p r o g r a m t o p r i n t o u t H e l l o W o r l d o n t h e s c r e e n .

    F o r t r a n 9 0 C o u r s e N o t e s F i l e : C o u r s e N o t e s . p s

  • 8/2/2019 Fortran 90 Course Notes (278P)

    28/278

    M o d u l e 2 :

    I n t r o d u c t i o n t o F o r t r a n 9 0

    3 I n t r o d u c t i o n

    F o r t r a n 7 7 h a s b e e n w i d e l y u s e d b y s c i e n t i s t s a n d e n g i n e e r s f o r a n u m b e r o f y e a r s n o w . I t h a s b e e n a

    v e r y s u c c e s s f u l l a n g u a g e b u t i s n o w s h o w i n g s i g n s o f a g e , i n t h e l a s t f e w y e a r s t h e r e h a s b e e n a t e n d e n c y

    f o r p e o p l e t o d r i f t a w a y f r o m F o r t r a n 7 7 a n d b e g i n t o u s e C , A d a o r C + + . T h e F o r t r a n s t a n d a r d h a s

    n o w b e e n r e v i s e d t o b r i n g i t u p t o d a t e w i t h a n e w i m p r o v e d l a n g u a g e , k n o w n i n f o r m a l l y a s F o r t r a n 9 0 ,

    b e i n g d e n e d . . C o m p a r i s o n s h a v e b e e n ( r i g h t l y ) d r a w n b e t w e e n t h e n e w F o r t r a n s t a n d a r d a n d A P L ,

    A