Fortran 90 examples
Exercise 1 : Greeting
We check that things work with the shortest possible program.
program hello
!Izpiše klasični pozdrav na terminal
implicit none
write (unit = *, fmt= *) "Hello World!"
end program hello
The program is compiled and run with the command
$ f95 hello.f90 -o hello
$ ./hello
Hello World
Exercise 2 : Degrees to Radians
In addition to the built-in trigonometric functions (sin(), cos(), tan, …), we want various helper functions, such as degree conversion.
Let us write a module called trig that contains these functions for modular use in other programs. A draft of the program trig.f90 is as follows
module trig
implicit none
contains
real function degtorad
...
end function degtorad
real function radtodeg
...
end function radtodeg
end module trig
Let us also write a draft of the program anglecov.f90, which reads 3 angles (in degrees) from the screen
program anglecov
use trig
! preberi tri kote
! Pretvori kote iz stoping v radiane
! izpiši kote
! pretvori kote iz radianov v stopinje
! izpiši kote
end program anglecov
Exercise 3: Quadratic roots
Write a program that computes both roots of the quadratic function
ax²+bx+c=0
for a=1, b=0, c=-4, including a conditional statement (if) that checks whether the discriminant b²-4ac is positive and reports an error in case of invalid input data. You can also extend this example to complex numbers, which for a=4, b=1, c=1 must print x=-0.125+0.48i and x=-0.125-0.48i.
Primeri v Fortranu 90
Vaja 1 : Pozdrav
Preverimo delovanje programa z najkrajšim programom.
program hello
!Izpiše klasični pozdrav na terminal
implicit none
write (unit = *, fmt= *) "Hello World!"
end program hello
Program prevedemo in poženemo z ukazom
$ f95 hello.f90 -o hello
$ ./hello
Hello World
Vaja 2 : Stopinje v Radiane
Poleg vgrajenih trigonometrijskimi funkcijami (sin(), cos(), tan, …) si želimo še razne pomožne funkcije, kot je pretvorba stopinj.
Napišimo modul imenovan trig, ki vsebuje te funkcije za modularno rabo v drugih programih. Osnutek programa trig.f90 je naslednji
module trig
implicit none
contains
real function degtorad
...
end function degtorad
real function radtodeg
...
end function radtodeg
end module trig
Napišimo še osnutek programa anglecov.f90, ki prebere 3 kote (v stopinjah) iz zaslona
program anglecov
use trig
! preberi tri kote
! Pretvori kote iz stoping v radiane
! izpiši kote
! pretvori kote iz radianov v stopinje
! izpiši kote
end program anglecov
Vaja 3: Kvadratni koreni
Napišite program, ki izračuna oba korena kvadratne funkcije
ax²+bx+c=0
za a=1, b=0, c=-4 s tem, da je potrebno vključiti pogojni stavek (if), pri katerem se kontrolira pozitivnost diskrimimante b²-4ac in javiti napako v primeru napake vhodnih podatkov. Lahko pa ta primer razširite še na kompleksna števila, ki mora za a=4, b=1, c=1 izpisati x=-0.125+0.48i in x=-0.125-0.48i.