COSC2500/COSC7500—Semester 2, 2022 Exercises—C programming
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
COSC2500/COSC7500—Semester 2, 2022
Exercises—C programming
Problem C.1 (40 points in total, 10 points per subproblem)
Fibonacci numbers, is also named as Fibonacci sequence, in which each number is the sum of the two preceding ones as following:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
The task is to implement two functions in C
a) using recursive function style,
F(0) = 1 and F(1) = 1
b) using a For Loop style.
c) calling two functions in main functions and display the items of one of the Fibonacci sequences from n=2 to 70.
d) Plot the curves (from n=1 to 14) to compare with F(n) = n2 . The Figure 1 is a plot demo from n=0 to 12.
Figure 1. An example of output the plot demo
Problem C.2 (40 points in total, 10 points per subproblem)
Finding a median number from a unsort array in C language. For example, a sequence is { 12, 10, 15, 3, 23 }, the median will be 12 because it is the middle element of sorted sequence {3, 10, 12, 15, 23}.
a) The elements of the array should be input form console. The first input is size of the array and then following the numbers. For example, input is 4 12 10 15 3. Then the array has 4 elements {12, 10, 15, 3}.
b) Write a function to find the median numbers using sort algorithms. The sort algorithm can be search from any textbooks and web
resources, such as
1. Selection Sort (With Code in Python/C++/Java/C) (programiz.com)
2. C Program: Insertion sort algorithm - w3resource
c) Write a function to find the median number without sorted the sequence (such as finding k largest numbers or other methods).
d) Test the above two functions in a main function to verify that both functions are correct.
Problem C.3 (20 points in total, 10 points per subproblem)
Entropy is a popular concept in physics and chemistry, it is also used to
detect signal randomness. In mathematics, an entropy of an input integer
sequence of X can be defined by (1)
E(X) = ∑j(n)=1 p(Xj ) ln( p(Xj ))
(1)
while p(x) is the value of Xj distribution of X. Assume X=(4, 2, 3, 5, 3, 4,
5), then p(X) = (0,0, , , , )
a) Writing a C function to implement equation (1) and a main function to test it.
b) Writing a C function to implement a joint entropy of X and Y
E(X, Y) = ∑j(n)=1 p(Xj, Yj ) ln( p(Xj, Yj )),
(2)
Please use C struct to organise the input data: X and Y and write a main function to test the function.
2022-09-27