I tried to create an array and fill it with random numbers using rand(), and it would seem that I got the code to work but when I return the data to the screen it all appears to be correct except in value arrayPrimary[5][2]. It just shows a garbage value and I cant seem to figure out why it would do that in only this spot. I'm still new to learning the C language so please be as descriptive as possible to help me understand:
#include <stdlib.h>
main ()
{
srand (time(NULL));
int arrayPrimary[5][5];
int x,y,a,b;
for(x=1; x<6; x++)
{
for (y=1; y<6; y++)
{
int *z= &arrayPrimary[x][y];
*z=rand() %10;
}
}
for(a=1; a<6; a++)
{
for(b=1; b<6;b++)
{
printf ("The current value of [%d][%d] is:%d\n",a,b,arrayPrimary[a][b]);
}
}
return 0;
}
0 to 4for both dimensions ofarrayPrimaryso you are invoking undefined behavior by accessing index5.<stdio.h>and<time.h>header?arrayPrimary[x][y] = rand() % 10?