Corrigé feuille d'exercices n°1
>
longueur := proc(a::array)
return(nops(convert(a,list)));
end;
Exo1 : fonction <<genere>>
>
genere := proc(n :: integer)
local t, i;
t := array(0..n-1);
for i from 0 to n-1 do
t[i] := rand(0..400)()
od;
return(t);
end;
>
affiche := proc(a::array)
print(convert(a,list));
end;
> tableau := genere(10);
Exo 2 : fonction <<plusgrand>>
>
plusgrand := proc(a :: array)
local n, m, i;
n := longueur(a);
m := a[0];
for i from 1 to n-1 do
if a[i] > m then m := a[i] fi
od;
return(m);
end;
> affiche(tableau); plusgrand(tableau);
Exo 3 : fonction <<indiceplusgrand>>
>
indiceplusgrand := proc(a :: array)
local n, i, m, k;
n := longueur(a);
m := a[0]; k := 0;
for i from 1 to n-1 do
if a[i] > m then m := a[i]; k := i fi
od;
return(k);
end;
> indiceplusgrand(tableau);
Exo 4 : fonction <<moyenne>>.
>
moyenne := proc(a :: array)
local m, i, n;
n := longueur(a);
m := a[0];
for i from 1 to n-1 do
m := m + a[i]
od;
return(evalf(m/n));
end;
> moyenne(tableau);
exo 5 : fonction <<maxsucc>>
>
maxsucc := proc(a :: array)
local n, i, m;
n := longueur(a);
m := abs(a[1]-a[0]);
for i from 1 to n-2 do
if abs(a[i+1]-a[i]) > m then m := abs(a[i+1]-a[i]) fi;
od;
return(m)
end;
> maxsucc(tableau);
exo 6 : fonction <<ecrete>>
>
ecrete := proc(a::array, seuil :: integer)
local n, b, i;
n := longueur(a);
b := array(0..n-1);
for i from 0 to n-1 do
if a[i] > seuil then b[i] := seuil else b[i] := a[i] fi;
od;
return(b);
end;
exo 7 : fonction <<amplitude>>
>
amplitude := proc(a :: array)
local n, i, m, M;
n := longueur(a);
m := a[0]; # pour repérer le plus petit élément
M := a[0]; # pour repérer le plus grand élément
for i from 1 to n-1 do
if a[i] > M then M := a[i]
elif a[i] < m then m := a[i] fi;
od;
return(M-m);
end;
> amplitude(tableau);
exo 8 : fonction <<croissance_max>>.
>
croissance_max := proc(a :: array)
local n, i, croiss, imin;
croiss := 0;
imin := 0;
n := longueur(a);
for i from 1 to n-1 do
if a[i] - a[imin] > croiss then croiss := a[i] - a[imin];
elif a[i] < a[imin] then imin := i fi;
od;
return(croiss)
end;
> croissance_max(tableau);
Maple
TM is a registered trademark of Waterloo Maple Inc.
Math rendered by
WebEQ