In this C program, we will learn about finding smallest element of an array. Given an array of length N, we have to find the minimum element of an array and it's position in array.
If minimum element occurs more than once in input array, then we will return the index of it's first occurrence(smallest index). We can find the minimum element in an array by traversing the array from first to last element and comparing each element with the minimum element found till now.
- First of all, take N numbers as input from user and store it in an array(lets call it inputArray).
- We will declare variable minElement and minElementPosition and initialize it with first element of inputArray and 0 respectively.
- We will start traversing inputArray from index 0 to N -1 and compare each element with minElement. If minElement is more than current element, we will update minElement and minElementPosition with current element and current position respectively.
- At any instant of time suppose we are at index i, then minElement will give the minimum element between array index 0 to i.
C program to find minimum element and it's position in array
In below program, we first take number of elements as input from user using scanf function and stores in an integer variable 'elementCount'. Then we take 'elementCount' numbers as input from user and stores them in an integer array using a for loop. Now, we assume that first element(inputArray[0]) is the minimum element of inputArray and starts traversing inputArray form index 0 to N-1.
For every element we compare it's value with minElement and update value of minElement If current element is greater than minElement. At the end of traversal, minElement will contain the minimum element of inputArray and minElementPosition will contain it's position in array.
#include <stdio.h> int main(){ int minElement, inputArray[500], minElementPosition; int elementCount, counter; printf("Enter number of elements in array: "); scanf("%d", &elementCount); printf("Enter %d numbers \n", elementCount); for(counter = 0; counter < elementCount; counter++){ scanf("%d", &inputArray[counter]); } minElement = inputArray[0]; minElementPosition = 0; for(counter = 1; counter < elementCount; counter++){ if(inputArray[counter] < minElement){ minElement = inputArray[counter]; minElementPosition = counter; } } printf("Minimum element in array is %d at index %d", minElement, minElementPosition); return 0; }Output
Enter number of elements in array: 6 Enter 6 numbers 4 -2 7 1 -4 8 Minimum element in array is -4 at index 4
C program to find smallest elements in an array using divide and conquer
#include <stdio.h> int getMin(int num1, int num2); int getMinElement(int *array, int leftIndex, int rightIndex); int main(){ int minElement, inputArray[500]; int elementCount, counter; printf("Enter number of elements in array: "); scanf("%d", &elementCount); printf("Enter %d numbers \n", elementCount); for(counter = 0; counter < elementCount; counter++){ scanf("%d", &inputArray[counter]); } minElement = getMinElement(inputArray, 0, elementCount-1); printf("Minimum element in array is %d ", minElement); return 0; } int getMin(int num1, int num2){ if(num1 <= num2) return num1; else return num2; } int getMinElement(int *array, int leftIndex, int rightIndex){ int midIndex, leftMin, rightMin; if(NULL == array){ printf("Invalid Input"); return -1; } if(leftIndex == rightIndex) return array[leftIndex]; midIndex = (leftIndex + rightIndex) / 2; leftMin = getMinElement(array, leftIndex, midIndex); rightMin = getMinElement(array, midIndex+1, rightIndex); return getMin(leftMin, rightMin); }Output
Enter number of elements in array: 6 Enter 6 numbers 1 9 2 2 0 6 Minimum element in array is 0Related Topics