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));
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.txtand
echo `seq 1 0.5 4` | ./main-stdio > test.io.out.txtNote the backquotes around the command
`seq 1 0.5 4`
. Read
about backquotes in man bash
, section "Command
Substitution". Read man seq
. If you have Danish locale, and
if your seq
produces decimal commas, try seq -f %g
1 0.5 4
or seq 1 0.5 4 | sed -e 's/,/./'
. Read
man sed
.
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)); }
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.txtand
./main-cmdline `seq 0 0.5 2` > test.cmd.out.txt
Prepare an input file with some numbers to make a plot of sine and cosine, for example,
echo `seq 0 0.2 5` > input.data.txt
Feed this file inout your prorams and produce tables of sine and cosine, for example,
cat input.data.txt | ./main-stdio > out.io.txtand
./main-cmdline `cat input.data.txt` > out.cmd.txt
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