I am trying to write a function that would be able to look at all the indexes of a vector which have no values stored in them and then replace one of them randomly with the number 2.
How it works(or at least how it should):
Find number of indices with zero ---> used this to find a random number between 1 and number of zero founs ---> finds that index and changes it to 2.
int random_num(std::vector<int>&v){
int length = std::sqrt(v.size());
int count = 0;
for(int i = 0; i < v.size(); i++){
if(v[i] = 0){
count++;
}
}
srand(time(0));
int tmp = (rand()%(count+1));
for(int i = 0; i < length; i++){
if(v[i] = 0 && i == (tmp-1)){
v[i] = 2;
}
}
}
The only issue here is that when I compile this, the values of the vector all come out as 0 when I print them out. What is wrong with this?
Aucun commentaire:
Enregistrer un commentaire