C Program to Find the Maximum and Minimum Element in the Array (original) (raw)
Last Updated : 20 Nov, 2024
In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.
The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the current element is smaller or larger respectively.
C `
#include <stdio.h>
// Function to find maximum and minimum in an array void findMinMax(int arr[], int n, int *max, int *min) {
// Assuming first element as minimum and maximum
*max = arr[0];
*min = arr[0];
for (int i = 1; i < n; i++) {
// Update max if arr[i] is larger
if (arr[i] > *max)
*max = arr[i];
// Update min if arr[i] is smaller
if (arr[i] < *min)
*min = arr[i];
}
}
int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); int max, min;
// Finding minimum and maximum values in arr
findMinMax(arr, n, &max, &min);
printf("%d\n", max);
printf("%d\n", min);
return 0;
}
`
**Explanation: We passed the variables to store the minimum and maximum element to findMinMax() function as to avoid the complex techniques to pass multiple values from function.
C also have a few other methods to find the maximum and minimum element of the array that are useful in different cases. Let’s take a look at them one by one:
Table of Content
Using Recursion
In this approach, we search for the maximum and minimum values by checking each element the array using recursion.
C `
#include <stdio.h>
// Recursive function to find maximum and minimum void findMinMax(int arr[], int n, int *max, int *min) {
// Base Case: Only single element left
if (n == 1) {
*max = arr[0];
*min = arr[0];
return;
}
// Recursive call for the rest of the array
findMinMax(arr, n - 1, max, min);
// Update max and min for the current element
if (arr[n - 1] > *max)
*max = arr[n - 1];
if (arr[n - 1] < *min)
*min = arr[n - 1];
}
int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); int max, min;
// Call the recursive function
findMinMax(arr, n, &max, &min);
printf("%d\n", max);
printf("%d", min);
return 0;
}
`
By Sorting
The largest and smallest element can be easily determined by sorting the array using qsort() function. If the array is sorted in ascending order, the smallest element will be at the beginning (0-th index), and the largest will be at the end (size - 1 index) of the array.
C `
#include <stdio.h> #include <stdlib.h>
// Comparator function for qsort (ascending order) int compare(const void *a, const void b) { return ((int *)a - *(int *)b); }
void findMinMax(int arr[], int n, int *max, int *min) {
// Sort the array using qsort
qsort(arr, n, sizeof(int), compare);
// Minimum element at the start
*min = arr[0];
// Maximum element at the end
*max = arr[n - 1];
}
int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); int min, max;
// Finding minimum and maximum element in the arr
findMinMax(arr, n, &min, &max);
// After sorting, the first element is the smallest
// and the last element is the largest
printf("%d\n", min);
printf("%d", max);
return 0;
}
`
This method is simple to implement but has a higher time complexity.
Similar Reads
- C Program to Find Largest Element in an Array In this article, we will learn how to find the largest element in the array using a C program.The simplest method to find the largest element in the array is by iterating the array and comparing each element with the assumed maximum and updating it when the element is greater.C#include <stdio.h 3 min read
- C Program For Maximum and Minimum of an Array Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons. Examples: Input: arr[] = {3, 5, 4, 1, 9}Output: Minimum element is: 1Â Â Â Â Â Â Â Maximum element is: 9 Input: arr[] = {22, 14, 8, 17, 35, 3}Output:Â Minimum eleme 7 min read
- C Program to Find Minimum Value in Array In this article, we will learn how to find the minimum value in the array.The easiest and straightforward method is to iterate through each element of the array, comparing each value to an assumed minimum and updating it if the current value is less.C#include <stdio.h> int findMin(int arr[], i 3 min read
- How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read
- C Program to Sort the Elements of an Array in Descending Order Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn different ways to sort an array in descending order in C.The simplest method to sort the array in descending 3 min read
- How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read
- How to Find the Mode of Numbers in an Array in C? In C, the mode of array numbers is the element that appears most frequently in the array. To find the mode, we can count the occurrences of each element and identify the one with the highest count. In this article, we will find the mode of numbers in C. Example:Input: myArray = { 1, 2, 3, 4, 5, 2, 3 5 min read
- C program to Find the Largest Number Among Three Numbers Given 3 integer numbers, the task is to find the largest number among them.ExamplesInput: a = 10, b = 22, c = 19Output: 22 is the largest number.Explanation: Among the numbers 5, 8, and 3, the largest number is 8.Input: a = 12, b = 7, c = 9Output: 12 is the largest number.Explanation: Among the numb 4 min read
- C program to count frequency of each element in an array Given an array arr[] of size N, the task is to find the frequency of each distinct element present in the given array. Examples: Input: arr[] = { 1, 100000000, 3, 100000000, 3 } Output: { 1 : 1, 3 : 2, 100000000 : 2 } Explanation: Distinct elements of the given array are { 1, 100000000, 3 } Frequenc 4 min read
- How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read