vendredi 4 juin 2021

Generating a sequence of different numbers

I am trying to generate a sequence of 6 random numbers with all the numbers being different from each other(no repetition of the numbers). But when I try to test whether I have successfully generated those numbers or not by trying to print them out, the code compiles successfully but nothing appears on the screen. Something seems to be wrong with my use of the function erase() because once I remove it the code produce some output although not what I am looking for.

#include<time.h>
using namespace std;
int main(void)
{
srand(time(NULL));
    vector<int>v;
    vector<int>V;
    int arr[6];
    int i,j;
    for(i=1;i<=6;i++)
    {
        V.push_back(i);
    }
    
   for(int i=0;i<6;i++)
   {
       cout<<V[i]<<" ";
   }
   for(int i=0;i<6;i++)
   {
       int n=rand()%6;
       v.push_back(V[n]);
       V.erase(V.begin()+n);
   }
   cout<<endl;
    for(int i=0;i<6;i++)
   {
       cout<<V[i]<<" ";
   }
    return 0;
}



Aucun commentaire:

Enregistrer un commentaire