You are on page 1of 6

HIBMAT UNIVERSITY INSTITUTE OF BUEA (HUIB)

BUEA and DOUALA

SCHOOL YEAR: 2023-20224 LEVEL: HND 1, SEMESTER: 1


INSTRUCTOR: NGOLAH KENNETH TIM (PhD. Cand. SWE, MSc. CSc & Eng) DEPARTEMENT: CCE
COURSE CODE: Introduction to Algorithms

TUTORIAL SHEET

ALGORITHMS AND COMPLEXITY


Write an algorithm in pseudo code and flow chat to do the following
1. To boil 10 eggs
2. To cook jellof rice for 5 people
3. To describe the road to your house to somebody coming for the first time. Note. The person has to
walk on foot for the last 300 m to your house.
4. To prompt for an integer value from the keyboard and print it on the screen
5. To prompt for two decimal values from the keyboard and print the sum on the screen
6. To prompt for three integer values from the keyboard and prints the highest of the numbers on the
screen using:
a) if statement
b) case statement
7. To display the sum and the average of the first 100 integer numbers on the screen using:
a) for loop
b) while loop
c) Do while loop
8. Simulate the algorithm below when the input num =7

1
9. Consider the flow chart in Figure 1 below:
What would the result of your algorithm be if N=22

10. Figure 1 below represents an algorithm to solve a computational problem.

1: Start
2: Read A
3: Let i2
4: while(i < A)
5: if (A is divisible by i)
6: Display i,
7: ii + 1
8: endwhile
9: Stop

Figure 1: Pseudo-codes for the algorithm.


a. Propose a flow chart for the algorithm in Figure 1.
b. What is the output if A=8?
c. What is the time complexity of the algorithm
2
11. Give an algorithm (pseudocode) to do the following
a) to get an integer number and checks if it is odd or even. If odd, add 1, if
even, add 2
b) to get an integer number and check if it is prime or not
c) to get an integer number and get the first and last digit then add them
d) to get an integer number and display the factors
e) to get two integer numbers and look for the LCM and HCF

12. HIBMAT Buea wants to implement a system that can be used in secondary
school Mathematics. The system needs only get an integer number from the
user and checks if the number is positive (>0) or negative (<0). If the number is
negative, the system stops with no output. If the number is positive, the system
should check if the number is odd or even. If the number is even, the system
should check if the number is a multiple of 4 and displays "Multiple of 4" if
true or it displays "Not multiple of 4" if false, The system should display "Odd
number" if the number is odd.

Constrains:
A number is positive if it is greater than zero
A number is negative if it is less than zero
A number is even if it is divisible by 2
A number is odd if it is not divisible by 2
A number is a multiple of 4 if it is divisible by 4
A number is not a multiple of 4 if it is not divisible by 4
Task:
a) Write an algorithm in pseudocode for the system
b) Write the equivalent algorithm in flow chart for the system
c) Hand trace or simulate the algorithm with 6 as the number from the user
12. Study the following pseudocode
1. start
2. declare a,b,c as integer
3. set a=1
4. print "hnd 2021"
5. while a<3
6. set c=a
7. print a
8. set b=1
9. while b<4
10. set c=c+a
11. print c
12. if(a is equal to 1 and c is greater than or equal to 4) then;
13. print "let us work hard"

3
14. else
15. if(a is equal to 1 and c is greater than or equal to 4) then
16. print "who do we appreciate?"
17. endif
18. endif
19. set b=b+1
20. end while
21. set a=a+1
22. end while
23. stop

Task to do:
a) Draw the flow chart of this pseudocode
b) Simulate or handtrace this algorithm

14. Consider the following algorithm in pseudocode given below


1. start
2. variables: x=0, i,j,k as integer
3. for (i=0; i<2; i+1)
4. for (j=0; j<2; j+1)
5. for (k=0; k<2; k+1)
6. x=x+1
7. Display x
8. endfor
9. endfor
10. endfor
11. stop
Task:
a) Give the equivalent flow chart of the algorithm
b) Find the space complexity of the algoritm
c) Find the time complexity of the algorithm
d) Simulate or handtrace the algorithm

4
15. Figure below represents an algorithm to solve a computational problem.

1. Start
2. Variables: a,b,c as integers
3. for(a=0; a<=3; a+1)
4. {
5. for(b=1; b<5; b+1)
6. {
7. for(c=2; c<=5; c+1)
8. {
9. Display c;
10. }
11. }
12. }
13. Stop
figure 1: Algorithm

a. Propose a flow chart for the algorithm in the Figure above. (6 marks)
b. Simulate the algothm (3 marks)
c. Calculate the space complexity of the algorithm (2 marks)
d. Calculate the time complexity of the algorithm (2 marks)

16. Calculate the time complexity of the following;


1. #include<stdio.h>
int main()
{
int a,b,c;
c=a+b;
printf(“%d”,c);
return ;
}

2. for ( i = 0; i < N; i++ )


statement;

3. for ( i = 0; i < N; i++ ) {


for ( j = 0; j < N; j++ )
statement;
}

4. while ( low <= high ) {


mid = ( low + high ) / 2;
if ( target < list[mid] )

5
high = mid - 1;
else if ( target > list[mid] )
low = mid + 1;
else break;
}

5. void quicksort ( int list[], int left, int right )


{
int pivot = partition ( list, left, right );
quicksort ( list, left, pivot - 1 );
quicksort ( list, pivot + 1, right );
}

6. int fun(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < n; j += i)
{
// Some O(1) task
}
}
}

You might also like