Une solution pour la deuxième session d'examen d'informatique (juillet 2001) DEUG MASS+MIAS S1

Exercice 1
 
Instructions:

i:=1;
P1;
    i:=1+1
    write(i,'  ');
writeln(i);
i:=2;
P2(i);
    n:=n+1;
    write(n,'  ');
writeln(i);
i:=3;
P3(i);
    n:=n+1;
    write(n,'  ');
writeln(i);
readln;
Effets:
 
i=1 
 
i=2 
affiche: 2  
affiche: 2  
i=2

i=3
affiche:
affiche: 3
i=3
n=i=3
n=4
affiche: 4
affiche: 3
 
Commentaires:
 
 
 
P1 a accès directement à la variable globale
 
première ligne à l'écran: 2  2

accès direct à i (passage par adresse de n pour P2)
 on modifie i directement
deuxième ligne à l'écran: 3
deuxième ligne à l'écran: 3  3
 
P3 recopie la valeur de i dans la variable locale n
la variable locale n est modifiée 
troisième ligne à l'écran: 4  
troisième ligne à l'écran: 4  3
 

En conclusion, l'affichage final sera :
2  2
3  3
4  3

Exercice 2

program DeuxiemeSession;

CONST nmax = 999;
      cmax = 20;

TYPE point = array[1..2] of real;      (* couple de coordonnées *)
     cloud = array[1..nmax] of point;  (* nuage de particules *)
     concentration = array[1..cmax,1..cmax] of integer;

VAR N : integer;
    nuage : cloud ;
    L, C : integer;
    T : real;
    conc : concentration;
    G : point;
    choix : char;
    maximum, minimum, moyenne : real;
    exterieurs : integer;

PROCEDURE SAISIE(var U: cloud; var taille: integer);
Var x, y : real;
    continuer : string[1];
    c : char;
Begin
writeln('saisie des coordonnées des particules:');
taille:=0;
repeat
  taille:=taille+1;
  repeat
    write('abscisse (>0) ? '); readln(u[taille,1]);
    until (u[taille,1]>=0);
  repeat
    write('ordonnée (>0) ? ') ; readln(u[taille,2]);
    until (u[taille,2]>=0);
  write('continuer la saisie (o|n) ? ');
  readln(continuer);
  if length(continuer)=0 then c:='O' else c:=upcase(continuer[1]);
  until (taille>=nmax) or (c='N');
writeln;
End;

FUNCTION DIST(C1,C2:point):real;
Begin
DIST:=sqrt(sqr(C1[1]-C2[1])+sqr(C1[2]-C2[2]));
End;

PROCEDURE BARYCENTRE(U: cloud; taille: integer; var G: point);
Var i : integer;
Begin
G[1]:=0; G[2]:=0;
for i:=1 to taille do
    begin
    G[1]:=G[1]+u[i,1];
    G[2]:=G[2]+u[i,2];
    end;
G[1]:=G[1]/taille;
G[2]:=G[2]/taille;
End;

PROCEDURE DSTAT(U: cloud; taille: integer; var max, min, moy: real);
Var i : integer;
    G : point;
Begin
BARYCENTRE(U,taille,G);
min:=DIST(G,u[1]);
max:=DIST(G,u[1]);
moy:=0;
for i:=1 to taille do
    begin
    if (DIST(G,u[i])<min) then min:=DIST(G,u[i]);
    if (DIST(G,u[i])>max) then max:=DIST(G,u[i]);
    moy:=moy+DIST(G,u[i]);
    end;
moy:=moy/taille;
End;

PROCEDURE NUM(P: point; L,C: integer;T: real; Var colonne,ligne: integer);
Begin
ligne:=1+trunc(P[2]/T);
colonne:=1+trunc(P[1]/T);
End;

FUNCTION CALCONC(U:cloud;taille:integer;Var D:concentration;L,C:integer;T:real):integer;
(* retourne le nombre de particules exterieures au quadrillage *)
Var resultat : integer;
    x, y : integer;
    i, j : integer;
Begin
for i:=1 to cmax do for j:=1 to cmax do D[i,j]:=0;
Resultat:=0;
for i:=1 to taille do
    if (U[i,1]>C*T) or (U[i,2]>L*T)
       then resultat:=resultat+1
       else begin
            NUM(U[i],L,C,T,x,y);
            D[x,y]:=D[x,y]+1;
            end;
CALCONC:=resultat;
End;

PROCEDURE ECRIRE(D:concentration; L,C: integer; T: real; ext: integer);
Var i, j : integer;
Begin
writeln('Taille d''un carré :  ',T:4:1);
writeln;
for j:=1 to L do
    begin
    for i:=1 to C do
        write(D[i,j]:4);
    writeln;
    end;
writeln;
writeln(ext,' particule(s) à l''extérieur.');
writeln;
End;

PROCEDURE AFFICHERNUAGE(C: cloud; taille: integer);
Var i, j : integer;
Begin
writeln(taille,' particules:');
for i:=1 to taille do
    write('(',C[i,1]:0:1,';',C[i,2]:0:1,')  ');
writeln;
writeln;
End;

BEGIN
SAISIE(nuage,N);
BARYCENTRE(nuage,N,G);
writeln('Les coordonnées du centre de gravité G sont : ');
writeln('     ',G[1]:4:1,' et ',G[2]:4:1);
writeln;
repeat
  writeln('voulez-vous');
  writeln('caractériser la dispersion du nuage de particules par :');
  writeln('  -1- le calcul des concentrations (i)');
  writeln('  -2- les distances maximum, minimum, moyenne (ii)');
  writeln('ou bien :');
  writeln('  -3- afficher le nuage');
  (* fonctionnalité rajoutée car bien pratique ! *)
  writeln('  -0- terminer');
  repeat
    write('? '); readln(choix);
    until (choix>='0') and (choix<='3');
  case choix of
       '1' : begin
             repeat
               write('Combien de lignes ? ');
               readln(L);
               until (L>=1) and (L<=cmax);
             repeat
               write('Combien de colonnes ? ');
               readln(C);
               until (C>=1) and (C<=cmax);
             write('Quelle taille pour les carrés du quadrillage ? ');
             readln(T);
             writeln;
             exterieurs:=CALCONC(nuage,N,conc,L,C,T);
             ECRIRE(conc,L,C,T,exterieurs);
             end;
       '2' : begin
             DSTAT(nuage,N,maximum,minimum,moyenne);
             writeln('maximum : ',maximum:4:1);
             writeln('minimum : ',minimum:4:1);
             writeln('moyenne : ',moyenne:4:1);
             end;
       '3' : AFFICHERNUAGE(nuage,N);
       end;
  until (choix='0');
END.