I've created this 2D 21x21 array that has all it's values set to -1. I wrote it to print the address and value and somehow it only starts at [6][19] why?
What i want to do is to replace some of the -1 values with random numbers from 0 to 100 in the same array. I know i need to seed it with srand but i'm having problems connecting the functions since i'm a total beginner in C.
The 2D array is:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int a[21][21], i , j;
for (i = 0; i < 21; i++)
{
for ( j = 0; j < 21; j++)
{
a[i][j] = -1;
printf("a[%d][%d] = %d \n", i, j, a[i][j]);
}
}
return 0;
}
My seed looks like this:
#include <stdio.h>
#include <stdlib.h>
// random seed
int GetRand(int min, int max);
int main(void)
{
int i, r;
for (i = 0; i < 21; i++)
{
r = GetRand(0, 100);
printf("Your number is %d \n", r);
}
return(0);
}
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
srand(time(NULL));
Init = 1;
}
rc = (rand() % (max - min +1) +min);
return (rc);
}
I know they're two different files, that's why there are to main functions. I'm just trying to figure out how a random seed can "override" the values of the 2D array.
I've been searching tutorials but all of them seem to be for C++ so that's why i;m asking here.
Any good advice?
Aucun commentaire:
Enregistrer un commentaire