Hi dear readers ,
Hope this article will add some extra knowledge to you to learn programming so read the article full get the important points from the article.
In this article we are going to discuss about How find the largest element in an array range (This means we ask to user how many large elements you want to find from the array ).
We solve this problem by the help of C++ programming language If You do not know about C++ Programming don't worry just understand the logic i discussed below and you will get an idea .
we use some built in functions to find the solutions so Lets go.
The Below points mention are discussed in this article
- Question
- Logic to solve
- source code
- source code explanation
- output
- feedback
Question -
Program to find largest element in an array
Logic to solve
- First we declare a variable (Let The variable N) is used to take the that how many elements user want to input
- Then run a for loop and take the elements input
- Now use the built-in function sort and sort the array in descending order
- Then declare a variable (Let the variable K) It is used to take that how many large value user want
- Then again run a for loop and make the condition Till "K" and print out the array in the range.
Hope you guys understand the logic some how so lets go and implement it into code
Source code
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
int arr[100];
cout << "enter the size of array:"<<" :";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter the data :no. "<<i+1 <<" :"<< endl;
cin >> arr[i];
}
int k;
cout<<"Enter the number how many large array you need :"<<endl;
cin>>k;
//for ordering from large to small
sort(arr,arr+n,greater<int>());
//greater element used to order in descending order
cout<<"The large arrays are :\n";
for (int i = 0; i < k; i++)
cout<<arr[i];
}
Source code explanation
- I Think You guys will understand the program from the logic we have already discussed above if not please read again and try to understand.
1-) Here we use the built in function which is actually is use to sort the elements in ascending order but we make one thing here that we use greater (It is also a built in function is make the array into large to small )
- The above line i explain that is the main thing in this program if you understand that line the program is become easier for you.
So till now we understand the logic and implementation for the program so now lets go for the OUTPUT of the program
Output
enter the range - 6
2 4 6 3
enter the range how many big numbers you want
3
The biggest element are in range 3
6 4 3
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