C Program to Find Minimum Value in Array
Last Updated :
21 Feb, 2024
In C, an array is a collection of items of the same data type stored at contiguous memory locations.In this article, we will learn how to find the minimum value in an array in C.
Example
Input:
myArray = [12, 54, 10, 6, 78]
Output:
min = 6
Finding the Minimum Value in Array in C
In C, you can find the minimum value in an array by iterating through the array and keeping track of the smallest value found so far.
Approach
- Create a min variable and assign it the value of the first element of the array.
- Use a loop to go through each element of the array, starting from the second element.
- Inside the loop, compare each element of the array with the current minimum value.
- If you find an element that is smaller than the current minimum, update the minimum value variable with that element.
- Continue to the next iteration otherwise.
At the end, the min variable will be storing the minimum value present in the array.
C Program to Find Minimum Value in Array
C
#include <stdio.h>
int findMinimum( int arr[], int size)
{
int min = arr[0];
for ( int i = 1; i < size; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int main()
{
int myArray[] = { 12, 54, 10, 6, 78 };
int size = sizeof (myArray) / sizeof (myArray[0]);
int minimumValue = findMinimum(myArray, size);
printf ( "The minimum value in the array is: %d\n" ,
minimumValue);
return 0;
}
|
Output
The minimum value in the array is: 6
Time Complexity : O(n), where n is the number of elements in the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...