Open In App

C Program to Find Minimum Value in Array

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C program to find the minimum value in an array
#include <stdio.h>
  
// Function to find the minimum value in an array
int findMinimum(int arr[], int size)
{
    // Assume the first element is the minimum
    int min = arr[0];
  
    // Loop through the array to find the minimum
    for (int i = 1; i < size; i++) {
        if (arr[i] < min) {
            // Update minimum if a smaller element is found
            min = arr[i];
        }
    }
  
    return min;
}
  
int main()
{
  
    int myArray[] = { 12, 54, 10, 6, 78 };
  
    // Calculate the size of the array
    int size = sizeof(myArray) / sizeof(myArray[0]);
  
    // Find the minimum value in the array
    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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads