Optimisation du programme et code complet
Maintenant que nous avons un programme fonctionnel, optimisons-le en combinant la recherche à la fois de l'élément le plus grand et du plus petit dans une seule boucle. Cela est plus efficace car nous n'avons besoin de parcourir le tableau qu'une seule fois au lieu de deux.
Ouvrez le fichier main.c
et remplacez tout le contenu par la version optimisée suivante :
#include <stdio.h>
int main() {
// We'll declare an array with a maximum capacity of 50 elements
int array[50];
// Variables to store the size of the array and loop counter
int size, i;
// Variables to store the largest and smallest elements
int largest, smallest;
printf("Finding Largest and Smallest Elements in an Array\n");
printf("------------------------------------------------\n\n");
// Ask user for the size of the array
printf("Enter the size of the array (max 50): ");
scanf("%d", &size);
// Validate the input size
if (size <= 0 || size > 50) {
printf("Invalid array size. Please enter a size between 1 and 50.\n");
return 1;
}
// Get array elements from the user
printf("\nEnter %d elements of the array:\n", size);
for (i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}
// Display the entered array
printf("\nThe array you entered is: [ ");
for (i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("]\n\n");
// Initialize largest and smallest with the first element
largest = smallest = array[0];
// Find both the largest and smallest elements in a single pass
printf("Searching for largest and smallest elements...\n");
for (i = 1; i < size; i++) {
// Check for largest
if (array[i] > largest) {
largest = array[i];
printf("New largest found at position %d: %d\n", i + 1, largest);
}
// Check for smallest
if (array[i] < smallest) {
smallest = array[i];
printf("New smallest found at position %d: %d\n", i + 1, smallest);
}
}
// Display results
printf("\nResults:\n");
printf("- The largest element in the array is: %d\n", largest);
printf("- The smallest element in the array is: %d\n", smallest);
// Calculate and display the range
printf("- The range (difference between largest and smallest) is: %d\n", largest - smallest);
return 0;
}
Cette version optimisée :
- Utilise une seule boucle pour trouver à la fois l'élément le plus grand et le plus petit, rendant le programme plus efficace.
- Initialise à la fois
largest
et smallest
au premier élément du tableau.
- Ajoute un calcul de l'étendue (la différence entre les valeurs maximale et minimale).
Compilons et exécutons notre programme optimisé :
gcc main.c -o main
./main
Entrez la taille et les éléments du tableau comme précédemment. Par exemple, si vous entrez les valeurs 10, 25, 5, 17 et 9, vous devriez voir une sortie similaire à :
Finding Largest and Smallest Elements in an Array
------------------------------------------------
Enter the size of the array (max 50): 5
Enter 5 elements of the array:
Element 1: 10
Element 2: 25
Element 3: 5
Element 4: 17
Element 5: 9
The array you entered is: [ 10 25 5 17 9 ]
Searching for largest and smallest elements...
New largest found at position 2: 25
New smallest found at position 3: 5
Results:
- The largest element in the array is: 25
- The smallest element in the array is: 5
- The range (difference between largest and smallest) is: 20
Cette version optimisée fournit les mêmes résultats qu'avant mais est plus efficace et inclut des informations supplémentaires sur l'étendue des valeurs.
Lorsque vous travaillez avec de grands tableaux, ces améliorations d'efficacité peuvent réduire considérablement le temps de calcul, ce qui est une considération importante en programmation.