C Program to Find Largest Element in an Array

An array is a data structure containing a collection of values or variables. The simplest type of array is a linear array or one-dimensional array. An array can be defined in C with the following syntax:

int Arr[5] = {12, 56, 34, 78, 100};
/* here 12,56,34,78,100 are the elements at indices 0,1,2,3,4 respectively */

In this example, array Arr is a collection of 5 integers. Each integer can be identified and accessed by its index. The indices of the array start with 0, so the first element of the array will have index 0, the next will have index 1 and so on.

Largest element of the array is the array element which has the largest numerical value among all the array elements.

Examples:

If we are entering 5 elements (N = 5), with array element values as 12, 56, 34, 78 and 100
Then, largest element present in the given array is: 100

advertisement
advertisement

If we are entering 4 elements (N = 4), with array element values as 1, 6, 3, and 2
Then, largest element present in the given array is: 6

Problem Description

Write a C program to find the largest element in the given array.

Problem Solution

In this program, we have to find the largest element present in the array. We will do this by first saving the value of the first element in the variable ‘largest’. Then we will compare with remaining elements of the array and store the value if another larger number is found in this array. This will go on N-1 times and the program ends.

The sequence of steps for the solution will be as follows:
1. Create an array of user-defined size.
2. Run the for loop till the user-defined size to insert the element at each location.
3. Considering the first element of the array to be the largest, compare all the remaining elements of the array, and change the largest value if assumed largest element is smaller than the element being compared.
4. At last, the largest element will hold the actual largest value in the array. Thus, print it.

There are several ways to find the largest element of an array in C language. Let’s take a detailed look at all the approaches to find the largest element of an array.

Method 1: Largest Element of an Array in C using Loops (Naive Approach)

In this approach, at first we assume that the first element is the largest. After that we traverse the array and if the current element is greater than the largest element, we update the largest element.

Example:

Given Array: [1,2,4,1,6,4]

advertisement

Initially, Largest Element: 1

Now we start traversing the array.

Index: 1
Index element > Largest Element, so updating the largest element with 2.

Index: 2
Index element > Largest Element, so updating the largest element with 4.

Index: 3
Index element < Largest Element, so Largest element remains the same.

advertisement

Index: 4
Index element > Largest Element, so updating the largest element with 6.

Index: 5
Index element < Largest Element, so Largest element remains the same.

So Largest Element is 6.

Program/Source Code

Here is the source code of the C Program to find the largest number in an array using loops. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C Program to find the largest number in an array using loops
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int size, i, largest;
  10.  
  11.     printf("\n Enter the size of the array: ");
  12.     scanf("%d", &size);
  13.     int array[size];  //Declaring array
  14.  
  15.     //Input array elements
  16.  
  17.     printf("\n Enter %d elements of the array: \n", size);
  18.  
  19.     for (i = 0; i < size; i++)
  20.     {   
  21.         scanf(" %d", &array[i]);
  22.     }
  23.  
  24.      //Declaring Largest element as the first element
  25.     largest = array[0];
  26.  
  27.     for (i = 1; i < size; i++) 
  28.     {
  29.         if (largest < array[i])
  30.         largest = array[i];
  31.     }
  32.  
  33.     printf("\n largest element present in the given array is : %d", largest);
  34.  
  35.     return 0;
  36.  }
Program Explanation

1. Take the size of the array as input from the user.
2. Then, initialize an array of size given by the user.
3. Using for loop, take array element as input from users and insert them into the array.
4. After inserting all the elements of the array, consider the very first element of array to be the largest.
5. Run a for loop, from 1 to arraySize-1, extracting array element one by one and comparing it to the largest element.
6. If the largest element is smaller than the element being compared, then the largest element is updated with the value of the current element of the array.
7. In the end, the largest element will hold the actual largest value present in the array.

Time Complexity: O(n)
In the program, we are traversing the array so the time complexity is O(n).

Space Complexity: O(n)
Space is required to store the array, so space complexity is O(n).

Runtime Test Cases

Here is the runtime output of the C program where the user is reading array of 6 elements with values as 1, 2, 4, 1, 6, and 4. Then it finds out the largest element and displays its value.

Enter the size of the array: 6
 
Enter 5 elements of  the array: 
1
2
4
1
6
4
 
largest element present in the given array is: 6

Method 2: Largest Element of an Array in C using Recursion

In the recursive approach, the recursive function returns the max of the current element and the rest of the elements with the base case to stop when the array is traversed.

Example:

Array: [7,2,4,1,5]

return max( 7 , max( [2,4,1,5] )) ––––––––––––––––––––––––––––   7
                      return max( 2 , max( [4,1,5]) ––––––––––––––––––––    5                        
                                      return max( 4, max( [1,5])  ––––––––––    5
                                                                return max(1,max( [5]) ––– 5
                                                                                     return 5
Program/Source Code

Here is the source code of the C Program to find the largest number in an array using recursion. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C Program to find the largest number in an array using recursion
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <math.h>
  7. //Function to find max of two numbers
  8. int max(int a,int b)
  9. {
  10.     if(a>b)
  11.     return a;
  12.     else
  13.     return b;
  14. }
  15. int largest(int arr[],int size,int i)
  16. {
  17.     if(i == size-1)
  18.     return arr[i];
  19.     return max(arr[i], largest(arr,size,i+1));
  20. }
  21. int main()
  22. {
  23.     int size;
  24.     printf("Enter Size of Array: ");
  25.     scanf("%d",&size);
  26.  
  27.     //Declaring array
  28.     int arr[size];
  29.  
  30.     //Input array elements
  31.     printf("Enter Array Elements: ");
  32.     for(int i=0;i<size;i++)
  33.     scanf("%d",&arr[i]);
  34.     printf("Largest Element is: %d",largest(arr,size,0));
  35.  
  36.     return 0;
  37. }
Program Explanation

1. Take the size of the array as input from the user.
2. Using for loop, take array element as input from users and insert them into the array.
3. Declare a function to find the largest element of the array which returns max between the current element and all other elements with base stops when the array is traversed.
4. Print the value returned by the function as the largest element.

Time Complexity: O(n)
As there are n function calls, the time complexity is O(n).

Space Complexity: O(n)
Space is required to store the array, so space complexity is O(n).

Runtime Test Cases

Here is the runtime output of the C program where the user is reading array of 5 elements with values as 7, 2, 4, 1, and 5. Then it finds out the largest element and displays its value.

Enter the size of the array: 5
Enter Array Elements: 7 2 4 1 5
Largest Element is: 7

Method 3: Largest Element of an Array in C using Function

In this approach, we declare a function in which we assume that the first element of an array is the largest. After that we traverse the array and if the current element is greater than the largest element, we update the largest element. At the end of the function, return the largest element.

Example:

Array: [11, 34, 21, 100]

Initially, Largest Element: 11

Now we start traversing the array.

Index: 1
Index element > Largest Element, so updating the largest element with 34.

Index: 2
Index element < Largest Element, so largest element remains the same i.e. 34.

Index: 3
Index element > Largest Element, so updating the largest element with 100

So Largest Element is 100

Program/Source Code

Here is the source code of the C Program to find the largest number in an array using function. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C Program to find the largest number in an array using function
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <math.h>
  7. int largest(int arr[],int size)
  8. {
  9.     //Declaring Largest element as the first element
  10.     int largest=arr[0];
  11.     for(int i=1;i<size;i++)
  12.     {
  13.         //Updating largest element if current element is greater than largest element
  14.         if(arr[i] > largest)
  15.         largest=arr[i];
  16.     }
  17.     return largest;
  18. }
  19. int main()
  20. {
  21.     int size;
  22.     printf("Enter Size of Array: ");
  23.     scanf("%d",&size);
  24.  
  25.     //Declaring array
  26.     int arr[size];
  27.  
  28.     //Input array elements
  29.     printf("Enter Array Elements: ");
  30.     for(int i=0;i<size;i++)
  31.     scanf("%d",&arr[i]);
  32.     printf("Largest Element is: %d",largest(arr,size));
  33.  
  34.     return 0;
  35. }
Program Explanation

1. Take the size of the array as input from the user.
2. Using for loop, take array element as input from users and insert them into the array.
3. Declare the function to find the largest element.
4. Inside the function, initialize the largest element as the first element of the array.
5. Traverse the array and if the current element is greater than largest element, update the largest element.
6. Return and print the largest element.

Time Complexity: O(n)
In the program, we are traversing the array, so the time complexity is O(n).

Space Complexity: O(n)
Space is required to store the array, so space complexity is O(n).

Runtime Test Cases

Here is the runtime output of the C program where the user is reading array of 4 elements with values as 11, 34, 21, and 100. Then it finds out the largest element and displays its value.

Enter the size of the array: 4
Enter Array Elements: 11 34 21 100
Largest Element is: 100

Method 4: Largest Element of an Array in C using Pointers

In this approach, we find the largest element of an array using the pointers. We declare a function to find the largest element which takes the array, its size and pointer to the largest element. We traverse the complete array and if current element is greater than the largest element, we update the largest element pointer. At first we assume that the first element is the largest. After that we traverse the array and if the current element is greater than the largest element, we update the largest element.

Example:

Array: [2, 5, 21, 9]

Initially, Largest Element: 2

Now we start traversing the array.

Index: 1
Index element > Largest Element, so updating the largest element with 5.

Index: 2
Index element > Largest Element, so updating the largest element with 21.

Index: 3
Index element < Largest Element, so Largest element remains the same i.e 21.

So Largest Element is 21

Program/Source Code

Here is the source code of the C Program to find the largest number in an array using pointers. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C Program to find the largest number in an array using pointers
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <math.h>
  7. void largest_elem(int arr[],int size,int *largest)
  8. {
  9.     //Declaring Largest element as the first element
  10.     *largest=arr[0];
  11.     for(int i=1;i<size;i++)
  12.     {
  13.         //Updating largest element if current element is greater than largest element
  14.         if(arr[i] > *largest)
  15.         *largest=arr[i];
  16.     }
  17. }
  18. int main()
  19. {
  20.     int size;
  21.     printf("Enter Size of Array: ");
  22.     scanf("%d",&size);
  23.  
  24.     //Declaring array
  25.     int arr[size],largest;
  26.  
  27.     //Input array elements
  28.     printf("Enter Array Elements: ");
  29.     for(int i=0;i<size;i++)
  30.     scanf("%d",&arr[i]);
  31.     largest_elem(arr,size,&largest);
  32.     printf("Largest Element is: %d",largest);
  33.  
  34.     return 0;
  35. }
Program Explanation

1. Take the size of the array as input from the user.
2. Using for loop, take array element as input from users and insert them into the array.
3. Declare a function to find the largest element which takes an array, its size and pointer to the largest element as the parameter.
4. In the function:

  • Initialize largest element as the first element of the array.
  • Traverse the array and if the current element is greater than the largest element,update the largest element.

5. Print the Largest Element.

Time Complexity: O(n)
In the program, we are traversing the array, so the time complexity is O(n).

Space Complexity: O(n)
Space is required to store the array, so space complexity is O(n).

Runtime Test Cases

Here is the runtime output of the C program where the user is reading array of 4 elements with values as 2, 5, 21, and 9. Then it finds out the largest element and displays its value.

Enter the size of the array: 4
Enter Array Elements: 2 5 21 9
Largest Element is: 21

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.