0) Random number generation. (50 pts)
Often, computer scientists, statisticians, physicists, social scientists, and mathematicians need a list of random numbers. Political pundits also find lists of random numbers useful to provide 'statistical' evidence of their arguments.
Implement a tool to generate and print a sequence of 10 random numbers. Each random number should be an integer in the range from
0 to 100, inclusive (that means both 0 and 100 should have a chance of appearing). I do not expect you to implement your own random number generator. I expect you to use the standard way of obtaining randomness in a secure fashion.
$ ./grand
56 77 91 2 33 40 72 100 6 2
Augment your program to take a command line parameter specifying how many random numbers to generate. That is, if grand is invoked by:
$ ./grand 4
it will output 4 random numbers from 0 to 100, inclusive. Make sure your program does something sensible if the supplied argument is not a valid integer. There should not be an arbitrarily imposed upper limit to this number (in other words, do not have an artificial cap on the value of the argument).
Since it is easy to forget how to use programs, it is often useful to provide a well-known command line switch (or parameter) that tells the user how to invoke the program. Augment your program to recognize two more command line arguments, '-h' and '--help' that prints out the usage information for the program. For example,
$ grand -h
and
$ grand --help
should output the following usage information:
grand [-h | --help] : output this usage message.
grand [n] : print out n random integers in [0,100]
Finally, augment your program to print out its version. Don't forget to add this usage case to your help dialog.
$ grand –version OR -v
grand-0.0.3
1) Functions and Recursion (50 pts)
Experiment with recursion. The Fibannaci sequence is a famous naturally recursive series of numbers whose ratio approaches the Golden Ratio.
f(n) = f(n-1) + f(n-2)
Implement a program named 'fib' that uses recursion to calculate and print the first n Fibannaci numbers, where 'n' is less than or equal to 30.
Usage:
fib [n]
For example, 'fib 4' should output:
fib(0) = 1
fib(1) = 1
fib(2) = 2
fib(3) = 3
fib(4) = 5
Modify your program so that providing the --target or -t option will print only the nth Fibannaci number.
$ ./fib --target 3
fib(3) = 3
Add the ability for your program to calculate the Fibannaci sequence iteratively rather than recursively. Output should not change. If you execute your program with the name 'ifib' then it performs the iterative calculation. If it is executed via 'rfib' or 'fib' it performs the recursive method of calculating.
$ ./fib --target 3
fib(3) = 3
$ ./ifib --target 3
fib(3) = 3
$ ./rfib --target 3
fib(3) = 3
There is an example of how to accomplish this sort of test of the program name in “C Programming Language, 2nd Edition, by Brian W. Kernighan, Dennis M. Ritchie” textbook. You can search Google for an iterative version of Fibannaci, but be sure to cite where you found it in your README.
Note:
You may want to consider using GNU getopt library for processing command line options.