HW 02
HW 02
2
National Taiwan Normal University
CSIE Computer Programming I
Instructor: Po-Wen Chi
Due Date: 2023.10.17 PM 11:59
Policies:
• Zero tolerance for late submission.
• Please pack all your submissions in one zip file. RAR is not al-
lowed!!
• For convenience, your executable programs must be named following
the rule hwXXYY, where the red part is the homework number and
the blue part is the problem number. For example, hw0102 is the
executable program for homework #1 problem 2.
• I only accept PDF or TEXT. MS Word is not allowed.
• Do not forget your Makefile. For convenience, each assignment needs
only one Makefile.
• Please provide a README file. The README file should have at
least the following information:
– Your student ID and your name.
– How to build your code.
– How to execute your built programs.
– Anything that you want to notify our TAs.
• DO NOT BE A COPYCAT!! You will get ZERO if you are
caught.
√
2.1 2 (20 pts)
√
The square root of 2 ( 2) is a positive real number that, when multiplied
by itself, equals the number 2. It was probably √the first number known
to be irrational. So how to derive the value of 2? I will show you an
approach called Continued fraction.
√ 1
2=1+
1
2+
1
2+
.
2 + ..
The sequence will be 1, 23 , 75 , 17
12
, . . .. In mathematics, this sequence will
finally converge to a constant. Is this√true in programming? Please develop
a program to calculate the value of 2 from 1 to n. For your simplicity, I
promise that n is a 16-bits unsigned integer. You should also calculate the
difference between the value and 1.414213562373095048801 .
1 $ ./ hw0201
2 Please enter n (16 - bits unsigned ): 2
3 n = 1: 1.00000000000000000000 ( -0.41421356237309504880)
4 n = 2: 1.50000000000000000000 (0.085786438............) // 我 懶
的算了
Note that you should use double instead of float. The precision should
be 20.
2-2
30 Please enter the second number : 12
31 1 2 3 4
32 *) 1 2
33 -----------
34 2 4 6 8
35 1 2 3 4
36 -----------
37 1 4 8 0 8
If there is any invalid input, print an error message and terminate your
program. For your simplicity, I promise that all inputs are integers and can
be put in a 32-bits memory space.
2-3
input. You need to show all possible states with the given inputs. For your
convenience, all inputs are promised to be 32-bits integers.
1 $ ./ hw0203
2 Please enter an integer : 13
3 Please enter an integer : 9
4 Please enter an integer : 0
5 Possible States : S2 , S3
Note that the input is annual rate but you should use monthly rate
for compound interests.
Last semester, NingQung was frustrated with his final project, Catan.
Catan is a famous board game. If you don’t know what it is, you can learn
how to play on this website. But in this problem, you don’t have to know
how to play.
In Catan, the map is built with a hexagon, which is like Figure 2.2.
2-4
NingQung is a Frontend Engineer in his team, and he had no idea about
how to generate the map. TA Howard was thinking for several hours, and
he realized that last year, ex-TA SnowEx built a Cuboid Super Infinity
Exporter, and then Howard came up with a solution!
The user will input N and L, N is the number of layers, and L is the
length of an island. Figure 2.3 shows the above description.
You should print the road with ’/’, ’\’, or ’-’, and print the intersection
with ’*’. The number of points are same for each edge. You can see some
examples in the end.
Otherwise, you need to align the map to the top-left corner. Figure 2.4
shows the correct and wrong examples.
Some cases can’t generate the map:
1. N is less than 1.
2. L is less than 3.
3. The width of the map is larger than 80.
I promise that all inputs can fit in int32_t. You should handle these error
cases.
I know this is a tough challenge. As a Super Friendly TA, I will use the
2-5
Figure 2.3: What is N and L?
Grading Criteria
Error case x pts(x >0)
N=1 y pts(y >x)
L=3 z pts(z >= y)
Others 20 - x - y - z pts
2-6
10 / \ / \
11 * *-* *
12 \ / \ /
13 *-* *-*
14 \ /
15 *-*
Example 2:
1 Please input the length : 3
2 Please input the number of layer : 3
3 *-*
4 / \
5 *-* *-*
6 / \ / \
7 *-* *-* *-*
8 / \ / \ / \
9 * *-* *-* *
10 \ / \ / \ /
11 *-* *-* *-*
12 / \ / \ / \
13 * *-* *-* *
14 \ / \ / \ /
15 *-* *-* *-*
16 / \ / \ / \
17 * *-* *-* *
18 \ / \ / \ /
19 *-* *-* *-*
20 \ / \ /
21 *-* *-*
22 \ /
23 *-*
Example 3:
1 Please input the length : 5
2 Please input the number of layer : 1
3 *---*
4 / \
5 / \
6 / \
7 * *
8 \ /
9 \ /
10 \ /
11 *---*
2-7
2.6 Bonus: What Happens?? (5 pts)
Please read the following code and guess the output. Compile this code
and run it. Is the output the same with what you guess? Please explain
the reason of the output in Chinese.
1 # include <stdint .h>
2 # include <stdio .h>
3
4 uint32_t ui = 0;
5 uint16_t us = 0;
6 int32_t si = -1;
7
8 int main ()
9 {
10 int64_t r1 = ui + si;
11 int64_t r2 = us + si;
12 printf ("% ld %ld\n", r1 , r2);
13 }
Hint: You can look up the reason from C11. I have uploaded C11
draft on my website. The keyword: Integer Conversion and Integer
Promotion.
2-8