Yasmtidk / practice

practice INF155
0 stars 0 forks source link

Exercice #3 #47

Open Yasmtidk opened 7 months ago

Yasmtidk commented 7 months ago

/*

Date : 08/02/24 Par : Yasmine Beddouch

Description : Écrivez une fonction qui reçoit un tableau en paramètre ainsi qu’un entier correspondant à la taille du tableau. La fonction retourne la valeur minimale contenue dans ce tableau.

*/

/**/

include

include

//D�FINITION DES TYPES ET CONSTANTES

define APPLICATION 1

define TEST 0

define SIZE 10

// D�CLARATION DES SOUS-PROGRAMMES PRIV�ES

// Declaration de la fonction print_tableau void print_tableau(int tableau[], int taille_tableau);

int valeur_minimale(const int tableau[], int taille_tableau);

// D�FINITION DU SOUS-PROGRAMME PRINCIPALE

if(APPLICATION)

int main() { int tableau[SIZE] = {9,5,17,3,4,2,6,8,1,10}; // Tableau de taille = 10 remplit avec les nombres aleatoire de 1 à 17. int resultat; // Valeur minimale contenu dans le tableau

//Affichage de l'entete de la reponse
printf("\n la valeur minimale contenue dans ce tableau: \n ");

//Apelle de la fonction print_tableau pour affciher le tableau de taille = 10 remplit avec les nombres aleatoire de 1 à 17.
print_tableau(tableau,SIZE);

resultat = valeur_minimale(tableau, SIZE);

printf("\n est : (%d) ", resultat);

return EXIT_SUCCESS;

}

endif

// D�CLARATION DES SOUS-PROGRAMMES PRIV�ES

// Definition de la fonction/procedure print_tableau; void print_tableau(int tableau[], int taille_tableau) {

//Boucle (for) passant par chaque case du tableau en affichant la donne de ce dernier
for (int i = 0; i < taille_tableau; i++) {

    //Pour la valeur situer dans la case # 0
    if (i == 0)
    {
        //Affichage de la valeur a la case tableau[0] (sans virgule)
        printf("%d", tableau[i]);
    }

    //Afichage de la valeur situer dans la case # i (de 1 a 19)
    else printf(", %d", tableau[i]);
}

}

int valeur_minimale(const int tableau[], int taille_tableau)
{
    int resultat = tableau[0];

    //Boucle (for) passant par chaque case du tableau en analysant la valeur de ce dernier (Si min alors sera retourner)
    for (int i = 0; i < taille_tableau; i++)
    {
        // Condition (if) la valeur dans la case #i du tableau est plus petite que la valeur resultat dans la case intialiser tableau[0]
        if(tableau[i] < resultat)
        {
            // Alors la valeur resultats seras remplacer par la valeur minimale
            resultat = tableau[i];
        }
    }

    //Retourne le resultat qui est la valeur minimale dans les cases du tableau
    return resultat;
}

// D�FINITION DES SOUS-PROGRAMMES DE TESTS

if(TEST)

endif

Yasmtidk commented 7 months ago

Here's why const int is used:

const: The const keyword is used to specify that the array tableau should not be modified within the function. It acts as a promise or contract that the function won't change the contents of the array. This is useful for two reasons:

It helps document the intent of the function. Anyone reading the function signature immediately knows that the function does not modify the array. It helps prevent accidental modifications to the array within the function. If you inadvertently attempt to modify tableau inside valeur_minimale, the compiler will generate an error, alerting you to the mistake. Passing Arrays: In C, when you pass an array to a function, you're actually passing a pointer to the first element of the array. Using const int tableau[] ensures that the function doesn't modify the data the pointer points to. This helps maintain the immutability of the array from the function's perspective.

Parameter Declaration Style: Using const int tableau[] is a matter of good style and readability. It clearly communicates to other developers that the array won't be modified. While you could technically omit const, doing so would make the function signature less expressive and might mislead someone into thinking that the function modifies the array.

So, in summary, using const int in the function parameters helps improve code readability, maintainability, and prevents unintended modifications to the array.