Hi everyone,
I got a piece of C code to "evolve"(Text wants me to create another file with the same structure but it's another thing)-in reality it's the past "went-bad" exam.-
I can't figure out how "srand" works with time in this code?
I repeat that I got a book where I study,but I already tried once this exam and everytime new functions appears.
I go up looking in the book and with ctrl+f = srand appears only 1 time near to int rand(void).
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 numbers[ARRLEN];
char name1[STRINGLEN+1];
char name2[STRINGLEN+1];
}
t_datum;
int main()
{
FILE *f;
t_datum x;
int i,k,n,temp;
srand(time(0)); // <-- WHAT'S THIS?
n = MAXREC + rand() % MAXREC;
if ((f = fopen("archive","wb")))
{
for (k = 0; k < n; k++)
{
for (i=0; i<ARRLEN; i++)
x.numbers[i]=rand()%10000;
for (i=0;i < STRINGLEN; i++)
{
temp=rand()%8;
if (temp==0)
x.name1[i]= 32;
else
x.name1[i]= 65 + rand()%26;
}
x.name1[STRINGLEN]='\0';
for (i=0;i < STRINGLEN; i++)
{
temp=rand()%8;
if (temp==0)
x.name2[i]= 32;
else
x.name2[i]= 97 + rand()%26;
}
x.name2[STRINGLEN]='\0';
fwrite(&x,sizeof(t_datum),1,f);
}
fclose(f);
}
else
printf("Error opening the file.\n");
return 0;
}
Ps. I have tomorrow the next C test and maybe I'll be willing to go if i can solve at least a part of this test...if you want the whole text requests for what to do tell me and I'll write the full text.