C Program to Find the Largest Element in an Array
In this article, we will discuss the C program to find the largest element in an Array. But before discussing the program, we must know about an array with its methods and examples.
What is an array?
A data structure called an array that contains a group of values or variables. A linear array is the most basic array, often known as a one-dimensional array.
Syntax:
The syntax to define an array in C is as follows:
int Arr[5];//12; 56; 34; 78; 100;
- The elements at indices 0, 1, 2, 3, and 4 are 12, 56, 34, and 78, 100, respectively.
- In this case, the array Arr consists of 5 numbers. Each integer has an index that can be used to identify and access it.
- The array's indices start at 0. Thus, its first element will have a 0 index, followed by a 1 for the second, and so on.
- The array element with the highest numerical value among all other array elements is referred to as the largest element of the array.
Examples:
- Using the array element values 12, 56, 34, 78, and 100, we can enter 5 elements (N = 5) into the system.
- Therefore, the largest element in the given array is 100.
Method 1: C's Loops to Find the Largest Element in an Array (Naive Approach)
In this method, the first element is initially thought to be the largest. Following that, we iterate through the array, updating the largest element if the current element is larger than the oldest one.
Example:
- Array: [1,2,4,1,6,4]
- Largest Element: 1 at first.
- Now, we are going through the array.
- Index: 1 Index element > Largest Element, Update the largest element with 2.
- Index: 2 Index element > Largest Element, Largest Element is updated with 4
- Index: 3, the Largest Element is equal to the Index Element, and hence, the Largest Element does not change.
- Index: 4 Index element > Largest Element, Largest Element is updated with 6.
Example: C Program to find the largest element in an array:
#include <stdio.h>
int main()
{
int size, i, largest;
printf("\n Enter the size of the array: ");
scanf("%d", &size);
int array[size]; //Declaring array
//Input array elements
printf("\n Enter %d elements of the array: \n", size);
for (i = 0; i< size; i++)
{
scanf(" %d", &array[i]);
}
//Declaring the Largest element as the first element
largest = array[0];
for (i = 1; i< size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("\n largest element present in the given array is : %d", largest);
return 0;
}
Output:
![C Program to Find the Largest Element in an Array](https://static.tutorialandexample.com/c/c-program-to-find-the-largest-element-in-an-array1.png)
Time Complexity O(n):
- The program's time complexity is O(n) because we traverse the array.
Space Complexity O(n):
- The space complexity of this array is O(n).
Method 2: Using Recursion in C to Find the Largest Element in an Array
The base case of the recursive technique is to halt when the array is traversed, and the recursive function returns the max of the current element and the remaining items.
Example: C Program to find the largest element in an array using Recursion:
#include <stdio.h>
int findLargest(int arr[], int size);
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i< n; i++) {
scanf("%d", &arr[i]);
}
int largest = findLargest(arr, n);
printf("Largest number in the array is: %d\n", largest);
return 0;
}
int findLargest(int arr[], int size) {
if (size == 1) {
return arr[0];
} else {
int max_rest = findLargest(arr, size - 1);
return (arr[size - 1] >max_rest) ? arr[size - 1] : max_rest;
}
}
Output:
![C Program to Find the Largest Element in an Array](https://static.tutorialandexample.com/c/c-program-to-find-the-largest-element-in-an-array2.png)
Time Complexity O(n):
- The program's time complexity is O(n) because we traverse the array.
Space Complexity O(n):
- The space complexity of this array is O(n).
Method 3: Using a Function, the largest element of an array in C
With this method, we declare a function and presumptively determine the size of an array's first element. Following that, we iterate through the array, updating the largest element if the current element is larger than the oldest one. After that, return the greatest element at the function's end.
Example:
Array: [11, 34, 21, 100]
- Largest Element at First: 11
- Now, we are now going through the array.
- Update the largest element with 34 using the indexing formula: Index 1 Element > Largest Element.
- Index: 2 Largest Element Index stays the largest element (34).
- Update the greatest element with 100 using the index 3 because 100 is the largest element.
Example: C Program to find the largest element in an array using Functions:
#include <stdio.h>
#include <math.h>
int largest(int arr[],int size)
{
//Declaring the Largest element as the first element
int largest=arr[0];
for(int i=1;i<size;i++)
{
//Updating the largest element if the current element is greater than the largest element
if(arr[i] > largest)
largest=arr[i];
}
return largest;
}
int main()
{
int size;
printf("Enter Size of Array: ");
scanf("%d",&size);
//Declaring array
int arr[size];
//Input array elements
printf("Enter Array Elements: ");
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Largest Element is: %d",largest(arr,size));
return 0;
}
Output:
![C Program to Find the Largest Element in an Array](https://static.tutorialandexample.com/c/c-program-to-find-the-largest-element-in-an-array3.png)
Time Complexity O(n):
- The program's time complexity is O(n) because we traverse the array.
Space Complexity O(n):
The space complexity of this array is O(n).