exo1DECLARETYPEint_arrayISTABLEOFINTEGERINDEXBYPLS_INTEGER;numbersint_array;BEGIN--Allocationdynamiqueetdimensionnementdutableaunumbers:=int_array();numbers.EXTEND(20);--Remplissagedutableauavecles20premierscarrésparfaitsFORiIN1..20LOOPnumbers(i):=i*i;ENDLOOP;--Inversiondel'ordre des éléments du tableau
FOR i IN 1..10 LOOP
numbers(i) := numbers(numbers.COUNT - i + 1);
END LOOP;
-- Affichage du tableau
DBMS_OUTPUT.PUT_LINE('Tableauinversédescarrésparfaits:');
FOR i IN 1..numbers.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('numbers(' || i || ')=' || numbers(i));
END LOOP;
END;
/
-----------------------------
exo 2
DECLARE
TYPE int_array IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
numbers int_array;
swapped BOOLEAN;
temp INTEGER;
BEGIN
-- Initialisation du tableau
numbers := int_array(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400);
-- Tri à bulle
REPEAT
swapped := FALSE;
FOR i IN 1..numbers.COUNT - 1 LOOP
IF numbers(i) > numbers(i + 1) THEN
-- Permutation des éléments
temp := numbers(i);
numbers(i) := numbers(i + 1);
numbers(i + 1) := temp;
swapped := TRUE;
END IF;
END LOOP;
UNTIL NOT swapped;
-- Affichage du tableau trié
DBMS_OUTPUT.PUT_LINE('Tableautriéaveclaméthodedutriàbulle:');
FOR i IN 1..numbers.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('numbers(' || i || ')=' || numbers(i));
END LOOP;
END;
/
----------------------------
exo 3
DECLARE
TYPE int_array IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
numbers int_array;
found BOOLEAN := FALSE;
low INTEGER := 1;
high INTEGER;
mid INTEGER;
search_value INTEGER := 225;
BEGIN
-- Initialisation du tableau (déjà trié)
numbers := int_array(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400);
high := numbers.COUNT;
-- Recherche par dichotomie
WHILE low <= high LOOP
mid := (low + high) / 2;
IF numbers(mid) = search_value THEN
found := TRUE;
EXIT;
ELSIF numbers(mid) < search_value THEN
low := mid + 1;
ELSE
high := mid - 1;
END IF;
END LOOP;
-- Affichage du résultat de la recherche
IF found THEN
DBMS_OUTPUT.PUT_LINE('L''élément' || search_value || 'aététrouvédansletableau.');
ELSE
DBMS_OUTPUT.PUT_LINE('L''élément' || search_value || 'n''apasététrouvédansletableau.');
END IF;
END;
/
-----------------------------
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.