Test 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Vietnamese German University

Computer Science Program


12/03/2024

Practice Test for C Programming Evaluation


Course: Programming 2
Duration: ... minutes

You could use any materials or search for information on the Internet.

Submit your answer files (.c files and a .doc/.pdf file) to Google Classroom. The .doc/.pdf
file contains screenshots of your codes and their outputs.

You have to use given test samples when you execute your implemented programs.

You need to self-organize and submit on time (before 4:15 PM). Late submissions are getting penalty
points.

If your points are less than 50, you need to attend extra classes on Wednesday evening.

There is an extra class on Wednesday evening (13/03/2024) in the VGU library, and the TA will
solve this test.

Break time: From 2:45 pm - 3:00 pm

Question I)(Basic Skill; 20 points)

1. Implement the function as follows:


x
f (x) = ∗ (1 + x + 0.04 ∗ x3 )
2
Example 1: f (0.5) ≈ 0.376250
Example 2: f (1.5) ≈ 1.976250

2. Implement the function as follows:


Pn 0.5i
ln (0.5) ≈ −
i=1 i
Example 1: n = 10, result ≈ −0.693065
Example 2: n = 100 ≈ −0.693147

Question II)(Using 1D Array; 20 points)

1
1. Implement the function which receives min value, max value and an array, then modifying this
array to ensure that all elements in the array cannot be out of the range (min value ≤ element
≤ max value ). If an element is less than the min value, you should replace it with min value.
Do the same for max value.
Example 1:
input: min value = 3, max value=6, x = {1, 2, 3, 4, 5, 6, 7, 8, 9}
result: x = {3, 3, 3, 4, 5, 6, 6, 6, 6}
Example 2:
input: min value = 11, max value=33, x = {4, 12, 23, 4, 35, 16, 7, 48, 19}
result: x = {11, 12, 23, 11, 33, 16, 11, 33, 19}
2. Implement the function which receive input array, diff array, and sign array. Use two point-
ers, one for diff array and the other for sign array to modify them. Elements in diff array are
results of the difference between two consecutive elements in the input array (right element - left
element). Elements in sign array indicate that the difference is negative (< 0) = -1, or positive
(≥ 0) = 1.
Example 1:
input: input array = {1, 3, 7, 1, 2, 6, 0, 1}
result:
diff array = {2, 4, -6, 1, 4, -6, 1}
sign array = {1, 1, -1, 1, 1, -1, 1}
Example 2:
input: input array = {11, 23, 7, 10, 21, 16, 40, 17}
result:
diff array = {12, -16, 3, 11, -5, 24, -23}
sign array = {1, -1, 1, 1, -1, 1, -1}
Question
 III)(Using 2D array; 30 points) Given a matrix, represented by a 2D array,
x11 ... x1m
 ... 
X= 
 ... 
xn1 ... xnm
1. Write a function to compute the sum of odd elements in each row
Example 1:
 
1 4 7
X =  2 5 8  the result is an array that has a number of elements in the array equal to the
3 6 9
number of rows: res = {8, 5, 12}
Example 2:
 
1 2 3
4 5 6
X=  7 8 9  the result is an array that has a number of elements in the array equal to the

10 11 12
number of rows: res = {4, 5, 16, 11}

2
2. Write a function to compute the sum of squared diagonal (column index = row index) in the
matrix
Example 1:
 
1 4 7
X = 2 5 8  result = 12 + 52 + 92 = 107
3 6 9
Example 2:
 
1 2 3
4 5 6 2 2 2
X=  7 8 9  result = 1 + 5 + 9 = 107

10 11 12

Question
 IV)(Using 1D array; 30 point) Given a matrix, represented by a 1D array,
x11 ... x1m
 ... 
X= 
 ... 
xn1 ... xnm
1. Write a function to compute sum for even index in each row
Example 1:
 
1 4 7
X = 2 5 8 the result is an array that has a number of elements in the array equal to the
3 6 9
number of rows: res = {8, 10, 12}
Example 2:
 
1 2 3
4 5 6
X=  7 8 9  the result is an array that has a number of elements in the array equal to the

10 11 12
number of rows: res = {4, 10, 16, 22}
2. Write a function to compute sum of doubled squared diagonal (column index = row index) in
the matrix
Example 1:
 
1 4 7
X = 2 5 8  result = 2 ∗ 12 + 2 ∗ 52 + 2 ∗ 92 = 214
3 6 9
Example 2:
 
1 2 3
4 5 6 2 2 2
X=  7 8 9  result = 2 ∗ 1 + 2 ∗ 5 + 2 ∗ 9 = 214

10 11 12

———————————————————–END———————————————————–

You might also like