Documente Academic
Documente Profesional
Documente Cultură
Subprograme
Definiţia funcţiei
lipsi.
Antete corecte:
function prima (a,b:integer):integer;
function ThisOne:real;
type cifre=0..9;
function AB (z:char):cifre;
Function AC (x,y:integer;z:char):integer;
function AE(s:string):string;
type nume = array[1..5] of real ;
function AR (x:nume):real;
Problema rezolvată
1 . S e d ă u n p o lig o n c u
8 laturi. Calculaţi aria
acestui poligon,
utilizînd o formula
deja cunoscuta. Se
presupune ca
distantele necesare
sunt cunoscute sau le
putem masura.
Program P1;
Var GF,GH,HA,AB,BC,CD,DE,EF,HF,AF,BF,CF,DF:real;
S1,S2,S3,S4,S5,S6,ST:real;
Function arie (a,b,c:real):real;
Var p:real;
Begin
P:=(a+b+c)/2;
Arie := sqrt(p*(p-a)*(p-b)*(p-c));
End;
Begin
Writeln(‘dati lungimea lat:
GF,GH,HA,AB,BC,CD,DE,EF,HF,AF,BF,CF,DF’);
Readln(GF,GH,HA,AB,BC,CD,DE,EF,HF,AF,BF,CF,DF);
S1:=arie(GF,GH,HF);
Writeln(‘arie primului triunghi =’,s1:8:2);
S2:=arie(HF,AF,HA);
Writeln(‘arie AL II -lea triunghi =’,s2:8:2);
S3:=arie(AF,AB,BF);
Writeln(‘arie AL III -lea triunghi =’,s3:8:2);
Begin
Writeln(‘dati lungimea lat:
GF,GH,HA,AB,BC,CD,DE,EF,HF,AF,BF,CF,DF);
Readln(GF,GH,HA,AB,BC,CD,DE,EF,HF,AF,BF,CF,DF);
S1:=arie(GF,GH,HF);
Writeln(‘arie primului triunghi =’,s1:8:2);
S2:=arie(HF,AF,HA);
Writeln(‘arie AL II -lea triunghi =’,s2:8:2);
S3:=arie(AF,AB,BF);
Writeln(‘arie AL III -lea triunghi =’,s3:8:2);
S4:=arie(BC,CF,BF);
Writeln(‘arie AL IV -lea triunghi =’,s4:8:2);
S5:=arie(CF,CD,DF);
Writeln(‘arie AL V -lea triunghi =’,s5:8:2);
S6:=arie(DF,EF,DE);
Writeln(‘arie AL VI -lea triunghi =’,s6:8:2);
ST:=s1+s2+s3+S4+S5+S6;
Writeln(‘arie totala =’,st:8:2);
Readln;
End.
2. Să se definească o funcţie pentru calcularea
factorialului şi să se calculeze cu ajutorul ei
combinări din n elemente luate cîte m.
Numerele m şi n sunt date.
Formula de calcul:
n!
C m
m!(n m)!
n
Formula de calcul:
program p2;
var n,m,d:integer;
c:real;
function Factorial(x:integer):longint;
var i:integer; p:longint;
begin
p : =1 ;
f o r i: =1 t o x d o
p:=p*i;
factorial:=p;
end;
begin
writeln ('dati n si m');
readln (n,m);
d:=n-m;
c:=factorial(n)/(factorial(m)*factorial(d));
writeln ('combinari din ',n,' cite ',m,':',c:6:2);
readln;
end.
Subprogram cu proceduri
Procedurile sunt subprograme care pot returna unu, mai
multe sau nici un rezultat
Lista parametrilor formali e formata din parametri
valoare si parametri variabila.
N= 6
A1 2 3 4 5 6
B 7 8 9 10 11 12
C 8 10 12 14 16 18 (suma A+B=C)
{Bloc de rezolvare}
For i=1 to n do
S=a[i]+B[i]
Algoritmul de lucru:
Introdu n
Introdu el. Tab A (procedura citire)
suma corespunzatoare)
Afisarea tabloului C (procedura afisare)
Program P1 ;
Const nmax=100 ;
Type vector=array[1..nmax] of integer;
Var A,B,C :vactor; I,n:byte;
Procedure citire (var x : vector);
Begin
For i:=1 to n do
Readln(x[i]) end; {citire}
Procedure afisare (x: vector);
Begin
For i:=1 to n do
Write(x[i]:3)
Writeln; end; {afisare}
Procedure sumacorespunz (A,B: vector, var C:vector);
Begin
For i:=1 to n do
C[i]:=a[i]+B[i];
End; {suma}
Begin
Write(‘n=’); readln(n);
Writeln(‘dati’,n,’elem.a tabloului A’);
Citire(A);
Writeln(‘dati’,n,’elem. A tab. B’)’
Citire(B);
Writeln(‘elementele tabloului A’);
Afisare(A);
Writeln(‘elementele tabloului B’);
Afisare (B);
Sumacorespunz(A,B,C);
Writeln(‘elementele tabloului C’);
Afisare(C);
Readln;
End.
Efecte colaterale
a b / a b/
2
16.Elaboraţi o procedură care, cunoscînd numerele reale a şi b,
afişaza la ecran unul din mesagele care urmează:
APROAPE AGAL-dacă /A-B/ ;
MAI MIC -dacă a-b < -0,01;
MAI MARE -dacă a-b > 0,01;
20. Fiind date numerele naturale a,b,c să se determine cel mai mare
divizor comun al acestor numere.
a) aria triunghiului
b) lungimea segmentului
c) Lungimea cercului
d) aria cercului
e)aria dreptunghiului
2. Elaboraţi o funcţie care calculează:
Var k, m, n : integer ;
a, b, c : real ;
procedure P(i: integer; var j :integer;
x : real; var y : real );
begin
{...}
end.
Care din apelurile ce unmează sînt corecte?
a) P (k, m, a, b) ;
b) P (3, m, a, b) ;
c) P (k, 3, a, b) ;
d) P ( m, n, a, b) ;
e) P ( m, k, 6. 1, b);
f) P ( n , m, 6, b) ;
g) P (n, m, 6, 20 ) ;
h) P ( a, m, b, c ) ;
i) P (i, i, i, i ) ;
j) P ( a, a, a ,a ). Argumentaţi răspunsul
Executii in proceduri
1. Program P1;
var : a,b : integer ;
procedure P ( x: integer , var y : integer );
begin
x:=x+1;
y:=y+x ; writeln ( ’a=’ ‚a ‚’b=’ ‚y);
end;
begin
a:=5;
b:=0;
P (a,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a+2,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (b,a); writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a,a) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (b‚b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
end.
2. Program P2;
var : a,b : integer ;
procedure P (var x: integer ;y : integer );
begin
x:=x+1;
y:=y+x ; writeln ( ’a=’ ‚a ‚’b=’ ‚y);
end;
begin
a:=5;
b:=0;
P (a,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a+2,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (b,a); writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a,a) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (b‚b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
end.
3. Program P3;
var : a,b : integer ;
procedure P (x: integer ;y : integer );
begin
x:=x+1;
y:=y+x ; writeln ( ’a=’ ‚a ‚’b=’ ‚y);
end;
begin
a:=5;
b:=0;
P (a,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a+2,b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (b,a); writeln (’=a’ ‚a ‚’b=’ ‚b);
P (a,a) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
P (b‚b) ; writeln (’=a’ ‚a ‚’b=’ ‚b);
end.
4. Program P1;
var x,y:integer;
procedure P(a:integer; var b:integer);
begin
a:=a+3;
b:=b+a;
end;
begin
x:=0; y:=0;
p(x,y); write(x,y);p(y,x);write(x,y);
p(x,x); write(x,y);p(y,y);write(x,y);
end.
Raspunsuri posibile:
a)0363153159
b)0363036369
c)0303163169
d)0003153159
1.Determinati ce va tipari urmatorul program:
Program test;
Var a,b,c,d:integer;
Procedure P(var b:integer; c:integer);
Var d:integer;
Begin
A:=5; b:=6; c:=7; d:=8;
Writeln(a,b,c,d);
End;
Begin
a:=1; b:=2; c:=3; d:=4;
P(a,b); writeln(a,b,c,d);
End.