In this tutorial, you will learn how to write C program to find first two largest numbers in a given array.
Below are the approach which we will be follow to write our program:
- In this program we will insert value in array.
- Then we will iterate the array using for loop and compare elements till the end of array element. During comparison we will find the greatest two elements in a given array.
- You can also follow a approach like first sort an array in decending order and then select top two distinct elements of an array.
How our program will behave?
As we have already seen above our logic to find the first two maximum number of a given array.
Our program will take an array as an input.
And on the basis of inputs and comparison it will print two greatest elements.
Program to find two largest number in an array
#include <stdio.h>
#include <stdlib.h>
void twoMax(int *arr, int size) {
int i;
int firstlargest = arr[0];
int secondlargest = arr[0];
for (i = 1; i < size; i++) {
if (arr[i] > firstlargest) {
secondlargest = firstlargest;
firstlargest = arr[i];
} else if (arr[i] > secondlargest && arr[i] != firstlargest) {
secondlargest = arr[i];
}
}
printf("%d and %d are the top two maximum numbers\n", firstlargest, secondlargest);
}
int main() {
int size, i, *arr;
printf("=== C Program to Find the Top Two Maximum Numbers in an Array ===\n");
printf("Enter the size of an array: ");
scanf("%d", &size);
arr = (int*)malloc(sizeof(int) * size);
// Taking input for the array
for(i = 0; i < size; i++) {
printf("Please give value for index %d: ", i);
scanf("%d", &arr[i]);
}
twoMax(arr, size);
free(arr); // Freeing the dynamically allocated memory
return 0;
}
Output:
=== C Program to Find the Top Two Maximum Numbers in an Array ===
Enter the size of an array: 5
Please give value for index 0: 4
Please give value for index 1: 3
Please give value for index 2: 2
Please give value for index 3: 5
Please give value for index 4: 1
5 and 4 are the top two maximum numbers
Explanation of above program to find first two maximum number of an array
- In the above program we have taken two int variable size, i and one integer pointer variable arr to declare an array in main() method.
- size variable will take the size of array given by user and arr will be array which size is calculated by malloc function dynamically at run time.
- Now value will be assigned to each index of an array which is given by user using scanf() function of C.
- We have a function called twoMax(arr,size) which will take array arr and size of array as an input.
- The body of function twoMax(int *arr, int size) has all the logic of finding first two largest number of an array.
- Method twoMax(int *arr, int size) have three variable i, firstlargest and secondlargest variable.
- In initial we assuming first and second largest value is 0.
- for loop is to iterated the input array of twoMax method.
- if statement inside for loop is to compare and calculate first and second largest value.
This was the concept to find program first and second largest value or we can say that top two maximum using C programming language.
What did you think?
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…