analyse2222.py

Created by joelkouakou2080

Created on March 13, 2024

2.78 KB


exo 1
DECLARE
    TYPE int_array IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
    numbers int_array;
BEGIN
    -- Allocation dynamique et dimensionnement du tableau
    numbers := int_array();
    numbers.EXTEND(20);
    
    -- Remplissage du tableau avec les 20 premiers carrés parfaits
    FOR i IN 1..20 LOOP
        numbers(i) := i * i;
    END LOOP;
    
    -- Inversion de l'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('Tableau inversé des carrés parfaits :');
    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('Tableau trié avec la méthode du tri à 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é dans le tableau.');
    ELSE
        DBMS_OUTPUT.PUT_LINE('L''élément ' || search_value || ' n''a pas été trouvé dans le tableau.');
    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:

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.