Tải bản đầy đủ (.pdf) (8 trang)

Supplementary tasks repetition statements

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (441.94 KB, 8 trang )

Chapter 5: Repetition Statements
A. Review
A.1. What do we mean “repetition” for repetition statements? Introduce at least three use
contexts of repetition statements?
A.2. What are differences between while.. statements and do..while.. statements? Give an
example to illustrate the differences between them.
A.3. Compare while.. statements and for.. statements? Give an example to illustrate their
dissimilarities and similarities.
A.4. Compare selection statements and repetition statements to clarify when selection
statements should be used and when repetition statements should be used. Give an example to
illustrate their use context.
B. Practice
B.1. Write repetition statements that might be used in the solutions of the problems B.1.1 to
B.1.15 in Chapter 1. Remember to write variable declaration statements before the repetition
statements if there is any variable in these repetition statements.
B.2. Identify all the errors in each program. For those errors, make appropriate changes so that
they become correct with no infinite loops. After all, describe the output of each program.
B.2.1.

B.2.2.

1


B.2.3.

B.2.4.

B.2.5.

B.2.6.



B.2.7.

2


B.2.8.

B.2.9.

B.2.10.
3


B.3. Rewrite the following programs using while.. statements instead of for.. statements.
Describe what will be on screen if they are executed.
B.3.1.

B.3.2.

B.3.3.

B.3.4.

B.3.5.

4


B.3.6.


B.3.7.

B.3.8.

5


B.4. Write a program to ask a user to input a sequence of n numbers. Calculate their total linear
sum, sum of squares, and their average. Display the results on screen with 2 decimal places.
B.5. Write a program to compute a series of n Fibonacci numbers, n is a positive number input
by a user. Print the series with at most 3 numbers on one single line on screen.
F(0)=F(1) = 1
F(n) = F(n-1) + F(n-2) for n>=2
B.6. Write a program to ask a user to input a positive integer number in [1, 1000000] until a
valid number is received. Examine the input number and return its information as follows:
B.6.1. How many digits are in the input number.
B.6.2. What is the biggest digit in the input number.
B.6.3. What is the smallest digit in the input number.
B.6.4. Determine if the input number is a symmetric one. Several symmetric numbers are given
as: 1, 11, 22, 121, 2332, 34143, 235532, …
B.6.5. List all the prime numbers smaller than the input number with at most 10 prime numbers
on one single line on screen. How many of them?
B.7. Given a matrix of positive integer numbers: NxN where N is input by a user. The diagonal
line contains N numbers each of which is N. All the parallel lines above the diagonal line contain
(N-1), (N-2), .., and 1 numbers each of which is N+1, N+2, …, and 2N-1, respectively. All the
6


parallel lines below the diagonal line contain (N-1), (N-2), .., and 1 numbers each of which is N1, N-2, …, and 1, respectively. Display such a matrix on screen. Any use of arrays is not allowed.

Matrix with N=6:
N
N-1
N-2
N-3
N-4
N-5

N+1
N
N-1
N-2
N-3
N-4

N+2
N+1
N
N-1
N-2
N-3

N+3
N+2
N+1
N
N-1
N-2

N+4

N+3
N+2
N+1
N
N-1

N+5
N+4
N+3
N+2
N+1
N

Matrix (6x6) on screen:
6
5
4
3
2
1

7
6
5
4
3
2

8
7

6
5
4
3

9
8
7
6
5
4

10
9
8
7
6
5

11
10
9
8
7
6

B.8. Given a following menu about calculation of two positive integer numbers:
What do you want with your numbers?
1. x + y
2. x – y

3. x * y
4. x / y
5. x^y
6. x & y
7. x | y
8. exit
Write a program to display the aforementioned menu. After that ask a user to input his/her
numbers along with the operator preferred. If a user would like to close the program, guide
7


them to input “-1 $ -1” to end the execution of the program. Perform the calculation he/she
enters and print the result “x operator y = …” on screen and then ask him/her if he/she want to
continue or close the program and so on.
B.9. Write a program to compute an approximate value of each following function with N and x
input by a user. Range of x is given as: R=1, i.e. x in [0, 1], R=, i.e. x in R. If N is given 4 then n is
from 0 to 4. Display the result with N and x on screen.

B.10. Check your coding style with the programs you have written. Should anything be changed
with your programs so that they can be not only executable but also readable? Rewrite your
programs if yes.

8



×