This is a C Program to calculate the largest two numbers in a given Array.
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.
1. Finding Largest 2 numbers in an array with unique elements:
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
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.
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.
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.
/*
* C program to read elements into an array and find the
* largest two elements in a given array.
*/
#include <stdio.h>
int main ()
{
int n = 0, i = 0, largest1 = 0, largest2 = 0, temp = 0;
printf ("Enter the size of the array\n");
scanf ("%d", &n);
int array[n];
printf ("Enter the elements\n");
for (i = 0; i < n; i++)
{
scanf ("%d", &array[i]);
}
printf ("The array elements are : \n");
for (i = 0; i < n; i++)
{
printf ("%d\t", array[i]);
}
printf ("\n");
largest1 = array[0];
largest2 = array[1];
if (largest1 < largest2)
{
temp = largest1;
largest1 = largest2;
largest2 = temp;
}
for (int i = 2; i < n; i++)
{
if (array[i] > largest1)
{
largest2 = largest1;
largest1 = array[i];
}
else if (array[i] > largest2 && array[i] != largest1)
{
largest2 = array[i];
}
}
printf ("The FIRST LARGEST = %d\n", largest1);
printf ("THE SECOND LARGEST = %d\n", largest2);
return 0;
}
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
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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Check C Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Practice BCA MCQs