Problems "stdio and gnuplot"

Remember that all exercises must be made using Makefiles.
  1. Write a program main-stdio that reads a set of numbers from the standard input and writes these numbers together with their cosines in a table form into the standard output.
    Hint:

    double x;
    while( scanf("%lg",&x) != EOF ) printf("%lg \t %lg\n",x,cos(x));
    
  2. Test the program by sending some numbers to it's standard input and observing its standard output, for example,

    echo 1 2 3 4 5 | ./main-stdio > test.io.out.txt
    
    and
    seq 1 0.5 4 | ./main-stdio > test.io.out.txt
    
    If you have Danish locale, and if your seq produces decimal commas, try seq 1 0.5 4 | sed -e 's/,/./g'. Read man sed.
  3. Write a program main-cmdline that reads a set of numbers from its command-line-arguments and writes these numbers together with their sines in a table form into the standard output.

    Note: the length of the command-line string is limited by something like 2Mb so big data-sets should be read via standard input.

    Hint:

    for(int i=1;i<argc;i++) {
    	double x=atof(argv[i]);
    	printf("%lg \t %lg\n",x,sin(x));
    }
    
  4. Test the program by sending some numbers to it's command line and observing its standard output, for example,

    ./main-cmdline 1 2 3 4 5 6 > test.cmd.out.txt
    
    and
    ./main-cmdline `seq 0 0.5 2` > test.cmd.out.txt
    
    Note the backquotes around the command `seq 0 0.5 2`. Read about backquotes in man bash, section "Command Substitution".
  5. Prepare an input file with some numbers to make a plot of sine and cosine, for example (you only need sed if your seq produces decimal commas),

    seq 0 0.2 5 | sed -e 's/,/./g' > input.data.txt
    
  6. Feed this file inout your prorams and produce tables of sine and cosine, for example,

    cat input.data.txt | ./main-stdio > out.io.txt
    
    or
    ./main-stdio < input.data.txt > out.io.txt
    
    and
    ./main-cmdline `cat input.data.txt` > out.cmd.txt
    
  7. Make a plot of these sine and cosine tables using gnuplot/pyxplot. For example by creating a scipt file, plot.gpi,
    set terminal svg
    set output "plot.svg"
    plot "out.io.txt", "out.cmd.txt"
    
    and the running gnuplot on it,
    gnuplot plot.gpi