C Program to Find Largest Element in an Array

Finding the largest element in an array is a common problem in C programming. This article will guide you through the process of creating a C Program to Find Largest Element in an Array using different solutions, each explained with an example and output. We’ll also cover the prerequisites and provide a conclusion.

Prerequisites

Before diving into the code, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with arrays and loops in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Using a Simple Iterative Approach

1.1 Explanation

In this solution, we will use a simple loop to iterate through the array and find the largest element. We will assume the first element is the largest and then compare it with each subsequent element, updating our largest element variable whenever we find a larger value.

1.2 Program

C
#include <stdio.h>

int main() {
    int n, i, largest;
    
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    
    int array[n];
    
    printf("Enter the elements of the array: ");
    for(i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }
    
    largest = array[0];
    
    for(i = 1; i < n; i++) {
        if(array[i] > largest) {
            largest = array[i];
        }
    }
    
    printf("The largest element in the array is: %d\n", largest);
    
    return 0;
}

1.3 Output

C
Enter the number of elements in the array: 5
Enter the elements of the array: 3 5 7 2 8
The largest element in the array is: 8

Solution 2: Using Array Sorting

2.1 Explanation

In this solution, we will sort the array in ascending order using the qsort function from the standard library, and then take the last element as the largest.

2.2 Program

C
#include <stdio.h>
#include <stdlib.h>

// Comparator function for qsort
int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int main() {
    int n, i;
    
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    
    int array[n];
    
    printf("Enter the elements of the array: ");
    for(i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }
    
    // Sorting the array
    qsort(array, n, sizeof(int), compare);
    
    printf("The largest element in the array is: %d\n", array[n-1]);
    
    return 0;
}

Output

C
Enter the number of elements in the array: 5
Enter the elements of the array: 3 5 7 2 8
The largest element in the array is: 8

Solution 3: Using Recursion

3.1 Explanation

In this solution, we will use a recursive function to find the largest element in the array. The function will compare the current element with the largest element found in the rest of the array.

3.2 Program

C
#include <stdio.h>

// Recursive function to find the largest element
int findLargest(int array[], int n) {
    if (n == 1)
        return array[0];
    
    int largest = findLargest(array, n - 1);
    
    if (array[n - 1] > largest)
        return array[n - 1];
    else
        return largest;
}

int main() {
    int n, i;
    
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    
    int array[n];
    
    printf("Enter the elements of the array: ");
    for(i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }
    
    int largest = findLargest(array, n);
    
    printf("The largest element in the array is: %d\n", largest);
    
    return 0;
}

Output

C
Enter the number of elements in the array: 5
Enter the elements of the array: 3 5 7 2 8
The largest element in the array is: 8

Conclusion

Finding the largest element in an array can be achieved through various methods, each with its own advantages. The iterative approach is straightforward and efficient for small to medium-sized arrays. Sorting the array provides a simple solution but can be less efficient due to the sorting process. Using recursion showcases the power of recursive functions but might not be as intuitive for beginners. Each method has its use cases, and understanding them provides a solid foundation for solving array-related problems in C programming.