C Program to Find the Largest Two Numbers in a given Array

«
»

This is a C Program to calculate the largest two numbers in a given Array.

Problem Description

We have to write a program in C such that the program will read the elements of a one-dimensional array, then compares the elements and finds which are the largest two elements in a given array.

Expected Input and Output

1. Finding Largest 2 numbers in an array with unique elements:

advertisement

If we are entering 5 elements (N = 5), with array element values as 2,4,5,8 and 7 then,
The FIRST LARGEST = 8
THE SECOND LARGEST = 7

2. Finding Largest 2 numbers in an array with recurring elements:

If we are entering 6 elements (N = 6), with array element values as 2,1,1,2,1 and 2 then,
The FIRST LARGEST = 2
THE SECOND LARGEST = 1

Problem Solution

In this program, we have to find the largest and second-largest elements present in the array. We will do this by first saving the values of the first element in the variable ‘largest’ and second element in the variable ‘second-largest’. Then we will start from the third element and compare and swap with ‘largest’ and ‘second-largest’ numbers if another larger number is found in this array. This will go on N-2 times and the program ends.

advertisement

The sequence of steps for the solution will be as follows:
1. Create an array and define the elements of the array.
2. Considering the first element of the array to be the largest number and second element of the array to be the second largest element.
3. Interchange these two numbers if required.
4. Now run the loop from the third element of the array to the last element.
5. Scan element of the array, comparing array elements with the first largest and second-largest numbers, changing both or one if required.
6. In the end, after for loop, we will be getting the actual first largest and the second largest number in the array.

Program/Source Code

Here is the source code of the C program to calculate the largest of two numbers in a given array. The program is successfully compiled and tested using Turbo C compiler in the Windows environment. The program output is also shown below.

  1. /*
  2.  * C program to read elements into an array and find the
  3.  * largest two elements in a given array.
  4.  */
  5.  
  6. #include <stdio.h>
  7. int main ()
  8. {
  9.     int n = 0, i = 0, largest1 = 0, largest2 = 0, temp = 0;
  10.  
  11.     printf ("Enter the size of the array\n");
  12.     scanf ("%d", &n);
  13.     int array[n];
  14.     printf ("Enter the elements\n");
  15.     for (i = 0; i < n; i++)
  16.     {
  17.         scanf ("%d", &array[i]);
  18.     }
  19.  
  20.     printf ("The array elements are : \n");
  21.     for (i = 0; i < n; i++)
  22.     {
  23.         printf ("%d\t", array[i]);
  24.     }
  25.  
  26.     printf ("\n");
  27.  
  28.     largest1 = array[0];
  29.     largest2 = array[1];
  30.  
  31.     if (largest1 < largest2)
  32.     {
  33.         temp = largest1;
  34.         largest1 = largest2;
  35.         largest2 = temp;
  36.     }
  37.  
  38.     for (int i = 2; i < n; i++)
  39.     {
  40.         if (array[i] > largest1)
  41.         {
  42.             largest2 = largest1;
  43.             largest1 = array[i];
  44.         }
  45.         else if (array[i] > largest2 && array[i] != largest1)
  46.         {
  47.             largest2 = array[i];
  48.         }
  49.     }
  50.  
  51.     printf ("The FIRST LARGEST = %d\n", largest1);
  52.     printf ("THE SECOND LARGEST = %d\n", largest2);
  53.  
  54.     return 0;
  55. }
Program Explanation

1. Declare an array of user-defined size.
2. Using for loop, define the elements of the array.
3. Consider the first element of array to be the first largest number (store it in a variable, largest1).
4. Consider the second element of array to be the second-largest number (store it in a variable, largest2).
5. Now interchange the values if the value of largest1 is smaller than the largest2.
6. Run a for loop from the third element of the array till the last element of the array, wherein each element will be compared to the largest1.
i) If the value of the current element is larger than largest1 then, we put the value of the current element to largest1 and value of largest1 to largest2.
ii) else if, the value of the current element is larger than the largest2 and is not equal to largest1, then we put the value of the current element to largest2.

7. Running loop to the end will give us the actual first largest and second-largest number.
8. Exit

advertisement
Runtime Test Cases

Here is the runtime output of the C program with 2 different test cases.

Test case 1: Here, the elements are unique. We are reading an array of 5 elements with unique values 2,4,5,8 and 7. The program is displaying the largest 2 numbers.

Enter the size of the array
5
Enter the elements
2
4
5
8
7
The array elements are :
2       4       5       8       7
The FIRST LARGEST = 8
THE SECOND LARGEST = 7

Test case 2: Here, the elements are recurring. We are reading an array of 5 elements with recurring values 2,1,1,2,1. The program is displaying the largest 2 numbers.

Enter the size of the array
6
Enter the elements
2
1
1
2
1
2
The array elements are :
2       1       1       2       1       2
The FIRST LARGEST = 2
THE SECOND LARGEST = 1

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn