Question

In C++, use a set to create a container of 100 randomly-generated numbers in descending order.

In C++, use a set to create a container of 100 randomly-generated numbers in descending order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ CODE :

///////////////////////////////////////////////////////////////////
////////////AUTHOR : Ramcharan jethu/////////
//////////////////////////////////////////////////////////////////

#include<bits/stdc++.h>
using namespace std;

int main()
{
srand(time(NULL)); //srand function to generate different random number every time
  
//create a container using set that will store 100 random numbers in descending order

//greater<int> is a functor used to keep the numbers in descending order
set<int,greater<int> > s;
  
//generate and insert 100 random numbers in a set
for(int i=0;i<100;i++)
{
int temp=rand()%5000;   //use %5000 to generate numbers less than 5000. we can use any number instead of 5000
s.insert(temp);
}
  
cout<<"100 random numbers in descending order are : \n";
  
set<int,greater<int> >:: iterator it=s.begin(); //iterator to traverse the set
  
//traverse whole set and print the content
while(it!=s.end())
{
cout<<*it<<" ";
it++;
}
}

//please let me know if you have any doubt or further query through comment section

//Screenshot of output :

Add a comment
Know the answer?
Add Answer to:
In C++, use a set to create a container of 100 randomly-generated numbers in descending order.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT