Hi everyone
I got a C code to "evolve"(Problem asks me to create another "database" or archive file,but this is another thing).-In reality this is the past "went-bad" exam.
I can't understant how srand works and how it works with time..
I got my book where I study, but searching with ctrl+f = srand appears only 1 time near "int rand(void)" explaining that starting with seed it creates new pseudo random numbers.
But what has it to do with time,and the whole text?
This is the code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRLEN 30
#define MAXREC 300
#define STRINGLEN 20
typedef
struct
{
int numeri[ARRLEN];
char nome1[STRINGLEN+1];
char nome2[STRINGLEN+1];
}
t_dato;
int main()
{
FILE *f;
t_dato x;
int i,k,n,temp;
srand(time(0));
n = MAXREC + rand() % MAXREC;
if ((f = fopen("archivio","wb")))
{
for (k = 0; k < n; k++)
{
for (i=0; i<ARRLEN; i++)
x.numeri[i]=rand()%10000;
for (i=0;i < STRINGLEN; i++)
{
temp=rand()%8;
if (temp==0)
x.nome1[i]= 32;
else
x.nome1[i]= 65 + rand()%26;
}
x.nome1[STRINGLEN]='\0';
for (i=0;i < STRINGLEN; i++)
{
temp=rand()%8;
if (temp==0)
x.nome2[i]= 32;
else
x.nome2[i]= 97 + rand()%26;
}
x.nome2[STRINGLEN]='\0';
fwrite(&x,sizeof(t_dato),1,f);
}
fclose(f);
}
else
printf("Errore nell'apertura del file.\n");
return 0;
}
Ps. I got this code because I already tried this test and I'll be willing to try tomorrow test if I can solve this past one..
If you want the full test text,tell me and i will write it