In this C program, we will learn about finding largest element of an array. Given an array of length N, we have to find the maximum element in array and it's position in array.
We can find the maximum element in an array by traversing the array from index 0 to N-1 and comparing each element with the maximum element found till now using comparison operators.
- First of all, take N numbers as input from user and store it in an array(lets call it inputArray).
- We will declare variable maxElement and maxElementPosition 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 maxElement. If maxElement is less than current element, we will update maxElement and maxElementPosition with current element and current position respectively.
- At any instant of time suppose we are at index i, then maxElement will give the maximum element between array index 0 to i.
C program to find maximum element of array
In below program, we first take number of elements in array as input from user 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 maximum element of inputArray and starts traversing inputArray form index 0 to N-1.
For every element we compare it's value with maxElement and update value of maxElement If current element is greater than max element. At the end of traversal, maxElement will contain the maximum element of inputArray and maxElementPosition will contain it's position in array.
#include <stdio.h> int main(){ int maxElement, inputArray[500], maxElementPosition; 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]); } maxElement = inputArray[0]; maxElementPosition = 0; for(counter = 1; counter < elementCount; counter++){ if(inputArray[counter] > maxElement){ maxElement = inputArray[counter]; maxElementPosition = counter; } } printf("Maximum element in array is %d at index %d", maxElement, maxElementPosition); return 0; }
Output
Enter number of elements in array: 5 Enter 5 numbers 3 0 9 6 1 Maximum element in array is 9 at index 2
C Program to find largest elements in an array using divide and conquer
Below program divides this problem into two sub-problems by splitting input array into two equal half. Then it calculates the maximum elements of both sub array by recursively calling itself for left and right sub-array.
To find the maximum element of whole array it takes maximum of leftMax and rightMax.
#include <stdio.h> int getMax(int num1, int num2); int getMaxElement(int *array, int leftIndex, int rightIndex); int main(){ int maxElement, 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]); } maxElement = getMaxElement(inputArray, 0, elementCount-1); printf("Maximum element in array is %d ", maxElement); return 0; } int getMax(int num1, int num2){ if(num1 >= num2) return num1; else return num2; } int getMaxElement(int *array, int leftIndex, int rightIndex){ int midIndex, leftMax, rightMax; if(NULL == array){ printf("Invalid Input"); return -1; } if(leftIndex == rightIndex) return array[leftIndex]; midIndex = (leftIndex + rightIndex) / 2; leftMax = getMaxElement(array, leftIndex, midIndex); rightMax = getMaxElement(array, midIndex+1, rightIndex); return getMax(leftMax, rightMax); }
Output
Enter number of elements in array: 6 Enter 6 numbers 7 2 5 1 1 9 Maximum element in array is 9
Related Topics