In this article we are going to implement the program that is find the three biggest element in the array To solve this problem we are actually going to use C ++ Programming language.
In this article we are discussed about the below points
- Question
- Logic
- Source code
- Using function solve
- Feedback
Question-
Write a program in c language to find out the three biggest element in the array.
Logic
- Take an array of some integers then take a variable ( n ) for taking the input how many data the user want to enter then run a for loop to taking the inputs .
- Again run another for loop and inside the loop use if condition for checking the large elements in the array , Take a variable for comparing the large element,.
- Then check inside the if the condition that the value that is larger than that assign it to large and print the large value
- Then again run a for loop to and this time run till 3 times ( i !=3) and print the array
Source code
USING FOR LOOP
#include <iostream>
using namespace std;
int main()
{
int n,first,second,third;
//Initialize the largest three elements as zero
third = first = second = 0;
cout << "Enter the size :" << endl;
cin >> n;
int arr[100];
for (int i = 0; i < n; i++)
{
cout << "Enter the element no." << i + 1 << " :";
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i];
}
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
else if (arr[i] > third)
{
third = arr[i];
}
}
cout << "The large 3 elements are :\n";
cout << first << endl
<< second << endl
<< third;
return 0;
}
USING FUNCTION
#include <iostream>
using namespace std;
void threelarge(int arr[], int n)
{
int first, second, third;
//Initialize the largest three elements as zero
third = first = second = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i];
}
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
else if (arr[i] > third)
{
third = arr[i];
}
}
cout << "The large 3 elements are :\n";
cout << first << endl
<< second << endl
<< third;
}
int main()
{
int n;
cout << "Enter the size :" << endl;
cin >> n;
int arr[100];
for (int i = 0; i < n; i++)
{
cout << "Enter the element no." << i + 1 << " :";
cin >> arr[i];
}
threelarge(arr, n);
}
As we already discussed about the program logic hopefully you can understand the program now and still any doubts please comment below we will try to reach out you soon
Thank you.
Check Out OUR Home page for more information about practice problem
Also check Out
Also check Out Projects for Resume
Problems On C++
Q-2) Vectors In c++
Problems on TCS paper
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
FEEDBACK
If still you have any doubts please comment below and please share your approaches with us .
If still you have any doubts please comment below and please share your approaches with us .
Post a Comment
Post a Comment
Please do not spam in comment