Subprograme Definite de Utilizator: 1. Ce Se Va Afiga in Urma Executirii Programului Urmdtor
Subprograme Definite de Utilizator: 1. Ce Se Va Afiga in Urma Executirii Programului Urmdtor
Subprograme Definite de Utilizator: 1. Ce Se Va Afiga in Urma Executirii Programului Urmdtor
9l
-
3. In urma apeluluiP(n,x) se doregteafigareacifrei maximea unui num6rnatural
prin intermediulapeluluiwrite(x),respectivcout<<x.Identificali antetulcorectal
subprogramuluiP.
a)
f u n c t i on P (n : inte ge r) :b y t e; int P (int n)
b) b)
procedure P (var a,b: integer) ; v oid P( int &x,int &y)
d)
procedlrre P (x : j-nteger;rrar n :byte) ; v oid P( int x, int &n)
b) b)
p ro e e d ure P(va r a :sir; v oi- d P( int a, int &x, int &y)
nrx: int eger ) i
beSirg
tru-
JIIIL.CL ma]-n ( )
x: =l:i i x:1 ;
p1 ( x ) ; w rite (x, | ') ; P1 (x ) ; c o u t< < x< < ' ' ;
p2 (x ) ; w riteln (x) ; p 2 (x ) ; c o ut< < x< < endl ;
end. )
a) 2 2 3 3 c )2 1 3 3
b)z 2 3 2 d)z r. 3 2
a ) 20 20 43 20 c) 30 20 .43 20
b) :o 20 43 43 d) zo 20 4 6 46
a) a)
f,unction F1 (z byte) ; int F1 (Lnt z,l')
c) ...e)
function F3 (y:byte) : t ex t i FEIA*.-F3(1nt v)
\)-
d) d)
f unct io n 1 2F (c:cha r ) : r eal; fl o a t 1 2F(ahar c)
e) ^!
94
-
Y
d) it d)
f , u nit ion M(x, y: inte ge r) : r eali f loat M ( in t x,int y)
begin
{
x := x div y,. x ,= x /y ;
y : =xnody; y -x ty ;
M : = (x + y)/2; re tu rn (x+ y112.
end,'
1 ,.
a) a)
f u n c tion p (a : sir; n :byte) : int eger ; i n t p 1 .(i n t a [101] , i nt n)
v a r i: int eger;
{
begin i n t i ,x = - 1;
p : = -L; fo r (i = 0 ; i < n; i + + )
f o r i-: = 0 to n -L do i tr (p ri m (al i l )) x= i .;
i f prim(atil) the n p: = i; return x;
end;
)
b) b)
f , u n ct ion p (a : sir;n :b yte ) : int eger i i n t p 2 (i n t a[101], i nt n)
v a r x: intege r;
{
begin i n t x :-1 ;
x : = -1; do x++;
repeat while . ilpri.n.(a Ix] ) a& (x<n) ) ;
rnc (x) if (x==n) return "':"'
until (prj-m (a Ix] ) ) or (x=n) ; else return x;. -1;
i f x= n then p ::--
i-
et.se p ::x;
end,'
c) c)
f u n c t ion p (a : sir;n:b yte ) : int eger ; i n t p 3 (i n t a[101], i nt n)
v a r x, i: integ ert
{
begin i n t 1 ,x = -1;
x : = -1; rh i l e (x < n -1) {
rhile x<n-1 do begin ,x + + ;
i n c (x) ; i f (p ri m .(al xl ) ) return x,"
i f , prj. m ( a [x] ) =tru e th en x : : n;
) ---
end; return .x-n-1-;
p: = x;
)'
end,'
95
I
d) d)
f un c t i oo p (a: sir;n :byte) : int eger ; int p4(int a[101], int n)
var i : int eger; {
begin i n t i rx = 0 ;
fo r (i = 0 ; i < n; i + + ;
fo r i: = 0 t o n -1 d o i tr (p ri n ( i ) ) x:i ;
if prim(i) th en return x;
begin )
-i .
^.
Y ' -L I
e xj-t ;
end
end,'
f or j::i to n -L do )
aI j]:= atj+11 ; els e i+ + ;
d ec (n) ; ]
end ]
e l s e inc (i) ; t4t-
end; *aid maln o
{
begin a t0 l = -3 '
a l ]-l = 2 ,
a [2 ] = -1 '
a[ 2 ] : = 2; a[3]:=-1; f,o r (i = n /2 ; i < n; i + + 1
f o r i: : 1 to n div 2 d o a l i l = a l n -i -11;
aln -i+1 1::a lil; x (a , n ) ,'
x (a r n ) ; fo r (i = 0 ; i < n; i + + ;
fo r i : :1 to n do write (a[i ] , ' ' 1. c o u t< < a Ii ] < < ' 'i
end. )
a ) -3 -1 -1 -3 ilz 2
b) -z -2 d)s 3 3 3
x(a,b) ; x (a rb ) ;
writ eln (a, b) ; c o u t< <a< < b< < endl ;
end. )
b) b)
function F (n:b yte ) : int eger ; int F (intr.n)
var x: lnte ge ri t
begin int x=1 ;
x: = 1; rhi1e (x<n) xx:2;
while (x<n) do x: = x * 2; return x;
F ::x; )
end;
c)
c) int F( i n t n)
function E (n:byte) : integer; {
var x:in teg er; int x = 1 ;
begin do
x: : 1; x l= 2;
repeat y hile ( x <n ) ;
xl.= x* 2 i return x-1;
until x>=n; )
F := x- 1 ;
end; d)
d) in t F ( int n)
f,unction F (n:byte) : integer; {
var j- , x: j-n teg er; iat i,x;
begin x = 1;
x: : 1; f or ( i =1 ; i <=n i i ++)
f or i:=1 to n do begin {
x::x*2; x*--2i
if x<=n then begin if ( x <=n ) returalx;
f: =xi e xit; )
end;end,' return x-1;
F : = x-1; )
end,'
98
-
:t"
-p
var a: rea.l_;
function
begin
F (var x: real) : real;
* inc lude
float
<stdio.h>
F(float &x)
{ x = j :_ 2 _ -Q ? x: -(x));
I
x := abs (x * 10 ); rl
F := int (x/L0 ) + frac ( x ) ; re tu rn i n t(x/1O) + x- int ( *l;
end; ]r t
begin
'JKFmain
tr
a : : 1 5.25,. { f loat a= 1 5 . 2 5 ;
w r i t e(F (a): Q3!,")i pr int f ( "% 0 . 2 f " , F ( a ) ) ;
w r i t e(F (a): 0:2 ,' ,) i pr int f ( "2 0 . 2 f " , F ( a ) ) ;
w r i t eln (a z0 :2); pr int f ( "%0 . 2 f " , a ) ;
end. ]
a) 1s . s o 15.s0 15 2 s.00 c) 1s.so 1s 2 . 0 01 s 2 s o
.0
b) rs . s o 1 s.so 1 s2 .so d) rs.50 1s 2 .o o 1 s 2 .s o
17. C6teelementedivizibile cu l0 se vor afi$a in urma executdrii programului
urmdtor:
va r x,y:byt e; # i n c l u d e <i ostream.h)
firnction F (var y:byte;x:byte) :byter. j .n t F (i n t &y, i nt x)
begin i
y : =y div 10 + y,.
Y :Y/L } + x ;
F : = x+ y ; return x + y;
end; )
beEin
fmain ( )
x : =101; y :=1-0 ; { i n tx = 1 0 1 , y= 10;
w r i t e (F (x, y; , ' ') ; c o u t< < F (x,y)(("
| | ,)i ";
w r i t e(x, ,yr, c o u t < < x ((tt " (( y(< " " ;
w r i t eln (F (x, y) ) ; c o u t< < F ( xry);
end. )
a)1 b)2 d )4
$3
18. in urma apeluluiwrite(P(n,.r)), respectivcout<<p(n, .r), se doregteafi$area
num[rului de aparilii al cifrei x in scriereazecimale a numerului intreg z.
Identificafi antetulcorectal subprogramuluip.
a)
procedure p (n, x: integer)
void P (int n, int x)
b)
function P (x, c: integer) : integ.er int P (int x, int c)
c)
fu n c t ion P (n : rea l_;x:byte) : by t e int P( int n[10], int x)
d) d)
fu n c t i on P (x: in teg er,.n : c har ) : by t e v oid P( int x, fleat n)
99
-
-'8
urm[tor;
19.Con$derdmsubprogramul
''
"1'
.s
functilo n P (va r x: int eger ) : by t e; int P (int &x)
var s :byte,' { int s : 0 ;
beqin whi] ' e ( x ! : 0 )
t
while x>0 do begin ' 1 s+=xZ2;
mod 2 ; x::x div 2i 1 x/:2;
"' :s* x -.
end; )
return s,'
end,' ]
a) z; b) :; c) a; d ) r.
end. ]
Caredintreurmitoareleafirma,tiiesteadevdrate:
100
-
"'*
m Sabprogromeimplementatetn manierd iterativd
Sectiunea2.7.7
L d). 6. b) il. b) 16. c)
2. b) 7. d) 12. c) 17. c)
3. b),d) 8. a) 13. c) 18. b)
4. c) e. e)' 14. a) 19. a)
5. c) 10. b) 15. a) 24. c)
Sectiunea2.7.3
fi:nctiorr det (x: integer) : integer; int det (int x)
va t z,y:in teg er; {
begin i, nl z , y i
Ya:xi zi=xi Y= x i z =x ;
while not prim,(y) do r hile ( !prim(y) )
inc (y) ; y ++;
wtli].e (z>1)and not prim(z)do r hile ( z >1 & & ! p r i m ( z ) )
d ec (z) ;
if (z<>1)a nd (x - z ( y - x ) t lr en Lf ( ( z ! =1 ) & & {x - z <y - x ) )
i^+ . -- zi
return
else else
llat -
gv L
.:tr.
.
J , return y;
end; )
mx:=n r; poz : = i; l
end; )
end; return pz;
end; )
220
-!