Une solution pour le partiel d'informatique du 24 novembre 2001 DEUG S1 MASS+MIAS
Exercice 1
program maximum; var a:array[1..
10] of real; (*1*)
(*2*) max : real; i : integer; begin repeat
(*3*) write('Donnez le nombre de termes de la suite: '); readln(n); until ((1 <= n) and (n <=10)); for i:=1 to n do begin
(*4*) write('a[',i,']');
(*5*) readln(a[i]); end;
(*6*) max:=a[1];
(*7*) for i:=1 to n do if (a[i]
< max) then max:=a[i]; (*8*) writeln('Le plus grand élément de la suite est: ',max); end. Exercice 2
PROGRAM METEO;
VAR precipitation : array[1..30] of real ; temperature : array[1..120] of integer; Tmax, i, j : integer ; Ptotal : real ;
BEGIN (* 1) saisie des temperatures et precipitations *) writeln('saisie des temperatures et precipitations :'); for i:=1 to 30 do begin writeln('Le ',i,'.09.2001 :'); writeln('Température :'); for j:=0 to 3 do begin
write(' à ',j*6:2,' heures : ');
readln(temperature[i*4+j-3]); end; write('Précipitation : '); readln(precipitation[i]) ; end;
(* 2) calculs et affichages *)
(* a) total des précipitations *) Ptotal:=0; for i:=1 to 30 do Ptotal:=Ptotal+Precipitation[i]; writeln('Total des précipitations de septembre 2001: ',Ptotal:0:1);
(* b) température max *) Tmax:=temperature[1]; j:=1; for i:=1 to 120 do if Tmax<temperature[i] then begin Tmax:=temperature[i]; j:=i; end; writeln('Température maximale du mois :'); write(Tmax:6,' °C atteinte le '); writeln(1+((j-1) div 4),'.09.2001 à ',6*((j-1) mod 4):2,'h.');
(* c) jours où T(0h) > T(6h) *) writeln('Jours où la température à 0h est
supérieure à celle de 6h:'); for i:=1 to 30 do if temperature[i*4-3]>temperature[i*4-2] then write('Le ',i:2,', '); writeln; writeln('.'); readln; END. Exercice 3
Question I
Instructions: (* au départ *)
X:=F0;
F0:=F1;
F1:=X+F0;
Effets: F0=p
F1=q
X=p
F0=q
F1=p+q
N.B. Ces instructions donnaient des indications pour le calcul des termes
successifs de la suite de Fibonacci.
Questions II et III
PROGRAM fibonacci ;
VAR a,b,m,i,j : integer ; moyenne : real; fibo, fibo1, fibo0 : longint ; (* longint plutot que integer car Fn peut etre tres grand *) choix : char ;
BEGIN writeln('Fibonacci :') ; writeln ; REPEAT(*1*) (* III.b. *)
(* II.a. saisie de a,b et m avec contrôle *) repeat write('a= (>1) ? ') ; readln(a) ; until a>1 ; repeat write('b= (>=a) ? ') ; readln(b) ; until b>=a ; repeat write('m= (entre 2 et 10) ? ') ; readln(m) ; until (m>=2) and (m<=10) ;
(* II.b. au choix de l'utilisateur... *) REPEAT (*2*) (* III.a. *) writeln; writeln('AU CHOIX : ') ; writeln('-0- terminer') ; writeln('-1- nombres de termes appartenant à [a;b]'); writeln('-2- moyenne arithmetique des m premiers termes') ; writeln('-3- afficher les m premiers termes IMPAIRS') ; writeln('-4- changer les valeurs de a, b et m') ; (* III.b. *) repeat write('votre choix ? ') ; readln(choix) ; until (choix>='0') and (choix<='4') ; case choix of '1' : begin fibo0:=0 ; fibo1:=1
; i:=0 ; repeat
fibo:=fibo1+fibo0 ; fibo0:=fibo1 ; fibo1:=fibo ;
until fibo>=a ; while (fibo<=b) do begin
fibo:=fibo1+fibo0 ; fibo0:=fibo1 ; fibo1:=fibo ; i:=i+1 ; end ;
writeln('La suite a ',i,' termes dans l''intervalle [',a,',',b,']'); end ; '2' : begin fibo0:=0 ; fibo1:=1 ;
moyenne:=fibo1 ;
(* moyenne des deux premiers termes *)
for i:=3 to m do begin
fibo:=fibo1+fibo0 ; fibo0:=fibo1 ; fibo1:=fibo ;
moyenne:=moyenne+fibo ; end ;
write('moyenne des ',m,' premiers termes : ') ; writeln((moyenne/m):0:1) ; end ; '3' : begin
writeln(m,' premiers termes impairs : ') ;
fibo0:=0 ; fibo1:=1 ; fibo:=1 ;
writeln('fibonacci(1)=',fibo:0) ;
(* F1 est le premier terme impair : F1=1 *) i:=1 ; j:=1; while i<m do begin
fibo:=fibo1+fibo0 ; fibo0:=fibo1 ; fibo1:=fibo ; j:=j+1 ;
if (fibo mod 2)=1 then
begin
i:=i+1 ;
writeln('fibonacci(',j,')=',fibo:0) ;
end; end ; end ; end ; {case} UNTIL
(CHOIX='4') OR (CHOIX='0') ; (*2*) UNTIL
CHOIX='0' ; (*1*) end.