I have two arrays:
array1( 'Apple',
'Pear',
'Banana',
'Apricot',
'Watermelon',
'Peach',
'Pineapple',
'Strawberry',
'Melon',
'Pomegranate',
'Oranges',
'Tangerine' );
The Size of a tangerineeee.. Ehem! I just cant help it. It was funny as hell.. Now lets get back to the second array.
array2( 'Fruits On Table',
'Fruits In Basket',
'Fruits In Fridge' );
I wanna generate a list
or an array
or a string
whatever can have the size of a tangerinee…
Something that can merge both of these arrays randomly without repeating the first array. Which is gonna be like:
Tangerine( 'Tangerine - Fruits In Fridge',
'Apple - Fruits On Table',
'Pear - Fruits In Basket',
'Peach - Fruits In Fridge',
'Apricot - Fruits On Table',
'Oranges - Fruits In Basket',
'Melon - Fruits In Fridge',
'Watermelon - Fruits On Table',
'Pineapple - Fruits On Table',
'Pomegranate - Fruits In Basket',
'Strawberry - Fruits In Fridge',
'Banana - Fruits In Basket' );
if you look carefully they are randomly chosen from both arrays and merged together without repeating the first array..
So how can i do this with C++?
I’m a newbie on C++
also a little algorithm about it would be nice.
You could do it like someone might in real life. You can treat the array like a bag that you randomly choose out of, and remove the item after each choice. There’s no chance you’ll pick a fruit twice that way! And if you don’t want to damage the original array, you can make a copy and use that as the “bag”…
Of course, there’s lots of performance problems with doing such things naively:
Get random element and remove it
How to select a random element in std::set?
…but if you don’t understand what they are and why to avoid them, then jumping straight to a sophisticated shuffle solution as presented by StackOverflowers will seem a bit…suspicious. 🙂