C Program to Find Largest Element in an Array

Get Certified in C Programming for Free and Take Your Skills to the Next Level

When working with arrays in programming, it is often necessary to find the largest element within them. This task can be achieved through various approaches and algorithms. In this article, we will delve into different implementations of a C program that locates the largest element in an array, providing you with multiple options to accomplish this common task efficiently.

Implementation of C Program to Find Largest Element in an Array

Method 1: Linear Search to find Largest Element in array in C

The simplest and most straightforward method to find the largest element in an array is by performing a linear search. This approach involves iterating through the array, comparing each element with a temporary variable that holds the current maximum value. Here’s the code for the linear search implementation:

#include <stdio.h>
int findLargestElement(int arr[], int n) 
{ 
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
return max;
}
int main() {
int arr[] = {10, 45, 98, 76, 33};
int size = sizeof(arr) / sizeof(arr[0]);
int largest = findLargestElement (arr, size);
printf("The largest element in the array is: %d\n", largest);
return 0;
}

Output

The largest element in the array is: 98

Method 2: Sorting the Array to find largest element in an array using C

Another approach to finding the largest element involves sorting the array in ascending order and returning the last element. While this method is conceptually simple, it may not be the most efficient for large arrays. Here’s the code for this implementation:

#include <stdio.h>
void bubbleSort(int arr[], int n) 
{ 
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}}}}

int findLargestElement(int arr[], int n) 
{ 
bubbleSort (arr, n);
return arr[n - 1];
}

int main() {
int arr[] = {10, 45, 98, 76, 33};
int size = sizeof(arr) / sizeof(arr[0]);
int largest = findLargestElement (arr, size);
printf("The largest element in the array is: %d\n", largest);
return 0;
}

Output

The largest element in the array is: 98

Method 3: Using Divide and Conquer (Recursive) to find largest element in an array

The divide and conquer strategy can also be employed to find the largest element in an array. This recursive approach divides the array into smaller subarrays until a single element remains, which is then returned as the largest element. Here’s the code for this recursive implementation:

#include <stdio.h>
int findLargestElementRecursive(int arr[], int low, int high) { 
if (low == high) {
return arr[low];
} else {
int mid = (low + high) / 2;
int leftMax = findLargestElementRecursive(arr, low, mid);
int rightMax = findLargestElementRecursive(arr, mid + 1, high); 
return (leftMax > rightMax) ? leftMax: rightMax;
}
}
int main() {
int arr[] = {10, 45, 98, 76, 33};
int size = sizeof(arr) / sizeof(arr[0]);
int largest = findLargestElementRecursive(arr, 0, size - 1); printf("The largest element in the array is: %d\n", largest); return 0;
}

Output

The largest element in the array is: 98

Conclusion:

In this article, we explored three different implementations of a C program to find the largest element in an array. The linear search method provides a simple and efficient solution for most scenarios. However, sorting the array and applying divide and conquer are alternative approaches that may be useful in specific situations. Remember to choose the most suitable method based on the size of the array and the performance requirements of your program.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *