You are on page 1of 9

2020-2021

𝑇𝑃0 FORTRAN
Énergétique 2 Groupe 3 TP2
Souayedd Mayssem
Yahya Siwar
Trabelsi Bt Adel Emna
Solaani Zakarya
1. Basic program structure in Fortran

• The program
• statement (which tells the compiler where the program
be- gins)
• Variable•declarations (which tells the compiler what variables will be used,
and what type they are)
• Instructions as to what to do
• An end statement (which tells the program where the program ends)
Wich looks like the following :

program nameofprogram
implicit none
integer a,b
real x,y
parameter nom variable=valeur
character h
instructions
end program

2. Outputting data
a. To print the results to the screen
b. To print the results to a file

• To print something out, you have to use a write statement. This


statement usually looks like:
write(X,Y) Z/
where Z is a list of things to print, X tells the code where to
print Z, and Y tells the code in what format to print Z.

• Alternatively, you may want to write this information into a file. To do


so, you first need to open the file, then write to the file, then close the
file. At the most basic level, this is done by the following commands:

Open (n
file=filename)
Write(n,*)Z
Close (n)

3. Reading data
• To read information, the command is very similar to that
of the write statements : they usually take the form of
read (X, Y) Z. For instance, to read the value of x from a
screen prompt, and then write it back to the screen, you
could add the following command to the code :

• Write(*,*) « what is the value of


x ? » read(*,*)x
• Write(*,*) « x is equal tox »,x

4. On-screen display and input atkeyboard

• Print *, liste de valeur


• Print * ! ligne vide
5. Simple instructions

• If inst then
Else inst exécuté si condition logique fausse
Endif

• Do inst
End do
• Do while condition
End do

6. Subroutines

subroutine nameofroutine(arg1,arg2,arg3,...,argn)
implicit none
declarations
instructions
end subroutine
nameofroutine

7. Application TP0

▪ Ouverture du logiciel :
On déclenche le logiciel Fortran
File →new file → Fortran Fixed Format File
Exercice 1 : calculer z = x+y
Pour lire une variable à l’écran
read *, ‘ Commentaire ‘, nom variables
Pour afficher une variable à l’écran
print *, ‘ Commentaire ‘, nom variables
Programmation :

Exercice 2 : Vérifier l’ordre décroissant ou croissant :


Instructions de contrôle :
If (condition logique) then
Instruction exécutée si condition logique varie
Else
Instruction exécutée si condition logique faux
Endif
Exercice 3 : Calculer la somme de 1 à 5
Do computer= début, fin, pas

Instructions

End do

Cas 1 : Pas =1

Cas 2 : Pas=2
Exercice 4 : utilisation d’un opérateur de comparaison :
Exercice 5: calcul d’une somme de deux matrices

You might also like