Question

Define a std::vector<int> variable v and add 15 random integers into that container. Use the <random>...

  • Define a std::vector<int> variable v and add 15 random integers into that container. Use the <random> header file and the “uniform_int_distribution” to generate values between 1 and 100, inclusive. <random> is available only in C++11 and newer. If you prefer, you can use floating-point values instead of ints, in that case, look up uniform_real_distribution -- if you decide to do that, use float or double instead of int in all subsequent parts of the lab.
  • Write a print function that accepts const std::vector<int> & and prints the elements of the vector, using the iterator std::vector<int>::const_iterator. For this function, you should not use the index operator [] directly (when using a for loop, the performance of the iterator is typically better than the [] operator - why? Optionally, measure the time each approach takes (using [] or iterators) -- note that you may have to add an extra outer loop to repeat this many times since timing short runs is inaccurate; refer to the chrono documentation and examples).
  • Print the random values of the vector v, using your previously-defined function.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

#include <iostream>
#include <chrono>
#include <random>
using namespace std;
int main ()
{
vector <int>v;
vector <int>::iterator it;
int temp;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
minstd_rand0 generator (seed);
for(int i=1;i<=15;i++)
{
temp = generator()%100+1; //generates random number from 1 to 100
v.push_back(temp);
}
cout<<"Vector contains: ";
for(it = v.begin(); it != v.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
return 0;
}

Below is the screenshot of output

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.

Add a comment
Know the answer?
Add Answer to:
Define a std::vector<int> variable v and add 15 random integers into that container. Use the <random>...
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