Program to find Second Largest Element in Array in C

second-largest-element-in-array-in-c-program

Program Description:

Write a Program to find the Second Largest Element in Array in C programming language. We have looked at the Minimum number in the array and Maximum Element in Array programs in earlier articles, In today’s article, We will look at the Second Largest element in the given array.

Example Inputs and Outputs:

Input:

Output:

Here we have provided the Largest and Second Largest Numbers as output.

Prerequisites:

We need to know the basics of the Arrays and functions in C to better understand the program. Please go through the following articles to learn more.

Second Largest Element in Array in C Program Explanation:

Here is the explanation to find the largest and second largest number in the array Program.

  1. First of all, Declare a numbers array, The numbers array holds 5 elements. Here we have hardcoded the size but you can also use a constant( const) or Macro.
  2. Take the input from the user and update the numbers[] array elements.
  3. Then, create two variables, largest and second_largest, and initialize them with the INT_MIN. ( Learn more about the INT_MIN at Size and Limits of datatypes in C)
  4. Iterate through all array elements
    • Update the second_largest and largest variables if you find a number that is larger than the present largest (i.e ( numbers[i] > largest)). First, update the second_largest with largest, Then update the largest with number[i].
    • If you find any number which is smaller than largest number and greater than the second_largest ( i.e (numbers[i] > second_largest && numbers[i] < largest)) Then update the second_largest number with number[i].
  5. Once the above loop is completed, The largest and second_largest variables hold the largest and second largest numbers correspondingly.
  6. Finally, display the largest and second_largest numbers on the console.

Second Largest Element in Array in C Program Algorithm:

  1. Start
  2. Declare numbers array – int numbers[5];.
  3. Update the array with user input.
    • Iterate from 0 to 4 and update &numbers[i] using scanf() function.
  4. Create largest and second_largest. Initialize with INT_MIN.
  5. Iterate over array elements and update the largest and second_largest elements.
    • Iterate from 0 to 4 ( size-1).
    • If numbers[i] > largest, Then update second_largest = largest; and largest = numbers[i];
    • else if(numbers[i] > second_largest && numbers[i] < largest), Then update the second_largest = numbers[i];
  6. Print the largest and second_largest using printf.

Program to find Second Largest Element in Array in C:

Here is the C Program to calculate the Largest and Second Largest element in C

📢 As we are using the INT_MIN, We need to include the limits.h header file.

Program Output:

Compile and Run the Program using GCC.

Test Case 1: Positive Numbers:

find-second-largest-element-in-array-in-c-output

Test Case 2: Negative Numbers and Zeros:

Largest and Second Largest Element with using Custom Array:

In the above program, We have hard-coded array size as 5. Let’s create a large array and allow the user to specify the required size.

Modify the above program to accept the array size from the user. Note: This is not a dynamic array.

Program Output:

Let’s compile and run the C Program.

Program to find Second Largest Element in Array in C using Functions:

We have used only one function (i.e main()) in the above programs. So let’s divide the program and use the C Functions.

The functions improve the code reusability and readability. ( Advantages of Functions )

Let’s rewrite the above program and create two functions named read() and get_second_largest()

We have defined two functions here.

  1. The read() function.
    • Prototype of the read() function – void read(int numbers[], int size);
    • The read function takes two arguments – numbers array and its size. The read() function prompts the user for array elements and updates the number array.
  2. The get_second_largest() function.
    • Prototype of the get_second_largest() function – int get_second_largest(int numbers[], int size);
    • The get_second_largest() function also takes two formal arguments one is the input array numbers[] and the sizeof the number’s array.
    • The get_second_largest() function finds the second largest element in the array by comparing all elements.
    • Finally, It returns the Second Largest element in the array back to the caller.

We called the read() and get_second_largest() functions from the main() function.

📢 Here we have returned only a single value from the get_second_largest() function. If you want to return multiple values like largest and second_largest, Then create a static array and return it back to the caller.

Program Output:

Compile and Run the Program and observe the output.

second-largest-element-in-array-using-function-in-c

Exercise: Sort the array and Get the last and last-second numbers:

We can also get the largest and second largest elements by sorting the array and accessing the last two elements. Try to sor the array and get the largest and second largest elements.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply