vendredi 8 juin 2018

generating and storing random arrays in c++

I am working on a programme which takes the initial array as an input and then random;y generated 10 random arrays from the initial array.

I have written a function to generate a single random array from the initial array.However,I am unable to develop a code which calls this function in the main function and generates 10 such random arrays.I will also be using these 10 randomly generated arrays for further calculation in the main function. Since my knowledge of C++ is limited for now,I am not aware of using vectors for dynamic allocation.It would be of great help if anyone can guide be regarding the code to be used in the main function.

Here int initial[] is the first array used to generate 10 other random arrays using the int *newparents() function.When the newparents() function is called in main it returns one randomlu shuffled array. I would request your help in generating 10 random arrays in the main function by calling the newparents() function and then storing them for further use.

The code written by me is as follows :

int initial[] = 
{10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200};
int size=sizeof(initial)/sizeof(initial[0]);

int *newparents(){
int *pt=new int[size];
random_shuffle(&initial[0],&initial[size]);
for(unsigned i=0;i<size;i++){
pt[i]=initial[i];
}
return pt;
}

int main(){
int *p;
p=newparents();
cout<<"p1="<<" " ;
for(int i=0;i<size;i++){
cout<<*(p+i)<<" ";
}
return 0;
} 

Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire