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

NAME: DENNIS MUTEMI.

REG.NO:18/04189.
PROGRAM: BSD.
MODE OF STUDY: DAY.
UNIT: DATA STRUCTURES AND ALGORITHMS
CAMPUS: MAIN.

QUESTION ONE. 10 MARKS

When a car alarm sounds, if it is one of the students’ cars and it belongs to a student and we
know that the alarm on that car regularly goes off, ignore it. Otherwise, go outside and have a
look, if the car does not seem to be broken into and there is nobody around, ignore the alarm.
Otherwise call the police. Create an algorithm expressed as a flowchart for this scenario.
Solution
Problem Analysis
 Problem: To determine why a car alarm goes off.
 Input: Car alarm sound.
 Output: Call the police.
 Process: Go outside and take a look.
 Test: Dry run.

1|Page
A flowchart to determine whether to ignore or call police when a car alarm
sounds.

Start

Car alarm
sounds

If it is one of the YES


NO students’ cars and it
Go outside and
belongs to a student
have a look
and we know that
the alarm on that car
regularly goes off

If the car does YES


not seem to be
Ignore
broken into and
there is nobody

NO

Call the police

2|Page
Stop
QUESTION TWO: 10 MARKS

Create an algorithm expressed in pseudo-code that asks a user to enter a start number and an end
number and then outputs the total of all the numbers in the range. For example, if the start
number was 1 and the end number was 10, the total would be 55 (that is 1 + 2 + 3 + 4 + 5 + 6 + 7
+ 8 + 9 + 10).
Solution
Problem analysis.
 Problem-display sum of numbers in a specified range.
 Input-start number and end number.
 Output-sum of numbers between start number and end number.
 Process-calculate the sum of all the numbers between start number and end number.
 Test-dry run.
A pseudo-code that asks a user to enter a start number and an end number and then
outputs the total of all the numbers in the range.
1. Start
2. Declare variables startnum ,endnum ,sum and counter.
3. Set sum:=0.
4. Get values of startnum and endnum from the user.
5. FOR counter= startnum TO counter<= endnum.
5.0. Calculate sum=sum+counter.
5.1. Update counter= counter+1.
6. End FOR
7. Display sum.
8. Stop.

3|Page
QUESTION THREE: 20 MARKS
Assuming you pick ten numbers beginning with 3 and ending with 9 as shown below:
3,4,7,11,6,4,13,7,8,9.
Solution
i. Store the numbers in a stack.
Stack
9
8
7
13
4
6
11
7
4
3
ii. Pop the numbers from the sack in i) above and store them in a binary search tree.
 9,8,7,13,4,6,11,7,4,3.

8 13

7 11

3 6

iii. Traverse the binary search tree created in ii) above using the pre-order traversal.
Preorder –visit the root node, left sub-tree then the right sub-tree.
 9,8,7,4,3,6,13,11

4|Page
iv. Store the numbers displayed in iii) in a hash table (resolving any collisions using
quadratic probing where the table size is 9).
index 0 1 2 3 4 5 6 7 8
element 9 11 3 4 13 6 7 8

Calculate the index using the hash function.


 Elements=9,8,7,4,3,6,13,11
Index=element modulus table size.
 9%9=0
 8%9=8
 7%9=7
 4%9=4
 3%9=3
 6%9=6
 13%9=4 a collision has occurred
(13+ (12))%9
14%9=5.
 11%9=2.

5|Page

You might also like