Introduction
Finding the largest and smallest elements in an array is a common problem in programming that you will encounter frequently. This skill is essential for various applications, such as data analysis, game development, and sorting algorithms.
In this lab, you will learn how to write a C program that identifies both the largest and smallest elements in an integer array. We will break down the process into clear, manageable steps to help you understand the logic behind this fundamental programming technique.

This lab requires basic knowledge of C programming, including variables, arrays, loops, and conditional statements. By the end of this session, you will have created a complete C program that demonstrates how to find extreme values in a dataset.
Setting Up the Program Structure
Let's begin by creating a new C file and setting up the basic structure of our program. This will include the necessary header files, the main function, and variable declarations.
First, navigate to the project directory and create a new file named main.c:
cd ~/project
touch main.c
Now, open the main.c file in the editor and add the following code:
#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");
return 0;
}
This code sets up the basic structure of our program. Let's understand what each part does:
#include <stdio.h>includes the standard input/output library, which provides functions likeprintf()andscanf().- The
main()function is the entry point of our program. - We declare an integer array named
arraywith a capacity of 50 elements. - The
sizevariable will store the number of elements the user wants to enter. - The
ivariable will be used as a loop counter. - The
largestandsmallestvariables will store the maximum and minimum values found in the array.
Save the file after adding this code. This sets up the foundation for our program.
Getting User Input
Now that we have our program structure ready, let's add code to get input from the user. We need to ask the user for the size of the array and then collect the array elements.
Open the main.c file in the editor and modify it by adding the following code before the return 0; statement:
// 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");
This code:
- Prompts the user to enter the size of the array.
- Validates that the size is between 1 and 50.
- Asks the user to input each element of the array one by one.
- Displays the entered array back to the user to confirm.
Let's compile and run our program to see what we have so far:
gcc main.c -o main
./main
You should see a prompt asking for the array size. Enter a small number like 5, then enter 5 values. The program will display the array you entered.
For example, if you enter 5 for the size, and then input the values 10, 25, 5, 17, and 9, you should see the following output:
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 ]
Now that we have collected the array elements from the user, we can proceed to find the largest and smallest values.
Finding the Largest Element
Now let's add code to find the largest element in the array. The strategy is:
- Assume the first element is the largest.
- Compare each subsequent element with the current largest.
- If a larger element is found, update the largest value.
Add the following code to your main.c file, before the return 0; statement:
// Initialize largest with the first element of the array
largest = array[0];
// Find the largest element
printf("Finding the largest element...\n");
for (i = 1; i < size; i++) {
if (array[i] > largest) {
largest = array[i];
printf("New largest found at position %d: %d\n", i + 1, largest);
}
}
printf("\nThe largest element in the array is: %d\n\n", largest);
This code initializes the largest variable with the first element of the array. It then iterates through the array starting from the second element (index 1), comparing each element with the current largest value. If a larger element is found, it updates the largest variable and prints a message.
Let's compile and run our program to see the results:
gcc main.c -o main
./main
Enter the array size and elements as before. For example, if you enter the values 10, 25, 5, 17, and 9, you should see output similar to:
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 ]
Finding the largest element...
New largest found at position 2: 25
The largest element in the array is: 25
This demonstrates how our program finds the largest element in the array. The algorithm starts with the first element as the largest and updates whenever it finds a larger value.
Finding the Smallest Element
Now that we've found the largest element, let's add code to find the smallest element in the array. The strategy is similar:
- Assume the first element is the smallest.
- Compare each subsequent element with the current smallest.
- If a smaller element is found, update the smallest value.
Add the following code to your main.c file, before the return 0; statement:
// Initialize smallest with the first element of the array
smallest = array[0];
// Find the smallest element
printf("Finding the smallest element...\n");
for (i = 1; i < size; i++) {
if (array[i] < smallest) {
smallest = array[i];
printf("New smallest found at position %d: %d\n", i + 1, smallest);
}
}
printf("\nThe smallest element in the array is: %d\n", smallest);
This code initializes the smallest variable with the first element of the array. It then iterates through the array starting from the second element (index 1), comparing each element with the current smallest value. If a smaller element is found, it updates the smallest variable and prints a message.
Let's compile and run our program to see the complete results:
gcc main.c -o main
./main
Enter the array size and elements as before. For example, if you enter the values 10, 25, 5, 17, and 9, you should see output similar to:
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 ]
Finding the largest element...
New largest found at position 2: 25
The largest element in the array is: 25
Finding the smallest element...
New smallest found at position 3: 5
The smallest element in the array is: 5
This demonstrates how our program finds both the largest and smallest elements in the array. The algorithms start with the first element and update whenever they find a larger or smaller value, respectively.
Optimizing the Program and Complete Code
Now that we have a working program, let's optimize it by combining the search for both the largest and smallest elements into a single loop. This is more efficient as we only need to traverse the array once instead of twice.
Open the main.c file and replace the entire content with the following optimized version:
#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;
}
This optimized version:
- Uses a single loop to find both the largest and smallest elements, making the program more efficient.
- Initializes both
largestandsmallestto the first element of the array. - Adds a calculation for the range (the difference between the largest and smallest values).
Let's compile and run our optimized program:
gcc main.c -o main
./main
Enter the array size and elements as before. For example, if you enter the values 10, 25, 5, 17, and 9, you should see output similar to:
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
This optimized version provides the same results as before but is more efficient and includes additional information about the range of values.
When working with large arrays, these efficiency improvements can significantly reduce computation time, which is an important consideration in programming.
Summary
In this lab, you successfully created a C program that finds both the largest and smallest elements in an array. Let's review what you learned:
- You set up a basic program structure with the necessary variable declarations.
- You wrote code to get user input for the array size and elements.
- You implemented an algorithm to find the largest element in the array.
- You implemented a similar algorithm to find the smallest element in the array.
- You optimized the program by combining both searches into a single loop and added extra functionality.
This lab covered several fundamental programming concepts:
- Arrays and array traversal
- Using loops for iteration
- Conditional statements
- Algorithm optimization
- Getting and validating user input
These skills are essential for any programmer and form the foundation for more complex data structures and algorithms. The ability to find extreme values in a dataset is a common requirement in many programming scenarios, such as finding high scores in games, analyzing temperature readings, or processing financial data.
You can extend this program further by adding features like:
- Finding the average of all array elements
- Sorting the array
- Finding the median value
- Counting occurrences of specific values
Continue practicing these concepts to strengthen your programming skills.



