+ 2

Using an RNG in a for loop inside of a function

https://code.sololearn.com/c576Im1lWz16/?ref=app what im trying to do is insert an array into a function, have the function use a for loop to fill the array with random numbers ***From one to six*** the first number works and will be a number from 1-6 . the rest however will pretty much completely ignore the 1-6 rule and make big numbers.

6th Jun 2017, 1:21 PM
K1llJ0Y
K1llJ0Y - avatar
14 Réponses
+ 5
odd. I edited my answer to @Dennis after he did. and it says I posted first.. sololearn strikes again
6th Jun 2017, 2:32 PM
jay
jay - avatar
+ 4
here is a good article on arrays and pointers https://www.tutorialspoint.com/cplusplus/cpp_pointer_to_an_array.htm
6th Jun 2017, 1:48 PM
jay
jay - avatar
+ 4
have a look at the code I linked to see what I did to get it to work
6th Jun 2017, 2:14 PM
jay
jay - avatar
+ 4
yep. it works for this example. i saw and updated once I realised it wont work when more numbers are added edit: yes based on what you said earlier :) I usually just use std::array. much easier to use
6th Jun 2017, 2:25 PM
jay
jay - avatar
+ 2
How would I go about this in my case. I'm sorta new and pretty much never use pointers
6th Jun 2017, 1:35 PM
K1llJ0Y
K1llJ0Y - avatar
+ 2
Do not call srand(time(0)); every time. Only do it once at the start of main and dont look at it again after that. Also do not use (sizeof(x)/sizeof(x[0])) in a function this way. It will not give you the correct array size. In this case (sizeof(x)/sizeof(x[0])) returns 2. So not the entire array gets initialized and you end up with weird values which probably explains why you get big numbers. Instead you can pass in the size of the array together with the array, like: void someFunction(int arr[], int size) There are more ways to do it but this is probably the easiest to start out with. Here's an example of *more ways: template<size_t N> void someFunction(int (&arr)[N]) { std::cout << (sizeof(arr)/sizeof(arr[0])) << std::endl; std::cout << N << std::endl; //Same results }
6th Jun 2017, 2:10 PM
Dennis
Dennis - avatar
+ 2
"Do not call srand(time(0)); every time. Only do it once at the start of main and dont look at it again after that." You still didn't do that, that's what causing your problem. int main() { srand(time(0)); //Rest of the code }
7th Jun 2017, 1:10 PM
Dennis
Dennis - avatar
+ 1
updated it and now the generator acts correctly. however the smallest function doesn't and I don't see the problem. the smallest function spits out some big number
6th Jun 2017, 2:11 PM
K1llJ0Y
K1llJ0Y - avatar
+ 1
@K1llJ0Y I'm assuming you're trying to get the smallest of the array. Do it like this: int smallest(int x[], int size) { int smallest = 2147483647; //Biggest possible int for(int i = 0; i < size; i++) { if(x[i] < smallest) { smallest = x[i]; } } return smallest; } and then call it like: int four_D_six[4]; int small; const int SIZE = sizeof(four_D_six) / sizeof(four_D_six[0]); //Yes, you can get the size here ^^ small = smallest(four_D_six, SIZE);
6th Jun 2017, 2:21 PM
Dennis
Dennis - avatar
+ 1
@jay Probably cause I edited it too, my "returns size of type" wasn't completely correct so I quickly added something ^^
6th Jun 2017, 2:34 PM
Dennis
Dennis - avatar
+ 1
@jay Nice try, but sizeof(x) will return the size of its type * its size. ( which it doesn't know in a function ) Which is an int, and the size of an int is 4 *, so it will always return 4 * * (on computers where int is 4 :P) Try it with any array that is bigger or less than 4 ^^ EDIT: I see you edited it :P
6th Jun 2017, 2:35 PM
Dennis
Dennis - avatar
+ 1
thank you all. the program is complete, probably could've made it a bit more tidy and organized but I'm just happy I got it done. with the help of all of you of course.
7th Jun 2017, 3:17 PM
K1llJ0Y
K1llJ0Y - avatar
0
so now my problem is that it generates the same number when I use the function multiple times
7th Jun 2017, 1:06 PM
K1llJ0Y
K1llJ0Y - avatar