Write a loop that fills a vector V with ten different random numbers between 1 a
ID: 3693548 • Letter: W
Question
Write a loop that fills a vector V with ten different random numbers between 1 and 100. I am having issues with the random numbers my code is:
#include <iostream>
#include <stream>
using namespace std;
class arrayClass
{ int x; public: void setX(int i)
{x = i; } int getX() {return x;}
}; int main()
{ arrayClass obs[10]; int i; int n; int reviewN; int randomSelect; bool duplicate = true; /*random number generator*/ for (i=0; i<10;i++) if(i==0) { obs[i].setX(rand()%99+1); } else { randomSelect = (rand()%95+5); /*loop through vector until sure not a duplicator*/ duplicate = true; do { for (n=0; n
Explanation / Answer
here is the correct working code
for random number generation use- rand() function which is preset in the cstdlib library
here is the code snippet in cpp ->
run it as ->
g++ filename.cpp
#include<iostream>
#include<vector>
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
int vec_size = 10;
vector<int> myvec(vec_size);
for( int i = 0; i < vec_size; ++i ) {
myvec[i] = rand() % 100 + 1;
cout << myvec[i] << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.