mercredi 11 avril 2018

linear search for number vector in c++

I am trying to output 9 random non repeating numbers. This is what I've been trying to do:

    #include <iostream>
#include <cmath>
#include <vector>
#include <ctime>
using namespace std;
int main() {
    srand(time(0));
    vector<int> v;
    for (int i = 0; i<4; i++) {
        v.push_back(rand() % 10);
    }
    for (int j = 0; j<4; j++) {
        for (int m = j+1; m<4; m++) {
                while (v[j] == v[m]) {
                    v[m] = rand() % 10;
                }
        }
        cout << v[j];
    }
}

However, i get repeating numbers often. Any help would be appreciated. Thank you.




Aucun commentaire:

Enregistrer un commentaire