Professional Documents
Culture Documents
Algorithm Exercises
Algorithm Exercises
Algorithm Exercises
1 (2 marks): Write an algorithm that will print out the sum of integers inputted from
the keyboard until the value 0 is inputted.
5. If input is not equal to zero. Add the input value to sum and return to step 3.
6. If input is equal to zero, the loop will stop and the computer will print out sum.
Exercise 2 (4 marks):
Suppose that:
In Viet Nam, each people has to pay for his/her yearly personal income tax as the following
description:
Rules:
Tax-free income:
Personal pending amount (tiền nuôi bản thân) pa= 9,000,000 d /month
Alimony (tiền cấp dưỡng) for each his/her dependent pd= 3,600,000 d/month/dependent
ti = income – tf
Based on taxable income, the employee has to pay his/her income tax with levels pre-
defined in the following table:
Write an algorithm that will calculate and print out : income, ti (Taxable Income) and income
tax.
9. If ti is less than or equal to zero then go to step 10. If ti is greater than zero, go to step 11.
Exercise 3 (4 marks):
Describe the steps for performing the algorithm Selection Sort for the following
sequence of numbers :
2. Element 3 is the smallest element and will be swapped with the first element of
the array.
3 4 9 12 7 15 8 14 10 6
3 4 9 12 7 15 8 14 10 6
4. Scan through the array that is not freezed and determine the smallest element.
5. Element 4 is already the smallest element so its position will be the same.
3 4 9 12 7 15 8 14 10 6
7. Scan through the array that is not freezed and determine the smallest element.
3 4 6 12 7 15 8 14 10 9
3 4 6 12 7 15 8 14 10 9
10. Scan through the array that is not freezed and determine the smallest element.
11. Element 7 is the smallest element so it will be swapped to the first element of
the array that is not freezed.
3 4 6 7 12 15 8 14 10 9
3 4 6 7 12 15 8 14 10 9
13. Scan through the array that is not freezed and determine the smallest element.
14. Element 8 is the smallest element so it will be swapped to the first element of
the array that is not freezed.
3 4 6 7 8 1 12 14 10 9
5
16. Scan through the array that is not freezed and determine the smallest element
17. Element 9 is the smallest element so it will be swapped to the first element of
the array that is not freezed.
3 4 6 7 8 9 12 14 10 15
3 4 6 7 8 9 12 14 10 15
19. Scan through the array that is not freezed and determine the smallest element
20. Element 10 is the smallest element so it will be swapped to the first element of
the array that is not freezed.
3 4 6 7 8 9 10 14 12 15
3 4 6 7 8 9 10 14 12 15
22. Scan through the array that is not freezed and determine the smallest element
23. Element 12 is the smallest element so it will be swapped to the first element of
the array that is not freezed.
3 4 6 7 8 9 10 12 14 15
24. Freeze the swapped element.
3 4 6 7 8 9 10 12 14 15
25. Scan through the array that is not freezed and determine the smallest element
26. Element 14 is the smallest element so its position will be the same.
3 4 6 7 8 9 10 12 14 15
3 4 6 7 8 9 10 12 14 15
28. Since 15 is the last element so it don’t need to be swapped so the last array is
the sorted list.
Sorted list:
3 4 6 7 8 9 10 12 14 15