Introduction
Finding the second largest element in an array involves determining the largest element first and then finding the next largest element. This guide will show you how to write a C program to identify the second largest element in an array provided by the user.
Problem Statement
Create a C program that:
- Takes the size of the array as input from the user.
- Takes the elements of the array as input.
- Finds the second largest element in the array.
- Displays the second largest element.
Example:
- Input: Array size = 6, Elements = [12, 35, 1, 10, 34, 1]
- Output: Second largest element = 34
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>
to include the standard input-output library, which is necessary for usingprintf
andscanf
functions. - Write the Main Function: Define the
main
function, which is the entry point of every C program. - Declare Variables: Declare variables to store the array size, the array elements, and the largest and second largest elements.
- Input the Array Size: Use
scanf
to take input from the user for the size of the array. - Input the Array Elements: Use a loop to take input from the user for the elements of the array.
- Find the Largest and Second Largest Elements: Use a loop to find the largest and second largest elements in the array.
- Display the Second Largest Element: Use
printf
to display the second largest element.
C Program
#include <stdio.h> #include <limits.h> // For INT_MIN /** * C Program to Find the Second Largest Element in an Array * Author: https://www.javaguides.net/ */ int main() { // Step 1: Declare variables to hold the array size, elements, and largest/second largest elements int n, i; int largest, second_largest; // Step 2: Prompt the user to enter the size of the array printf("Enter the number of elements in the array: "); scanf("%d", &n); // Step 3: Declare an array to hold the elements int arr[n]; // Step 4: Input the array elements printf("Enter %d elements:\n", n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Step 5: Initialize largest and second_largest largest = second_largest = INT_MIN; // Step 6: Find the largest and second largest elements for (i = 0; i < n; i++) { if (arr[i] > largest) { second_largest = largest; largest = arr[i]; } else if (arr[i] > second_largest && arr[i] != largest) { second_largest = arr[i]; } } // Step 7: Check if second_largest exists if (second_largest == INT_MIN) { printf("There is no second largest element in the array.\n"); } else { printf("The second largest element is %d\n", second_largest); } return 0; // Step 8: Return 0 to indicate successful execution }
Explanation
Step 1: Declare Variables
- The variable
n
is declared to store the size of the array.largest
andsecond_largest
are used to store the largest and second largest elements in the array, respectively. The variablei
is used as a loop counter.
Step 2: Input the Array Size
- The program prompts the user to enter the size of the array using
printf
. Thescanf
function then reads the input and stores it in the variablen
.
Step 3: Declare the Array
- The program declares an array
arr
of sizen
to hold the elements provided by the user.
Step 4: Input the Array Elements
- The program uses a
for
loop to take input for each element of the array. The loop iterates from 0 ton-1
, reading the elements usingscanf
.
Step 5: Initialize largest and second_largest
- Both
largest
andsecond_largest
are initialized toINT_MIN
(a constant defined inlimits.h
representing the minimum integer value). This ensures that any element in the array will be larger than the initial values.
Step 6: Find the Largest and Second Largest Elements
- The program uses a
for
loop to iterate through the array:- If an element is greater than
largest
,second_largest
is updated to the current value oflargest
, andlargest
is updated to the current element. - If an element is greater than
second_largest
but not equal tolargest
, it becomes the newsecond_largest
.
- If an element is greater than
Step 7: Check if second_largest Exists
- After the loop, the program checks if
second_largest
is stillINT_MIN
. If it is, this means no second largest element was found (e.g., all elements were the same), and an appropriate message is displayed.
Step 8: Display the Second Largest Element
- If a second largest element is found, it is displayed using the
printf
function.
Step 9: Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example 1:
Enter the number of elements in the array: 6 Enter 6 elements: 12 35 1 10 34 1 The second largest element is 34
Example 2 (All elements are the same):
Enter the number of elements in the array: 4 Enter 4 elements: 7 7 7 7 There is no second largest element in the array.
Example 3 (Negative numbers):
Enter the number of elements in the array: 5 Enter 5 elements: -3 -1 -7 -4 -2 The second largest element is -2
Conclusion
This C program demonstrates how to find the second largest element in an array. It covers basic concepts such as arrays, loops, conditional statements, and edge case handling, making it a useful example for beginners learning C programming.