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.

28th Jan 2019, 11:00 AM
Daniel Cooper
Daniel Cooper - avatar
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
28th Jan 2019, 4:10 PM
Zen
Zen - avatar
+ 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.
28th Jan 2019, 11:32 AM
Zen
Zen - avatar
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.
28th Jan 2019, 1:51 PM
Daniel Cooper
Daniel Cooper - avatar
0
Thanks my dude. I've been told about that before but totally forgot to look it up.
28th Jan 2019, 4:57 PM
Daniel Cooper
Daniel Cooper - avatar
0
Oh, yeah, I've already told you that. Didn't check if you were the same guy.
28th Jan 2019, 4:59 PM
Zen
Zen - avatar
0
Lol. We're smart, we swear. Anyway, I'm off to work. I'll work on using that algorithm once I get off.
28th Jan 2019, 5:05 PM
Daniel Cooper
Daniel Cooper - avatar