Professional Documents
Culture Documents
TUTORIAL SHEET
1
9. Consider the flow chart in Figure 1 below:
What would the result of your algorithm be if N=22
1: Start
2: Read A
3: Let i2
4: while(i < A)
5: if (A is divisible by i)
6: Display i,
7: ii + 1
8: endwhile
9: Stop
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
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)
5
high = mid - 1;
else if ( target > list[mid] )
low = mid + 1;
else break;
}
6. int fun(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < n; j += i)
{
// Some O(1) task
}
}
}