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

Adama Science & Technology University

School of Electrical Enginering & Computing


Computer Science & Engineering Program

Worksheet 1(Algorithms)
Objectives
 To understand the concept of algorithm
 To understand basic steps of algorithm design
 To work with the basic algorithm constructs using pseudocode and flowchart
I. Basic Algorithm Concept
An algorithm, is defined as a well-ordered collection of unambiguous and effectively
computable operations, that when executed, produces a result and halts in a finite amount of
time.
II. Basic Algorithm Constructs
All algorithms are constructed using three control structure or construct. These are:-
 The sequence structure is the construct where one statement is executed after another
 The selection structure is the construct where statements can executed or skipped
depending on whether a condition evaluates to TRUE or FALSE
 The repetition structure is the construct where statements can be executed repeatedly
until a condition evaluates to TRUE or FALSE
A. Pseduocode Construct
Class Constructs
Compute var1 as the sum of x and y or var1<- x + y
Computation/Assignmen
Assign x2 + y to var2 or var2 <-x2 + y
t
Increment counter1 or counter1<-counter1 + 1
Decrement counter1 or counter1<-counter1 - 1
Input: Get var1, var2, …
Input/Output
Output: Display var1, var2, …

Single-Selection IF Multiple-Selection IF Multiple-Selection Switch


IF condition IF condition THEN SWITCH expression TO
THEN statement/s case value1: statement/s
statement/s ELSE IF condition case value2: statement/s
Selection
THEN case valeu3: statement/s
statement/s etc.
etc default statement/s
ELSE
statement/s
WHILE condition DO FOR count=initialVal To
Repetition statement/s statement/s lastVal
WHILE condition statement/s

B. Flow Chart
Symbol Name/meaning Symbol Name/meaning
Process – Any type of internal Connector – connects sections of
operation: data transformation, data the flowchart, so that the diagram
movement, logic operation, etc can maintain a smooth, linear flow
Input/Output – input or output of Terminal – indicates start or end of
Data the program or algorithm
Decision – evaluates a condition or Flow lines – arrows that indicate
statement and branches depending the direction of the progression of
1|Page
Adama Science & Technology University
School of Electrical Enginering & Computing
Computer Science & Engineering Program

on whether the evaluation is true or the program


false
III. Working with Algorithms
Write an algorithm using pseduocode or flowchart or both for the following problems. (problem
9,10, 13 and 14,16 are to be submitted on the next lab class)
1. Write an algorithm that calculates sum of first 10 natural numbers. (Pseduocode and
Flowchart)
2. An algorithm that checks a number is odd or even. (Pseduocode and Flowchart).
3. Write an algorithm that calculates the Largest Number Among Three Numbers. (Pseduocode
and Flowchart).
4. Write an algorithm that Check Whether a character is Vowel or Consonant. (Pseduocode and
Flowchart)
5. It has been decided that a bonus of 12% of gross salary is to be given for each employee in an
organization. It was also agreed that if an employee has worked for more than 13 years
she/he is to receive an additional amount of Birr 350.00. Write an algorithm that calculate
and display the bonus and the net salary. (Flowchart)
6. Write an algorithm that chooses a random number between 1 and 20 and allow the user to
guess the number. The user is allowed only five trials. The algo should display guess result
(success or failure) and the number of trial. (Flowchart)
7. Write an algorithm that convert a score for a course to its corresponding letter grade using the
following scale. Your algorithm handles invalid input value. (Flowchart)
Score Letter Grade Score Letter Grade
>=90 A+ >=55 C+
>=80 A >4= C
>=75 B+ >=30 D
>=60 B <3 F
8. Write an algorithm that calculates and displays the roots of a quadratic equation ax 2 + bx +
c=0 where a, b and c are constant. .(Pseduocode)
9. The finance officers of the company XYZ wants to calculate total income collected from a no
of customers. Every customer is registered as typeA, typeB or typeC customer. TypeA,
typeB and typeC customer pay 100, 200, and 300 birr each respectively. Write an algorithm
that gets the customer type and calculate the subtotal income for each type and the total
income collected by the finance officer from its entire customer. Invalid type entry must be
anticipated and properly handled. (Pseduocode and Flowchart)
10. A list of positive integers is to be entered by the user. A negative or zero entry marks the end
of entry (sentinel value). Search and display the maximum and minimum of the list of
numbers. (Flowchart or pseduocode)
11. Calculate the tax to be deducted from an employee’s salary based on the following rates
(progressive tax). (Pseduocode)
 The first 300 birr is tax free.
 The next 500 birr is taxable 5% of that amount.
 The next 800 birr is taxable 10% of that amount
 The next 1000 birr is taxable 15% of that amount
 The next 1500 birr is taxable 25% of that amount.
 The rest is taxable 50 % of that amount.
12. Calculate and display the factorial of a number n. (Flowchart)

2|Page
Adama Science & Technology University
School of Electrical Enginering & Computing
Computer Science & Engineering Program

13. Write an algorithm that implement a simple calculator that performs the basic operations ( +,
-,* and /). Your algorithm should properly handle division by zero (Flowchart)

3|Page

You might also like