mardi 19 juin 2018

Generating random vectors C++

I am working on a programme which takes an initial vector as an inpit.This vector is vector of size 20.The programme then generates 10 random vectors from this vector.For this purpose I choose 2 ransom indices in the initial vector and swap them with each other to generate a new vector.This is done to generate all the 10 new vectors. The 10 new vectors generated should be stored in the following 2 dimensional vector

vector<vector<int>> allparents

I have been able to generate 2 random indice numbers using the srand() function and then swap the elements at these indices for the initial vector.However I am unable to generate 10 of such random parents and then store them in the allparents 2D vector.My code is as follows :

#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<ctime>
using namespace std;
int main () {
srand(time(NULL));
int i1=(rand()%19+1);
int i2=(rand()%19+1);
cout<<i1<<endl;
cout<<i2<<endl; 
vector<int> initial= {71,127,428,475,164,253,229,395,92,189,41,110,443,490,278,305,28,58,371,560};
vector<vector<int>> allparents[10][20];
for(int r=0;r<10;r++){
for(int c=0;c<20;c++){
swap(initial[i1],initial[i2]);
allparents[r][c]=myvector[c];
cout << "myvector contains:";
cout<<allparents[r][c]<" "<<endl;
}}
return 0;
}

As I am new to vectors,I would request your help in this programme.Thanks.




Aucun commentaire:

Enregistrer un commentaire