0% found this document useful (0 votes)
89 views16 pages

1 Introduction Ex

The document provides information about exercises for a Fortran 95 course, including where to find exercise materials and examples, guidelines for completing exercises based on a Fortran guide, how to use the Emacs text editor and GNU Fortran compiler for exercises, and examples of simple Fortran code compilation and output. It also provides several exercises asking the reader to determine the validity of Fortran code snippets, declare variables, and write control structures.

Uploaded by

cen1510353
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views16 pages

1 Introduction Ex

The document provides information about exercises for a Fortran 95 course, including where to find exercise materials and examples, guidelines for completing exercises based on a Fortran guide, how to use the Emacs text editor and GNU Fortran compiler for exercises, and examples of simple Fortran code compilation and output. It also provides several exercises asking the reader to determine the validity of Fortran code snippets, declare variables, and write control structures.

Uploaded by

cen1510353
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Fortran 95

Exercises

CSC – Tieteen tietotekniikan keskus Oy


CSC – IT Center for Science Ltd.
Exercise material

 You can find the material (exercise examples and


exercises) from the course web site.
 The example answers will be available on-line when the
course ends.
http://www.csc.fi/english/csc/courses/archive/fortran2010

2
About exercises

 The course will follow closely the CSC’s guide Fortran


95/2003 (only in Finnish). See:
http://www.csc.fi/csc/julkaisut/oppaat
http://www.csc.fi/csc/julkaisut/oppaat/pdfs/fortran2003
 The number of the exercise inside the parenthesis refers
to the numbering of the exercise in the Guide Fortran
95/2003.

3
Emacs editor and fortran modes

• For editing Fortran program files you can use Emacs


editor without (the option –nw) or with X-windows:
emacs –nw test.f90
emacs prog.f90

• Emacs fortran mode: On some Linux workstations it may


be necessary first load f90 lisp library: Esc X load-
library and then f90. Indentation and highlighting
works then by default.

4
Fortran compiler
 The GNU Compiler Collection (GCC) Fortran 95
compiler gfortran is used. It supports also FORTRAN
77 and Fortran 90. The newest versions implement also
some or all Fortran 2003 and Fortran 2008 features. The
versions available on Linux workstations are 4.1.2 and
4.4.0
 The GCC online documentation of the latest versions:
http://gcc.gnu.org/onlinedocs/
On Linux workstations the command gfortran
(version 4.1.2) and gfortran44 (version 4.4.0) can
be used directly to launch the GCC Fortran compiler.

5
Using the Gfortran compiler
Basic usage
In order to compile the file mycode1.f95 (a source code
file)

$ gfortran -c mycode1.f95

The output file will be mycode1.o (a object code file)

You can link your object files together to generate an


executable

$ gfortran -o ExecutableName mycode1.o mycode2.o mycode3.o


...

Here the executable will be called ExecutableName and the


object files were created as explained earlier. If the flag
-o ExecutableName is omitted, then the executable will be
named as a.out (on UNIX systems). 6
Using the Gfortran compiler
Basic usage ...
You can skip the separate compilation step by entering

$ gfortran -o ExecutableName mycode1.f95 mycode2.f95


mycode3.f95 ...

This will compile the source files ( mycode1.f95 mycode2.f95


mycode3.f95 ...), link and generate an executable
ExecutableName – all in one step. You could also add object
file names to this command line and they would automatically
be linked in during the link step.

Source files whose names end with .f90 or .f95 are assumed to
be free form -files. If file name ends with .f or .for then
the files are assumed to be fixed form -files (the fixed form
is an older Fortran source code style not recommended any
more).
Simple compilation
– Example program test.f95:
PROGRAM test
IMPLICIT NONE
INTEGER, PARAMETER :: result = 42
! Simple output statement...
PRINT *, ’Hello world!’
PRINT *, ’The result is ’, result
END PROGRAM test

Compilation and execution:


% gfortran test.f95 -o test
% ./test
Hello world!
The result is 42

8
Lecture#1 : Introduction

9
Exercise: Input format of the code
1. (4.1) What means the following (free format) code line
A = 0.0 ; B = 370 ! Initialization ; C = 17.0 ; D = 33.0

2. (4.2) Is the following syntactically correct Fortran 90


code?
Y = SIN(MAX(X1,X2)) * EXP(-COS(X3)**I) - TAN(AT&
& AN(X4))

3. (4.3) Is the following declaration correct?

REAL :: real; REAL :: price

10
1. (4.5) Are the following Fortran statements written
correctly?

character_string = ’Awake in the morning,


& asleep in the evening.’
x = 0.4-6
answer = ’True & false’
low-limit = 0.0005E10
y = E6

11
Exercise: Declaration of variables

1. (5.1) Are the following declarations legal in Fortran:

DOUBLE :: x
CHARACTER(LEN=*), PARAMETER :: "Name"
REAL :: pi = 22/7
REAL :: x = 2., y = -3
REAL :: pii = 22.0/7.0
REAL x = 1.0

2. (5.2) Declare the constant k, whose value is 0.75.

12
1. (5.5) Find out, what are the ranges of the values of integer and real
numbers, which the Fortran compiler you use can handle. Hint:
study the values which the following subprogram calls return
SELECTED_REAL_KIND
SELECTED_INT_KIND
e.g., by the following way:
PROGRAM kindtest
IMPLICIT NONE
INTEGER :: i, ikind
DO i = 1, 24
ikind = SELECTED_INT_KIND(i)
WRITE (*,*) ’i = ’,i, ’ kind = ’,ikind
END DO
END PROGRAM kindtest
Please, note that the values of the kind parameter depend on the
Fortran compiler used!
13
1. (5.6) Print the value of the statement

using at least the precision of 6 decimals. Use the


function calls LOG and SQRT. In addition, print the
value of the statement using the highest precision of
real numbers which the compiler knows.

14
Exercise: Control structures
1. (7.3) What are the iteration counts of the following DO loops, the
values of loop variable i inside the loop, and the value of the loop
variable after the DO construct?
DO i = 1, 5
DO i = 5, 0, -1
DO i = 10, 1, -2
DO i = 0, 30, 7
DO i = 3, 2, 1

1. (7.2) Write a SELECT CASE structure, which does different


operations when an integer variable is negative, it is zero, or it is
one of the prime numbers (3, 5, 7, 11, 13). In other cases nothing is
done.
15
1. (7.6) Print the transformation table for converting inches
to millimeters (1" = 25.4mm). Start from the value 0.0
and print values at half inch intervals up to 12 inches.

16

You might also like