0
Array sizes?
https://code.sololearn.com/c2M974oBensR/#cpp How can I make it so it generates numbers depending on the size of the array? Right now, the size has to be 10. I want to be able to use the function on arrays of varying sizes. Like, if I make 2 arrays, 1 of size 5 and another of size 10, the function should generate numbers from 1 to 5 for the first array, and 1 to 10 for the second.
6 ответов
+ 2
Here:
https://code.sololearn.com/cQi4Zmqjbx6L/?ref=app
By the way, this method isn't too great since you'll be rerolling a lot for later indexes. Look up the Fisher-Yates shuffle for a better way:
https://en.m.wikipedia.org/wiki/Fisher–Yates_shuffle
+ 4
Add a new parameter, the length of your array. Create your used array dynamically (with malloc). Use the length as a boundary in your loop and when generating a random number. Free the used array before exiting the function.
0
I've tried that but I don't know enough to make it work. Can you give me an example? It would be greatly appreciated.
0
Thanks my dude. I've been told about that before but totally forgot to look it up.
0
Oh, yeah, I've already told you that. Didn't check if you were the same guy.
0
Lol. We're smart, we swear.
Anyway, I'm off to work. I'll work on using that algorithm once I get off.