APPFS
Advanced practical programming for scientists
 All Data Structures Files Functions Variables Typedefs Macros
Functions
ex1_gen.c File Reference

Appfs Exercise 1: data generator. More...

#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 Generate n random integer numbers and write them to stdout. More...
 

Detailed Description

Author
Thorsten Koch
Date
24Oct2014

gcc -std=c99 -O2 -o ex1_gen -static -m32 ex1_gen.c

Definition in file ex1_gen.c.

Function Documentation

int main ( int  argc,
char **  argv 
)

The first number is 123456789; Every 1000037 number the number is negative.

Definition at line 15 of file ex1_gen.c.

16 {
17  const char* usage = "usage: %s count\n";
18 
19  /* Check arguments, we need a positive number
20  */
21  if (argc < 2 || atoi(argv[1]) < 1)
22  {
23  printf(usage, argv[0]);
24  exit(-1);
25  }
26  const int r = 123456789;
27  int n = atoi(argv[1]);
28 
29  srand(130267);
30 
31  fwrite(&r, sizeof(r), 1, stdout);
32 
33  for(int i = 0; i < n; i++)
34  {
35  r = rand(); // bad random number generator
36 
37  if (0 != i && 0 == (i % 1000037))
38  r *= -1;
39 
40  // fprintf(stderr, "%d\n", r);
41  fwrite(&r, sizeof(r), 1, stdout);
42  }
43  return EXIT_SUCCESS;
44 }