You are on page 1of 3

Vietnamese - German University

Electrical and Computer Engineering


OBJECT-ORIENTED PROGRAMMING - LAB 1

1. std::cout << is used to print a result to the console. std::cin >>, on the other hand, is
used to read a input parameter in from the keyboard. Write code that asks a user to enter
two integers from the keyboard and then writes the product of these integers to the screen.

2. Swap two given inputted numbers a and b, e.g. a = 4 and b = 5.

3. Sum of
N
X
S= k2 , (1)
k=1

where N is an inputted number.

4. Sum of
1 1 1 1
S =1+ + + + + ... (2)
2 4 8 16
for all elements greater than an inputted tolerance, e.g. 4e − 3.

5. Use the following code to complete the tasks. The following cases are independent from one
another. If a variable is not specified, it is inputted from users by std::cin >>.
1 # include < iostream >
2 int main ()
3 {
4 double p , q , x , y ;
5 int j ;
6 return 0 ;
7 }

(a) Set the variable x to the value 5 if either p is greater than or equal to q, or the variable
j is not equal to 10.
(b) Set the variable x to the value 5 if both y is greater than or equal to q, and the variable
j is equal to 20. If this compound condition is not met, set x to take the same value as
p.
(c) Set the variable x according to the following rule.

 0, p > q,

x= 1, p ≤ q, and j = 10, .

2, otherwise

6. What is the output result of each of the following cases? Are they correct? If not, investigate
what is wrong.

(a) Use the <cmath> library, and take x = 2.


p
cos(πx/2) + sin3 (πx/3). (3)
ECE OOP - Lab 1 Page 2 of 3

(b) What is the output when the following code is run?


1 # include < iostream >
2 using namespace std ;
3
4 int main ()
5 {
6 int i = 1 ;
7 int j = 2 ;
8 double result = 0 ;
9 result = i / j + 5 ;
10 cout << " result ␣ = ␣ " << result << endl ;
11 }

(c) What is wrong with the following while loop?


1 # include < iostream >
2 using namespace std ;
3
4 int main ()
5 {
6 double p o s i t i v e_ n u m b e r s [4] = {1.0 , 5.65 , 42.0 , 0.01} ;
7 double max = 0.0 ;
8 int count = 0 ;
9 while ( count < 4)
10 if ( p o s i t i v e _ n u m b e r s [ count ] > max )
11 max = p o s i t i v e _ n u m b e r s [ count ] ;
12 return 0 ;
13 }

7. π can be calculated by the following infinite series



X 1 π4
= . (4)
k4 90
k=1

Write a code to approximate the π number with 5 terms, 10 terms, and 100 terms. Check how
much the accuracy is improved. Try to implement your code using two different approaches:
(i) Use the for loop; (ii) Use the while loop.

8. Numerical integration: the trapezoidal rule

Zxm  
1 1
f (x)dx ≈ h f0 + f1 + . . . + fm−1 + fm , (5)
2 2
x0

where h = (xm − x)/m.


Write code to apply the trapezoidal rule with m = 20 to approximate
Z π
sin(x)dx, (6)
0

and compare your obtained estimate with the exact solution. Try with different values of m.

Page 2
ECE OOP - Lab 1 Page 3 of 3

9. The Newton’s method for approximating nonlinear equations f (x) = 0:

f (xi−1 )
xi = xi−1 − , x = 1, 2, 3, . . . , (7)
f ′ (xi−1 )

where the iteration is terminated if the approximation sufficiently approaches to the fixed
point solution by the estimate

|xi − xi−1 | < ε, (8)

with ε is called the tolerance.


Write a code using a while to approximate equation

f (x) = ex + x3 − 5 = 0 (9)

with ε = 1.0E − 5 and x0 = 0 as an initial guess. Print out the convergent solution at each
iteration.
Hint:

ˆ You don’t need to store all xi for each iteration. Just use xn and xp for xi and xi−1 .
ˆ Use assert to check if the denominator in Eq. (7) is zero or not.

Page 3

You might also like