Question

Write a program that generates 1,000,000 random integers between 0 and 9 and determines the longest...

Write a program that generates 1,000,000 random integers between 0 and 9 and determines the longest consecutive sequence of each of the numbers. The program displays the numbers and their longest consecutive sequence lengths in descending order.
in a c++ visual studios 2017
0 0
Add a comment Improve this question Transcribed image text
Answer #1

******************************************************************************************
Please Upvote the answer as it matters to me a lot :)
*****************************************************************************************
As per HomeworkLib expert answering guidelines,Experts are supposed to answer only certain number of questions/sub-parts in a post.Please raise the remaining as a new question as per HomeworkLib guidelines.
******************************************************************************************

#include <iostream>
#include <set>
using namespace std;
#include<time.h>
int main()
{
srand(time(NULL));
int n = 1000000;
int b[n];
for(int i=0;i<n;i++)
{
b[i]= rand()%10;
}
set<pair<int,int>> s;
for(int k=0;k<10;k++){
int max_length=0;
int current_max_length=0;
for(int i=0;i<n;i++)
{
if(b[i]!=k)
{
current_max_length=0;
}
else
{
current_max_length++;
if(current_max_length>max_length)
max_length=current_max_length;
}
}
s.insert({max_length,k});
}
for (auto it =s.rbegin(); it != s.rend(); ++it)
cout<<"Longest sequence of "<<(*it).second<<" is "<<(*it).first<<endl;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a program that generates 1,000,000 random integers between 0 and 9 and determines the longest...
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
ADVERTISEMENT