on-internet
← Community

Not sure why values don't wrap around

Stack Overflow ·

I'm trying to use a dynamically allocated array to store random values. I'm using C's built in random generator and specified that the random number should not go over 200. My implementation: int main(){ int random_num; int* dyn_arr = (int *) malloc(10 * sizeof(int)); //Populate the dynamic array. for(int i= 0; i <= 10; i++){ random_num = rand() * 200; dyn_arr[i] = random_num; } //Print out values of the array. for(int j = 0; j <= 10; j++){ printf("%d ", dyn_arr[j]); } return 0; } The problem: However, when I print the values stored in the dynamic array these values are going past the boundary

Comments

Sign in or sign up to comment and upvote.

No comments yet.