Hi,
I need to create a program that outputs a .txt file with a 20x30 array of random numbers from 0 to 9. What I have creates the .txt file, although it is empty when opened. Here is what I have:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
FILE *fp;
int i, j;
char list[20][30];
if((fp = fopen("numbers.txt", "a+")) == NULL)
{
fprintf(stdout, "Can't open \"numbers\" file.\n");
exit(1);
}
srand(time(NULL)); //initialize random seed
puts("the random numbers from 0 to 9 are:");
for (i=0; i<20; i++)
{
for (j=0; j<30; j++)
{
list[i][j] = rand() % 10; //random number between 0 and 9
fprintf (stdout, "%d ", list[i][j]);
}
putchar('\n');
}
if(fclose(fp) != 0)
{
fprintf(stderr,"Error closing file\n");
}
return 0;
}