Algorithm Exercises

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Exercise 

1 (2 marks): Write an algorithm that will print out the sum of integers inputted from
the keyboard until the value 0 is inputted.

1.      Start

2.      Initialize sum to zero

3.      Accept input integer

4.      Check if input is zero.

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

   With n dependents, Yearly tax-free income: tf = 12*(pa + n*pd)

Taxable income (thu nhập chịu thuế)

      ti = income – tf

 ( If ti<=0 then income tax = 0)

Based on taxable income, the employee has to pay his/her income tax with levels pre-
defined in the following table:
 

Leve Taxable Income Income


l tax

1 Less than or equal to 5.000.000 5%

2 From 5.000.001 to 10.000.000 10%

3 From 10.000.001 to 18.000.000 15%

4 Over 18.000.000 20%

 
Write an algorithm that will calculate and print out : income, ti (Taxable Income) and income
tax.

1.      Start

2.      Accept user’s income (income)

3.      Accept user’s number of dependents (n)

4.      Set a variable named pa equal to 9,000,000

5.      Set a variable named pd equal to 3,600,000

6.      Calculate tax-free income by the following equation: tf = 12*(pa+n*pd)

7.      Calculate taxable income by the following equation: ti = income – tf

8.      Check if ti is smaller or equal to zero.

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.

10.  Print out income = income, taxable-income = ti, income tax = 0


11.  If ti <= 5,000,000, go to step 12. If  5,000,001 <= ti <= 10,000,000, go to step 13. If
10,000,001 <= ti <= 18,000,000, go to step 14. If ti > 18,000,000, go to step 15.

12.  Print out: income, ti, income tax = ti*5%

13.  Print out: income, ti, income tax = ti*10%

14.  Print out: income, ti, income tax = ti*15%

15.  Print out: income, ti, income tax = ti*20%

Exercise  3 (4 marks):
Describe the steps for performing the algorithm Selection Sort for the following
sequence of numbers  :

5, 8, 9, 3, 7, 2, 6, 12, 18, 4 ( Mỗi sinh viên một dãy số).

My list:  6, 4, 9, 12, 7, 15, 8, 14, 10, 3

1.     Scan through the array and determine the smallest element.

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.     Freeze the first element.

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

6.     Freeze the second element


3 4 9 12 7 15 8 14 10 6

7.     Scan through the array that is not freezed and determine the smallest element.

8.     Element 6 is the smallest element so it will be swapped to the first element of


the array that is not freezed.

3 4 6 12 7 15 8 14 10 9

9.     Freeze the swapped element.

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

12. Freeze the swapped element.

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

15.  Freeze the swapped element.


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

18.  Freeze the swapped element.

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

21. Freeze the swapped element.

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

27. Freeze the swapped element.

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

You might also like