Need to have an array full of random numbers within range. Code:
void fillArray(int *arr){
for(int i=0;i<(sizeof arr);i++){
arr[i] = rand() % 2 - 2;
}
}
int *arrPoint(int *arr, int max){
for (int i=0;i<max;i++){
printf("%d is %d\n",i,arr[i]);
}
return arr;
}
int main(int argc, char *argv[]){
srand ( time(NULL) );
int arr_f[15];
fillArray(arr_s);
arrPoint(arr_s, 15);
system("PAUSE");
return EXIT_SUCCESS;
}
Output:
0 is -2
1 is -1
2 is -2
3 is -1
4 is 0
5 is 0
6 is 4633240
7 is 2686652
8 is 1973724226
9 is 1974338216
10 is 2686716
11 is 1973744850
12 is 8
13 is 1973752206
14 is 1973752162
Press any key to continue . . .
What the hell? Putting rand() % 2 into brackets doesn’t help either. What these numbers are and how do I get rid of them?
P.S. Tried this in crappy Dev-C++ and Code::Blocks with the same result. Need the program to be small sized (putting it to dropbox), so no, I can’t use 100mb boost lib.
Since
arris a pointer to an integer,sizeof arris equivalent tosizeof (int *), which is apparently 4 (32-bits) on your platform. That’s clearly not what you want. You only passfillArraya pointer to the first element of the array.If you need the number of elements in the array a pointer points to, you need to pass that information. The C and C++ languages provides no way to tell how many bytes a pointer points to given just the pointer.
You do it correctly in
arrPoint. Do it that way infillArray.