Starting from:

$30

HW3 Shell Programming

CS507 Computing Foundation for Computational Science HW3
Shell Programming

Instruction to electronic submission: http://cs.boisestate.edu/∼cs221/SubmissionProcedure.html
• The written assignment can be done in a pure text format (*.txt, for example, problem1.txt) on
Onyx.
• The programming assignment should be presented with source codes.
• Each problem should have its own working directory, such as HW2/prob1, HW2/prob2 ... For
example, the following table shows the structure of HW1 from the user “student1” and how to
submit HW to us through Onyx
1 [ student1@onyx :HW1] $ l s −l
2 drwxr−x−−−. 2 s t u d e n t 1 S tuden t s 13 Sep 19 2021 prob1
3 drwxr−xr−x . 3 s t u d e n t 1 S tuden t s 14 Sep 19 2021 prob2
4 [ student1@onyx :HW1] $ cd prob1 /
5 [ minlong@onyx : prob1 ] $ l s
6 −rw−r−−−−−. 1 s t u d e n t 1 S tuden t s 943 Sep 19 2021 problem1 . t x t
7 $ cd . .
8 [ student1@onyx :HW1] $ pwd
9 /home/ s t u d e n t 1 /CS507/HW1
10 [ student1@onyx :HW1] $ submit minlong CS507 HW1
Listing 1: A sample structure of homework and submission procedure.
• Your source codes (if any) must compile and run on Onyx.
• Documentation is important and proper comments are expected in your source code.
– comments giving description of: purpose, parameters, and return value if applicable
– other comments where clarification of source code is needed
– proper and consistent indentation
– proper structure and modularity
Don’t ask us or your classmates directly for solutions (it happened); just try as much as possible.
Be patient and enjoy coding!
1
Programming Problems
1. (5 pts) Order check. Compose a program that accepts three floats x, y, and z as command-line
arguments and writes True if the values are strictly ascending or descending ( x < y < z or
x > y > z ), and False otherwise.
2. (5 pts) Grade circle Compose a program that accepts four floats as command-line arguments—x1, y1, x2, and y2—(the latitude and longitude, in degrees, of two points on the earth)
and writes the great-circle distance between them. The great-circle distance d (in nautical miles)
is given by the following equation:
d = 60 arccos[sin(x1) sin(x2) + cos(x1) cos(x2) cos(y1 − y2)]
Use your program to compute the great-circle distance between Paris (48.87° N and -2.33° W)
and San Francisco (37.8° N and 122.4° W).
Hint:
• You need to import math module into your code for mathematical functions like math.sin(),
math.cos(), math.acos().
• The equation uses degrees, whereas Python’s trigonometric functions use radians. Use
math.radians() and math.degrees() to convert between the two.
• Read references on the math package at: https://docs.python.org/3/library/math.html
3. (10 pts) Color conversion. Several different formats are used to represent color. For example,
the primary format for LCD displays, digital cameras, and web pages, known as the RGB format,
specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255. The
primary format for publishing books and magazines, known as the CMYK format, specifies the
level of cyan (C), magenta (M), yellow (Y), and black (K) on a real scale from 0.0 to 1.0. Compose
a program that converts RGB to CMYK. Accept three integers—r, g, and b—from the command
line and write the equivalent CMYK values. If the RGB values are all 0, then the CMY values
are all 0 and the K value is 1; otherwise, use these formulas:
w = max(r/255, g/255, b/255)
c = (w − (r/255))/w
m = (w − (g/255))/w
y = (w − (b/255))/w
k = 1 − w
4. (10pts) Trigonometric functions. Compose 2 programs that compute the sine and cosine
functions using their Taylor series expansions
sin x =
X∞
n=0
(−1)n
(2n + 1)!x
2n+1 = x −
x
3
3! +
x
5
5! − ...
cos x =
X∞
n=0
(−1)n
(2n)! x
2n = 1 −
x
2
2! +
x
4
4! − ...
Hint:
2
• Taylor’s series of a function is an infinite sum of terms that are expressed in terms of the
function’s derivatives at a single point.
• In numerical calculation, we certainly can’t compute infinite sum of terms but has to stop
when the new term to be added becomes really small, say term ≤ 1e − 15. You can use it
for your boolean expression. You can see “EPSILON” in our example of Newton’s method
as a reference.
5. (10pts) Guess game in Python. Let’s redo our previous problem using Python. Use import
random in your code to generate a random number in a range [0, 50]. The code should be able to
randomly set up a number as an answer and read input from the command-line argument. When
the user’s guess number doesn’t match the answer, it will prompt the user to input another guess
till they match.
Hint:
• You can check the definition of the following functions to generate a random integer: random.randrange(), random.randint() at https://docs.python.org/3/library/random.html
1 $ python numbergame . py
2 The magic number i s between 0 and 5 0.
3 Make your g u e s s : 2 8
4 28 i s t o o low
5 The magic number i s between 29 and 5 0.
6 Make your g u e s s : 3 0
7 30 i s t o o low
8 The magic number i s between 31 and 5 0.
9 Make your g u e s s : 5 0
10 50 i s t o o hi gh
11 The magic number i s between 31 and 5 0.
12 Make your g u e s s : 4 0
13 40 i s t o o low
14 The magic number i s between 41 and 5 0.
15 Make your g u e s s : 4 5
16 45 i s t o o low
17 The magic number i s between 46 and 5 0.
18 Make your g u e s s : 4 8
19 48 i s t o o hi gh
20 The magic number i s between 46 and 4 7.
21 Make your g u e s s : 4 7
22 47 i s t o o hi gh
23 The magic number i s between 46 and 4 7.
24 Make your g u e s s : 4 6
25 You g o t i t i n 8 g u e s s e s !
Listing 2: Sample output.
Hint: You may use Python’s built-in input() function as shown in class. Or you can take a look
at the examples here: https://www.geeksforgeeks.org/python-input-function/
6. (10 pts) Rydberg formula. In 1888 Johannes Rydberg published his famous formula for the
wavelengths λ for the emission lines of the hydrogen atom:
1
λ

1
m2

1
n2

,
where R is the Rydberg constant R = 1.097 × 10−2 nm−1 and m and n are positive integers. For
a given value of m, the wavelengths λ given by this formula for all n > m forms a series. The
3
firms 3 such series, from m=1,2, and 3 are known as the Lyman, Balmer and Paschen series after
their respective discoverers. Write a Python program that prints out the wavelengths of the first
five lines in each of these three series.
1 $ python Rydberg . py
2 S e r i e s f o r m= 1
3 1 2 1. 5 4 3 6 0 3 7 6 7 8 5 1 7 nm
4 1 0 2. 5 5 2 4 1 5 6 7 9 1 2 4 8 8 nm
5 9 7. 2 3 4 8 8 3 0 1 4 2 8 1 3 7 nm
6 9 4. 9 5 5 9 4 0 4 4 3 6 3 4 1 5 nm
7 9 3. 7 6 2 2 0 8 6 2 0 9 1 4 1 8 nm
8 S e r i e s f o r m= 2
9 6 5 6. 3 3 5 4 6 0 3 4 6 3 9 9 3 nm
10 4 8 6. 1 7 4 4 1 5 0 7 1 4 0 6 8 nm
11 4 3 4. 0 8 4 2 9 9 1 7 0 8 9 9 nm
12 4 1 0. 2 0 9 6 6 2 7 1 6 4 9 9 5 nm
13 3 9 7. 0 4 2 4 3 8 9 7 4 9 8 2 2 5 nm
14 S e r i e s f o r m= 3
15 1 8 7 5. 2 4 4 1 7 2 4 1 8 2 8 3 6 nm
16 1 2 8 1. 9 0 5 1 9 5 9 8 9 0 6 1 2 nm
17 1 0 9 3. 8 9 2 4 3 3 9 1 0 6 6 5 4 nm
18 1 0 0 5. 0 1 3 6 7 3 6 5 5 4 2 4 nm
19 9 5 4. 6 6 9 7 6 0 5 0 3 8 5 3 6 nm
Listing 3: Sample output.
4

More products