Write a loop that fills a vector V with ten random numbers b

Write a loop that fills a vector V with ten random numbers between 1 and 100.

Solution

VectorTest.cpp

#include <iostream>
#include <vector>
#include <stdlib.h>   
#include <time.h>   
using namespace std;

int main()
{
/* initialize random seed: */
srand (time(NULL));
vector<int> V;
  

for(int i=0; i<10; i++){
/* generate secret number between 1 and 100 */
V.push_back(rand() % 100 + 1);
}

cout<<\"Vector elements are: \"<<endl;
for(int i=0; i<10; i++){
cout<<V[i]<<\" \";
}
cout<<endl;

return 0;
}

Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                            

sh-4.3$ main                                                                                                                                                                                                                    

Vector elements are:                                                                                                                                                                                                            

47 62 15 21 71 89 43 97 73 8

Write a loop that fills a vector V with ten random numbers between 1 and 100.SolutionVectorTest.cpp #include <iostream> #include <vector> #include &

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site