0% found this document useful (0 votes)
18 views7 pages

CSI Lab2

The document outlines Lab 2 of the CSI106 course, focusing on algorithms and their application in problem-solving. It includes objectives, materials, student tasks, and scoring criteria, along with examples of pseudocode and UML diagrams for various exercises. Students are required to write algorithms for specific problems, including calculating results based on user input and determining income tax based on given rules.

Uploaded by

dmixinhthee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

CSI Lab2

The document outlines Lab 2 of the CSI106 course, focusing on algorithms and their application in problem-solving. It includes objectives, materials, student tasks, and scoring criteria, along with examples of pseudocode and UML diagrams for various exercises. Students are required to write algorithms for specific problems, including calculating results based on user input and determining income tax based on given rules.

Uploaded by

dmixinhthee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CSI106: Foundations Of Computer Science

Duration: 90’

Lab 2: Algorithm
Objective:

 Define an algorithm and relate it to problem-solving


 Define three constructs and describe their use in algorithms.
 Describe pseudocode and how they are used in algorithms.
 Describe UML diagrams and how they are used in algorithms.

Materials:
Based on exercises of chapter 8 in the textbook “Foundations Of Computer Science, 4nd
Edition, Behrouz Forouzan, 2017.”
Student's task:
- Review the whole chapter 8 content in the textbook.
- Write down solutions to exercises (step by step)
- Finish exercises and submit the results to the lecturer in class.

Scoring scale: 10

Students will represent the algorithm of the problem in two ways: pseudo-code and UML

Sample: Write an algorithm to calculate the sum of two integers

Using pseudo-code:

Input: num1, num2


Processing: result=num1+num2
Output: print out result

Using UML

Input num1, num2

Result=num1+num2

Print out result


Exercise 1 (2 marks): Write an algorithm to allows users to input two integers and an
operator of four operators +, -, *, / then print out the result to the monitor.

Pseudocode:

Algorithm CalculateResult
Input:
num1: integer
num2: integer
operator: character (+, -, *, /)

Processing:
IF operator is '+' THEN
result = num1 + num2
ELSE IF operator is '-' THEN
result = num1 - num2
ELSE IF operator is '*' THEN
result = num1 * num2
ELSE IF operator is '/' THEN
IF num2 is NOT 0 THEN
result = num1 / num2
ELSE
Print "Error: Division by zero is not allowed."
Exit Algorithm (or handle error appropriately)
END IF
ELSE
Print "Error: Invalid operator."
Exit Algorithm (or handle error appropriately)
END IF

Output:
Print result

UML:
Exercise 2 (2 marks): Write an algorithm that will find the smallest integer among five
integers: 12, 34, 9, 24, 39

Pseudocode:

Algorithm FindSmallestInteger
Input:
numbers: list of integers [12, 34, 9, 24, 39]

Processing:
smallest = numbers[0] // Initialize smallest with the first number

FOR EACH number IN numbers (starting from the second number):


IF number < smallest THEN
smallest = number
END IF
END FOR

Output:
Print smallest

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

Pseudocode:

Algorithm SumUntilZero
Input: (Integers from keyboard)

Processing:
sum = 0
num = 1 // Initialize num to a non-zero value to enter the loop

WHILE num is NOT 0 DO


Input num
sum = sum + num
END WHILE

Output:
Print sum
Exercise 4 (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 tax


l
1 Less than or equal to 5%
5.000.000
2 From 5.000.001 to 10%
10.000.000
3 From 10.000.001 to 15%
18.000.000
4 Over 18.000.000 20%

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

Pseudocode:

Algorithm CalculateIncomeTax
Constants:
PA_MONTHLY = 9000000 // Personal pending amount per month
PD_MONTHLY = 3600000 // Dependent pending amount per month
MONTHS_IN_YEAR = 12

Input:
yearly_income: integer (Total yearly income)
num_dependents: integer (Number of dependents)

Processing:
// Calculate yearly tax-free income (tf)
tf = MONTHS_IN_YEAR * (PA_MONTHLY + (num_dependents * PD_MONTHLY))

// Calculate taxable income (ti)


ti = yearly_income - tf

// Initialize income_tax
income_tax = 0

// Determine income tax based on ti


IF ti <= 0 THEN
income_tax = 0
ELSE IF ti <= 5000000 THEN
income_tax = ti * 0.05
ELSE IF ti <= 10000000 THEN
income_tax = (5000000 * 0.05) + ((ti - 5000000) * 0.10)
ELSE IF ti <= 18000000 THEN
income_tax = (5000000 * 0.05) + (5000000 * 0.10) + ((ti - 10000000) * 0.15)
ELSE // ti > 18000000
income_tax = (5000000 * 0.05) + (5000000 * 0.10) + (8000000 * 0.15) + ((ti - 18000000) * 0.20)
END IF

Output:
Print "Total Yearly Income: " + yearly_income
Print "Taxable Income (ti): " + ti
Print "Calculated Income Tax: " + income_tax

You might also like