CS13002 Programming and Data Structures | Section 3/C, Spring 2003--2004 |
Assignment 2
Exercise 1
The harmonic numbers are defined for n>=1 as
Hn = (1/1) + (1/2) + (1/3) + ... + (1/n)It is known that as n tends to infinity, the sequence Hn converges to ln(n)+k, where ln is the natural logarithm and k is a constant known as Euler's constant. In this exercise you are asked to compute an approximate value for Euler's constant.
Generate the values of Hn and ln(n) successively for n=1,2,3,...,1000000, and compute the difference Hn - ln(n). Looking at the sequence of these differences, convince yourself that the mathematical theory just mentioned seems to be valid.
You should also convince us about the correctness of your program by showing the results for a small number of representative values of n, for example, for the 100 values n = 10000, 20000, 30000, ..., 1000000. Note that we will be VERY FURIOUS, if we see all the million iterations in your output (which require some twenty thousand pages for printing).
Sample output
Your output should be presented in the following fashion:n = 10000, H_n = 9.787606036, ln n = 9.210340372, H_n - ln n = 0.577265664 n = 20000, H_n = 10.480728217, ln n = 9.903487553, H_n - ln n = 0.577240665 n = 30000, H_n = 10.886184992, ln n = 10.308952661, H_n - ln n = 0.577232331 n = 40000, H_n = 11.173862898, ln n = 10.596634733, H_n - ln n = 0.577228165 n = 50000, H_n = 11.397003949, ln n = 10.819778284, H_n - ln n = 0.577225665 n = 60000, H_n = 11.579323839, ln n = 11.002099841, H_n - ln n = 0.577223998 n = 70000, H_n = 11.733473329, ln n = 11.156250521, H_n - ln n = 0.577222808 n = 80000, H_n = 11.867003829, ln n = 11.289781914, H_n - ln n = 0.577221915 n = 90000, H_n = 11.984786170, ln n = 11.407564949, H_n - ln n = 0.577221220 n = 100000, H_n = 12.090146130, ln n = 11.512925465, H_n - ln n = 0.577220665 ...
Exercise 2
Randomly generate a sequence of integers between -5 and +99 and output the maximum and minimum values generated so far. Exit, if a negative integer is generated. You must not store the sequence generated (say using an array), but update the maximum and minimum values on the fly, as soon as a new entry is generated.
Report the output of your program for about five runs with different seeds. At least one of these sequences should be as big as 20+ iterations.
Click here to know how to generate random numbers.
Sample output
Iteration 1: new entry = 84, max = 84, min = 84 Iteration 2: new entry = 87, max = 87, min = 84 Iteration 3: new entry = 72, max = 87, min = 72 Iteration 4: new entry = 53, max = 87, min = 53 Iteration 5: new entry = 93, max = 93, min = 53 Iteration 6: new entry = 23, max = 93, min = 23 Iteration 7: new entry = 88, max = 93, min = 23 Iteration 8: new entry = 52, max = 93, min = 23 Iteration 9: new entry = 17, max = 93, min = 17 Iteration 10: new entry = 46, max = 93, min = 17 Iteration 11: new entry = 61, max = 93, min = 17 Iteration 12: new entry = 16, max = 93, min = 16 Iteration 13: new entry = 70, max = 93, min = 16 Iteration 14: new entry = 37, max = 93, min = 16 Iteration 15: new entry = 19, max = 93, min = 16 Iteration 16: new entry = 33, max = 93, min = 16 Iteration 17: new entry = 60, max = 93, min = 16 Iteration 18: new entry = 36, max = 93, min = 16 Iteration 19: new entry = 84, max = 93, min = 16 Iteration 20: new entry = 70, max = 93, min = 16 Iteration 21: new entry = 40, max = 93, min = 16 Iteration 22: new entry = 28, max = 93, min = 16 Iteration 23: new entry = 36, max = 93, min = 16 Iteration 24: new entry = 96, max = 96, min = 16 Iteration 25: new entry = 54, max = 96, min = 16 Iteration 26: new entry = 76, max = 96, min = 16 Iteration 27: new entry = 17, max = 96, min = 16 Iteration 28: new entry = -4, ...quitting...