- How to find the minimum or smallest element in an array
- How to use a variable to keep track of the minimum value
- How to compare elements in an array
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to find the minimum or smallest element in an array.
Finding the minimum element in an array is a fundamental programming problem. It is used in many real-world applications, such as:
- Finding the lowest score in a class
- Finding the minimum temperature in weather data
- Finding the smallest number in a list of sales figures
- Finding the minimum value in sensor readings
💡 Key Point: To find the minimum element, we assume the first element is the smallest, then compare it with all other elements. If we find a smaller element, we update our minimum variable.
C Program to Find Minimum Element in Array
#include <stdio.h>
int main() {
int n, i;
int min;
// Ask user for number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Declare array of size n
int arr[n];
// Read elements into the array
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Assume first element is the minimum
min = arr[0];
// Find minimum element
for(i = 1; i < n; i++) {
if(arr[i] < min) {
min = arr[i];
}
}
// Display the minimum element
printf("\nMinimum element in the array: %d\n", min);
return 0;
}
Sample Output
Enter the number of elements: 5 Enter 5 elements: 10 50 30 40 20 Minimum element in the array: 10
Another Example:
Enter the number of elements: 6 Enter 6 elements: 100 200 50 75 25 150 Minimum element in the array: 25
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n;— stores the number of elementsint i;— loop counterint min;— stores the minimum element
- Get User Input: Prompts the user to enter the number of elements and then reads them into the array.
- Initialize Minimum:
min = arr[0];assumes the first element is the smallest. - Find Minimum: The
forloop starts fromi = 1and compares each element withmin:- If
arr[i] < min, then updatemin = arr[i];
- If
- Display Result:
printf("\nMinimum element in the array: %d\n", min);prints the smallest element. - Return:
return 0;indicates successful program execution.
📝 Note: The algorithm works for both positive and negative numbers. For negative numbers, the smallest (most negative) will be found correctly.
Algorithm to Find Minimum Element
- Start
- Read the number of elements (n)
- Read n elements into the array
- Set
min = arr[0] - For
i = 1ton-1:- If
arr[i] < min, setmin = arr[i]
- If
- Print min
- End
💻 Practice Exercise
Challenge 1: Modify the program to find both the maximum and minimum elements in the array in a single pass.
Challenge 2: Find the second smallest element in the array.
🔍 Click to Show Solution for Challenge 1
#include <stdio.h>
int main() {
int n, i;
int max, min;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = arr[0];
min = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
if(arr[i] < min) {
min = arr[i];
}
}
printf("\nMaximum element: %d\n", max);
printf("Minimum element: %d\n", min);
return 0;
}
Frequently Asked Questions
1. How do you find the minimum element in an array in C?
Assume the first element is the minimum, then compare it with all other elements using a loop. If you find a smaller element, update the minimum variable.
2. What if the array has only one element?
If the array has only one element, the loop doesn't run (since i = 1 and n = 1), and min correctly holds the only element.
3. Can I use this method with negative numbers?
Yes, the method works with any integers, including negative numbers. The algorithm correctly identifies the smallest (most negative) number.
4. What is the time complexity of this algorithm?
The time complexity is O(n), where n is the number of elements in the array. We need to visit each element once to find the minimum.
5. How is this different from finding the minimum using sorting?
Sorting the array would take O(n log n) time, while this method takes O(n) time. The direct comparison method is more efficient for finding just the minimum element.
💡 Tip: Always initialize the minimum variable with the first element of the array to avoid any assumptions about valid values.