CS13002 Programming and Data Structures | Section 3/C, Spring 2003--2004 |
Assignment 2 : Solution
Exercise 1
#include <stdio.h> #include <math.h> #define LIMIT 1000000 int main () { double sum = 0, logn; int n; for (n = 1; n <= LIMIT; n++) { sum += 1.0 / (double)(n); if (n % 10000 == 0) { logn = log(n); printf("n = %7d, H_n = %12.9lf, ln n = %12.9lf, H_n - ln n = %12.9lf\n", n, sum, logn, sum - logn); } } }Sample output
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 n = 110000, H_n = 12.185455855, ln n = 11.608235645, H_n - ln n = 0.577220210 n = 120000, H_n = 12.272466853, ln n = 11.695247022, H_n - ln n = 0.577219832 n = 130000, H_n = 12.352509240, ln n = 11.775289729, H_n - ln n = 0.577219511 n = 140000, H_n = 12.426616938, ln n = 11.849397702, H_n - ln n = 0.577219236 n = 150000, H_n = 12.495609571, ln n = 11.918390573, H_n - ln n = 0.577218998 n = 160000, H_n = 12.560147884, ln n = 11.982929094, H_n - ln n = 0.577218790 n = 170000, H_n = 12.620772322, ln n = 12.043553716, H_n - ln n = 0.577218606 n = 180000, H_n = 12.677930573, ln n = 12.100712130, H_n - ln n = 0.577218443 n = 190000, H_n = 12.731997648, ln n = 12.154779351, H_n - ln n = 0.577218296 n = 200000, H_n = 12.783290810, ln n = 12.206072646, H_n - ln n = 0.577218165 n = 210000, H_n = 12.832080856, ln n = 12.254862810, H_n - ln n = 0.577218046 n = 220000, H_n = 12.878600763, ln n = 12.301382825, H_n - ln n = 0.577217938 n = 230000, H_n = 12.923052427, ln n = 12.345834588, H_n - ln n = 0.577217839 n = 240000, H_n = 12.965611951, ln n = 12.388394202, H_n - ln n = 0.577217748 n = 250000, H_n = 13.006433862, ln n = 12.429216197, H_n - ln n = 0.577217665 n = 260000, H_n = 13.045654498, ln n = 12.468436910, H_n - ln n = 0.577217588 n = 270000, H_n = 13.083394755, ln n = 12.506177238, H_n - ln n = 0.577217517 n = 280000, H_n = 13.119762333, ln n = 12.542544882, H_n - ln n = 0.577217451 n = 290000, H_n = 13.154853591, ln n = 12.577636202, H_n - ln n = 0.577217389 n = 300000, H_n = 13.188755085, ln n = 12.611537754, H_n - ln n = 0.577217332 n = 310000, H_n = 13.221544854, ln n = 12.644327576, H_n - ln n = 0.577217278 n = 320000, H_n = 13.253293502, ln n = 12.676076275, H_n - ln n = 0.577217227 n = 330000, H_n = 13.284065113, ln n = 12.706847933, H_n - ln n = 0.577217180 n = 340000, H_n = 13.313918032, ln n = 12.736700897, H_n - ln n = 0.577217135 n = 350000, H_n = 13.342905527, ln n = 12.765688433, H_n - ln n = 0.577217093 n = 360000, H_n = 13.371076364, ln n = 12.793859310, H_n - ln n = 0.577217054 n = 370000, H_n = 13.398475301, ln n = 12.821258285, H_n - ln n = 0.577217016 n = 380000, H_n = 13.425143512, ln n = 12.847926532, H_n - ln n = 0.577216981 n = 390000, H_n = 13.451118965, ln n = 12.873902018, H_n - ln n = 0.577216947 n = 400000, H_n = 13.476436741, ln n = 12.899219826, H_n - ln n = 0.577216915 n = 410000, H_n = 13.501129323, ln n = 12.923912439, H_n - ln n = 0.577216884 n = 420000, H_n = 13.525226846, ln n = 12.948009990, H_n - ln n = 0.577216855 n = 430000, H_n = 13.548757315, ln n = 12.971540488, H_n - ln n = 0.577216828 n = 440000, H_n = 13.571746807, ln n = 12.994530006, H_n - ln n = 0.577216801 n = 450000, H_n = 13.594219638, ln n = 13.017002862, H_n - ln n = 0.577216776 n = 460000, H_n = 13.616198520, ln n = 13.038981768, H_n - ln n = 0.577216752 n = 470000, H_n = 13.637704702, ln n = 13.060487974, H_n - ln n = 0.577216729 n = 480000, H_n = 13.658758089, ln n = 13.081541383, H_n - ln n = 0.577216707 n = 490000, H_n = 13.679377355, ln n = 13.102160670, H_n - ln n = 0.577216685 n = 500000, H_n = 13.699580042, ln n = 13.122363377, H_n - ln n = 0.577216665 n = 510000, H_n = 13.719382650, ln n = 13.142166005, H_n - ln n = 0.577216645 n = 520000, H_n = 13.738800717, ln n = 13.161584091, H_n - ln n = 0.577216626 n = 530000, H_n = 13.757848894, ln n = 13.180632286, H_n - ln n = 0.577216608 n = 540000, H_n = 13.776541009, ln n = 13.199324419, H_n - ln n = 0.577216591 n = 550000, H_n = 13.794890131, ln n = 13.217673557, H_n - ln n = 0.577216574 n = 560000, H_n = 13.812908620, ln n = 13.235692063, H_n - ln n = 0.577216558 n = 570000, H_n = 13.830608182, ln n = 13.253391640, H_n - ln n = 0.577216542 n = 580000, H_n = 13.847999909, ln n = 13.270783383, H_n - ln n = 0.577216527 n = 590000, H_n = 13.865094328, ln n = 13.287877816, H_n - ln n = 0.577216512 n = 600000, H_n = 13.881901432, ln n = 13.304684934, H_n - ln n = 0.577216498 n = 610000, H_n = 13.898430721, ln n = 13.321214236, H_n - ln n = 0.577216485 n = 620000, H_n = 13.914691228, ln n = 13.337474757, H_n - ln n = 0.577216471 n = 630000, H_n = 13.930691557, ln n = 13.353475098, H_n - ln n = 0.577216459 n = 640000, H_n = 13.946439901, ln n = 13.369223455, H_n - ln n = 0.577216446 n = 650000, H_n = 13.961944076, ln n = 13.384727642, H_n - ln n = 0.577216434 n = 660000, H_n = 13.977211536, ln n = 13.399995114, H_n - ln n = 0.577216422 n = 670000, H_n = 13.992249403, ln n = 13.415032991, H_n - ln n = 0.577216411 n = 680000, H_n = 14.007064477, ln n = 13.429848077, H_n - ln n = 0.577216400 n = 690000, H_n = 14.021663266, ln n = 13.444446877, H_n - ln n = 0.577216390 n = 700000, H_n = 14.036051993, ln n = 13.458835614, H_n - ln n = 0.577216379 n = 710000, H_n = 14.050236618, ln n = 13.473020249, H_n - ln n = 0.577216369 n = 720000, H_n = 14.064222850, ln n = 13.487006491, H_n - ln n = 0.577216359 n = 730000, H_n = 14.078016163, ln n = 13.500799813, H_n - ln n = 0.577216350 n = 740000, H_n = 14.091621806, ln n = 13.514405465, H_n - ln n = 0.577216341 n = 750000, H_n = 14.105044817, ln n = 13.527828486, H_n - ln n = 0.577216332 n = 760000, H_n = 14.118290035, ln n = 13.541073712, H_n - ln n = 0.577216323 n = 770000, H_n = 14.131362108, ln n = 13.554145794, H_n - ln n = 0.577216314 n = 780000, H_n = 14.144265505, ln n = 13.567049199, H_n - ln n = 0.577216306 n = 790000, H_n = 14.157004522, ln n = 13.579788224, H_n - ln n = 0.577216298 n = 800000, H_n = 14.169583297, ln n = 13.592367007, H_n - ln n = 0.577216290 n = 810000, H_n = 14.182005809, ln n = 13.604789527, H_n - ln n = 0.577216282 n = 820000, H_n = 14.194275894, ln n = 13.617059619, H_n - ln n = 0.577216275 n = 830000, H_n = 14.206397247, ln n = 13.629180980, H_n - ln n = 0.577216267 n = 840000, H_n = 14.218373431, ln n = 13.641157171, H_n - ln n = 0.577216260 n = 850000, H_n = 14.230207882, ln n = 13.652991628, H_n - ln n = 0.577216253 n = 860000, H_n = 14.241903915, ln n = 13.664687668, H_n - ln n = 0.577216246 n = 870000, H_n = 14.253464730, ln n = 13.676248491, H_n - ln n = 0.577216240 n = 880000, H_n = 14.264893420, ln n = 13.687677186, H_n - ln n = 0.577216233 n = 890000, H_n = 14.276192968, ln n = 13.698976742, H_n - ln n = 0.577216227 n = 900000, H_n = 14.287366263, ln n = 13.710150042, H_n - ln n = 0.577216220 n = 910000, H_n = 14.298416093, ln n = 13.721199878, H_n - ln n = 0.577216214 n = 920000, H_n = 14.309345157, ln n = 13.732128949, H_n - ln n = 0.577216208 n = 930000, H_n = 14.320156068, ln n = 13.742939865, H_n - ln n = 0.577216203 n = 940000, H_n = 14.330851351, ln n = 13.753635154, H_n - ln n = 0.577216197 n = 950000, H_n = 14.341433455, ln n = 13.764217264, H_n - ln n = 0.577216191 n = 960000, H_n = 14.351904749, ln n = 13.774688563, H_n - ln n = 0.577216186 n = 970000, H_n = 14.362267531, ln n = 13.785051350, H_n - ln n = 0.577216180 n = 980000, H_n = 14.372524026, ln n = 13.795307851, H_n - ln n = 0.577216175 n = 990000, H_n = 14.382676392, ln n = 13.805460222, H_n - ln n = 0.577216170 n = 1000000, H_n = 14.392726723, ln n = 13.815510558, H_n - ln n = 0.577216165
Exercise 2
#include <stdio.h> #include <stdlib.h> int main () { int a, i, max, min; srand((unsigned int)time(NULL)); i = 1; while (1) { a = (rand() % 105) - 5; printf("Iteration %3d: new entry = %3d, ", i, a); if (a < 0) { printf("...quitting...\n\n\n"); exit(0); } if (i == 1) max = min = a; else { if (a > max) max = a; if (a < min) min = a; } printf("max = %3d, min = %3d\n", max, min); ++i; } }Sample output
Iteration 1: new entry = 81, max = 81, min = 81 Iteration 2: new entry = 89, max = 89, min = 81 Iteration 3: new entry = 45, max = 89, min = 45 Iteration 4: new entry = 2, max = 89, min = 2 Iteration 5: new entry = 18, max = 89, min = 2 Iteration 6: new entry = 4, max = 89, min = 2 Iteration 7: new entry = 2, max = 89, min = 2 Iteration 8: new entry = 77, max = 89, min = 2 Iteration 9: new entry = 82, max = 89, min = 2 Iteration 10: new entry = 47, max = 89, min = 2 Iteration 11: new entry = 80, max = 89, min = 2 Iteration 12: new entry = 63, max = 89, min = 2 Iteration 13: new entry = 1, max = 89, min = 1 Iteration 14: new entry = 53, max = 89, min = 1 Iteration 15: new entry = 3, max = 89, min = 1 Iteration 16: new entry = 64, max = 89, min = 1 Iteration 17: new entry = 59, max = 89, min = 1 Iteration 18: new entry = 38, max = 89, min = 1 Iteration 19: new entry = -2, ...quitting... Iteration 1: new entry = 64, max = 64, min = 64 Iteration 2: new entry = 82, max = 82, min = 64 Iteration 3: new entry = 98, max = 98, min = 64 Iteration 4: new entry = -1, ...quitting... Iteration 1: new entry = 0, max = 0, min = 0 Iteration 2: new entry = 48, max = 48, min = 0 Iteration 3: new entry = 48, max = 48, min = 0 Iteration 4: new entry = 3, max = 48, min = 0 Iteration 5: new entry = 43, max = 48, min = 0 Iteration 6: new entry = 60, max = 60, min = 0 Iteration 7: new entry = 29, max = 60, min = 0 Iteration 8: new entry = 79, max = 79, min = 0 Iteration 9: new entry = 82, max = 82, min = 0 Iteration 10: new entry = 55, max = 82, min = 0 Iteration 11: new entry = 59, max = 82, min = 0 Iteration 12: new entry = 49, max = 82, min = 0 Iteration 13: new entry = 75, max = 82, min = 0 Iteration 14: new entry = 33, max = 82, min = 0 Iteration 15: new entry = 82, max = 82, min = 0 Iteration 16: new entry = 53, max = 82, min = 0 Iteration 17: new entry = 91, max = 91, min = 0 Iteration 18: new entry = 93, max = 93, min = 0 Iteration 19: new entry = 56, max = 93, min = 0 Iteration 20: new entry = 94, max = 94, min = 0 Iteration 21: new entry = 1, max = 94, min = 0 Iteration 22: new entry = 32, max = 94, min = 0 Iteration 23: new entry = 83, max = 94, min = 0 Iteration 24: new entry = 63, max = 94, min = 0 Iteration 25: new entry = 79, max = 94, min = 0 Iteration 26: new entry = -2, ...quitting... Iteration 1: new entry = 55, max = 55, min = 55 Iteration 2: new entry = 5, max = 55, min = 5 Iteration 3: new entry = 14, max = 55, min = 5 Iteration 4: new entry = 74, max = 74, min = 5 Iteration 5: new entry = 73, max = 74, min = 5 Iteration 6: new entry = 79, max = 79, min = 5 Iteration 7: new entry = 41, max = 79, min = 5 Iteration 8: new entry = 79, max = 79, min = 5 Iteration 9: new entry = 51, max = 79, min = 5 Iteration 10: new entry = 14, max = 79, min = 5 Iteration 11: new entry = 90, max = 90, min = 5 Iteration 12: new entry = 37, max = 90, min = 5 Iteration 13: new entry = 83, max = 90, min = 5 Iteration 14: new entry = 71, max = 90, min = 5 Iteration 15: new entry = 84, max = 90, min = 5 Iteration 16: new entry = 32, max = 90, min = 5 Iteration 17: new entry = 16, max = 90, min = 5 Iteration 18: new entry = 89, max = 90, min = 5 Iteration 19: new entry = 41, max = 90, min = 5 Iteration 20: new entry = 38, max = 90, min = 5 Iteration 21: new entry = 48, max = 90, min = 5 Iteration 22: new entry = 9, max = 90, min = 5 Iteration 23: new entry = 31, max = 90, min = 5 Iteration 24: new entry = 96, max = 96, min = 5 Iteration 25: new entry = 66, max = 96, min = 5 Iteration 26: new entry = 17, max = 96, min = 5 Iteration 27: new entry = 17, max = 96, min = 5 Iteration 28: new entry = 53, max = 96, min = 5 Iteration 29: new entry = 53, max = 96, min = 5 Iteration 30: new entry = -2, ...quitting... Iteration 1: new entry = 14, max = 14, min = 14 Iteration 2: new entry = 19, max = 19, min = 14 Iteration 3: new entry = 74, max = 74, min = 14 Iteration 4: new entry = 8, max = 74, min = 8 Iteration 5: new entry = -1, ...quitting...