( ) INTRODUCTION TO C – EXERCISE 7

INTRODUCTION TO C – EXERCISE 7
HAND IN THE PRINTOUTS (CODE AND RESULTS): Dec. 17, 2014
(put them into the boxes provided after the lecture AND upload the C code
to your FTP account!)
DISCUSSION OF THE PROGRAMS: Jan. 14, 2015
Program 21 (4 points):
Write a program that reads a floating
point number from stdin to a double
variable, and prints its IEEE 754 stanEnter floating point number : 3.141592654
dard byte representation in hexadecimal
(see figure). Use an unsigned char
HEX representation: 40 09 21 FB 54 52 45 50
pointer to individually access all eight
bytes of the double variable; the byte with the highest address should be printed at leftmost position. Are
you able to identify sign bit, biased exponent, and mantissa fraction?
IEEE 754 DOUBLE PRECISION STANDARD
==================================
Hint: The “%02X” format of printf prints a byte as two hexadecimal digits.
Program 22 (5 points):
There is a magical point between the Earth and the Moon, called the L1 Lagrange point, at which a satellite
will orbit the Earth in perfect synchrony with the Moon, staying always in between the two. This works because the inward gravitational pull of the Earth and the outward pull of the Moon combine to create exactly
the needed centripetal force that keeps the satellite in its orbit. Hopefully you are able to understand why the
distance r from the centre of the Earth to the L1 point satisfies the equation (assuming circular orbits)
GM E
GM M
−
− ω 2r = 0
2
2
r
(R − r )
where
G = 6.674 × 10–11 m3 kg–1 s–-2
ME = 5.974 × 1024 kg
MM = 7.348 × 1022 kg
R = 3.844 × 108 m
ω = 2.662 × 10–6 s–1
gravitational constant
mass of the Earth
mass of the Moon
distance Earth-Moon
2π / orbital period of the Moon
The above equation is a fifth-order polynomial equation in r that cannot be solved exactly in closed form.
Write a program that calculates and prints its solution r using a binary search algorithm. First of all, we abbreviate the left hand side of the equation as f(r). Then choose two arbitrary values R > ra > 0 and R > rb > ra
that satisfy f(ra) > 0 and f(rb) < 0 ; this defines your initial search interval [ra , rb] (hint: the L1 point neither
lies within the Earth nor within the Moon). Calculate the interval midpoint rm = (ra + rb)/2 that splits the
original interval into two halves of equal widths. If by some miracle f(rm) = 0 you are ready, but it is much
more likely that the requested solution is located either in the left (if f(rm) < 0) or the right (if f(rm) > 0) subinterval, respectively. Now, set ra and rb to the boundaries of the correct sub-interval (the one that contains
the solution of the equation) and repeat the whole process until the width of the search interval becomes less
than 10 km. The midpoint of this final interval is your approximation to the L1 point distance.
Remark: There are five Lagrange points in each gravitational two-body system. All but L4 and L5 are unstable, so no “space debris” will be caught in the L1 point of the Earth-Moon system. A satellite
to be permanently placed there would need a propulsion system to correct the long-term drift due
to orbital perturbations.
.
Don’t miss all the fun on the next page!
Program 23 (6 points):
The Sieve of Eratosthenes is an efficient method to calculate all prime numbers up to N:
(1) Define a sufficiently long integer array
A and initialise it with A[k] = 1 for
k = 2,3,…,N. Set Index = 2.
SIEVE OF ERATOSTHENES ( N=1000 )
================================
2
31
73
127
179
233
283
353
419
467
547
607
661
739
811
877
947
3
37
79
131
181
239
293
359
421
479
557
613
673
743
821
881
953
5
41
83
137
191
241
307
367
431
487
563
617
677
751
823
883
967
7
43
89
139
193
251
311
373
433
491
569
619
683
757
827
887
971
11
47
97
149
197
257
313
379
439
499
571
631
691
761
829
907
977
13
53
101
151
199
263
317
383
443
503
577
641
701
769
839
911
983
17
59
103
157
211
269
331
389
449
509
587
643
709
773
853
919
991
19
61
107
163
223
271
337
397
457
521
593
647
719
787
857
929
997
23
67
109
167
227
277
347
401
461
523
599
653
727
797
859
937
29
71
113
173
229
281
349
409
463
541
601
659
733
809
863
941
(2) Set A[n⋅Index] = 0 for n = 2,3,4,… (as
long as n⋅Index ≤ N ).
(3) Increment Index until A[Index] ≠ 0 or
Index > N . If Index > N continue with (4), otherwise go back to
(2).
(4) The prime numbers are given by those
n = 2,3,…,N with A[n] ≠ 0.
Your program should print all prime numbers up to N = 1000 using this algorithm. Format the output to 10
numbers per line (cf. figure).
Hint: The standard library function double sqrt(double x) returns the square root of a double argument x.
You must include the header file math.h to get the correct function declaration.