/* File: makedata.c Make a file of random integers to test our sorting programs. */ #include #include #include #include main(int argc, char *argv[]) { int i, r, n, *A ; long seed ; FILE *ofile ; /* Sanity check */ if (argc != 3) { fprintf(stderr, "Usage: makedata n filename\n") ; exit(1) ; } /* Get number of items */ r = sscanf(argv[1],"%d", &n) ; if (r != 1) { fprintf(stderr, "%s not an integer!\n", argv[1]) ; exit(1) ; } /* Open file */ ofile = fopen(argv[2],"wb") ; if (ofile == NULL) { printf("Could not open file: %s\n", argv[2]) ; exit(1) ; } /* Set random seed to time in seconds since 01/01/70 */ seed = (long) time(NULL) ; printf("Set random seed to %ld\n", seed) ; srand48(seed) ; /* Allocate memory for our array */ A = (int *) malloc(n * sizeof(int)) ; for ( i = 0 ; i < n ; i++) { A[i] = (int) lrand48() ; } r = fwrite(A, sizeof(int), n, ofile) ; if (r != n) { fprintf(stderr, "Oops, only %d numbers written\n", r) ; } fclose(ofile) ; }