0% found this document useful (0 votes)
4 views18 pages

Algo Tutos

The document is a tutorial sheet for a Level 200 Computer Science Engineering course, focusing on algorithms. It contains a series of tasks (T1 to T35) that require students to write algorithms and implement them in C programming language, covering topics such as mathematical computations, data structures, and sorting algorithms. Additionally, it includes exercises on testing, debugging, and algorithm efficiency.

Uploaded by

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

Algo Tutos

The document is a tutorial sheet for a Level 200 Computer Science Engineering course, focusing on algorithms. It contains a series of tasks (T1 to T35) that require students to write algorithms and implement them in C programming language, covering topics such as mathematical computations, data structures, and sorting algorithms. Additionally, it includes exercises on testing, debugging, and algorithm efficiency.

Uploaded by

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

B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms tatsopt@gmail.

com
NB: After writing each algorithm, do the implementation in C prog. Lang. when your program
doesn’t produce expected results, modify the algorithm until it produces expected results.
T1: Write an algorithm to swap 2 integer objects using an intermediate variable.
T2: Write an algorithm that reads 12 real numbers and calculates the mean,
variance, standard deviation and then displays them.
T3: Write an algorithm which allows to reads 2 positives integers and calculates
their product using the following principle: AxB=A+A+A+...+A, (B times).
T4: Write an algorithm that reads an integer and displays its equivalent in binary
T5: Write an algorithm that reads an integer n, calculates the elements of the
pascal triangle of depth n and finally displays the triangle thus obtained.
T6: (System of equations) Write an algorithm that solves a linear system of two
equations with two unknowns parameters.
T7: Write an algorithm that calculate an were a and n are read from the keyboard.
T8: Write an algorithm which read a value n as input compute Un=Un-1+Un-2
whith U0=U1=1
T9: Write an algorithm to determine the greatest common divisor (GCD)of two
numbers read as input
T10: Let s be a sequence of positive integers ordered in ascending order and
terminated by the -1 marker. Write an algorithm to determine the length L
of the longest sub-sequence extracted from s and having only identical
elements. e.g if the following is s = 11227779-1, L = 3
T11: Write an algorithm that calculates the series 1-1/3+1/5-1/7+...+(-1)n/(2*n+1)
with a precision whose value will be read in data.
T12: Write an algorithm that calculates an approximate value of sin (x), with x
in rad. The algorithm will use the x-x3/3!+x5/5!-x7/7!+...+(-1)nx2n+1/(2n!)
development for evaluating sin (x)
T13: Write the algorithm that asks to enter a square matrix of order n, displays it,
calculates its trace and displays it. The algorithm then searches for the smallest
element of the matrix, the poster and its various coordinates.
T14: (Scalar product of two vectors) Let U and V be two vectors of real n and of
components u 1, u2, ..., un and v1, v2 ..., vn respectively. Write the algorithm that
Calculate and display the U * V scalar product.
T15: Write an algorithm which displays TRUE if a number nb is equal to the sum
of two consecutive elements of an array A and false otherwise. The number nb
and the array A are read in data.
T16: Matrix multiplication
Write and algorithm that allow to:
 Read two matrix of size NxN
 Multiply them
 Displays the result

Page 1 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
T17: Magic square
A magic square is a matrix of order n whose elements are all integers between 1
and n2 such that the sum of the elements of each column and the sum of the
elements of each diagonal line are equal. Propose an algorithm that builds a magic
square of order n.
Study the following algorithm and state what it is performing: T18 – T22
T18:

T19:

Page 2 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
T20:

T21:

Page 3 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
T22:

T23: Study the following procedures and functions carefully, write main
algorithm where you call these subroutine with appropriate parameters.

Page 4 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]

T24: Consider the following C program


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){ // main is the calling function.
int i=10,j=20;
clrscr();
printf("The values before swap is i: %d, j:%d\n", i, j); // 1.
swapv(i,j); // 2. here, i and j are formal parameters.
printf("The values after swap is i: %d, j:%d\n", i, j); // 3.
printf("\n");
swapr(&i,&j); // 4. swapv and swapr are called functions.
printf("The values after swap is i: %d, j:%d\n", i, j); // 5.
printf("\n");
return 0;
}
// Functions definition
swapv(int x,int y) { swapr(int *x,int *y) {
int temp; int temp;
temp=x; temp=*x;
x=y; *x=*y;
y=temp; *y=temp;
} }
a. State the mechanism that is used to pass parameters in 2. and 4.
b. What values will be printed for i and j in 1. 3. and 5.
c. What name do you give to i and j, then x and y?

T25:
1. We want to write an algorithm that computes

𝑛!
n
Cp = (𝑛𝑝) = ,𝑛 ≥ 𝑝
𝑝!(𝑛−𝑝)!
a. Define a function that accepts a parameter x and returns the factorial of x.
b. In the main algorithm, request two positive numbers n and p from the user and make
sure to control that 𝒏 ≥ 𝒑 before proceeding.
i. Call the function defined above appropriately to compute nCp
ii. Display the result previously computed.

Page 5 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
T26:
We want to write an algorithm that approximates the values of 𝑒 𝑥 , sin(𝑥) and cos(𝑥) for any
real value of x, using the following formulas:
𝑥 𝑥2 𝑥𝑛
𝑒𝑥 = 1 + + +⋯+
1! 2! 𝑛!
𝑥3 𝑥5 𝑥7
sin(𝑥) = 𝑥 − + − +⋯
3! 5! 7!
𝑥2 𝑥4 𝑥6
cos(𝑥) = 1 − + − +⋯
2! 4! 6!
a. Define a function that accepts a parameter n and returns the factorial of n.
b. Define a function that accepts two parameters b and n and returns the value of b raised
to the power n.
c. In the main algorithm, call the above functions appropriately to compute the
approximation of 𝑒 𝑥 , sin(𝑥) and cos(𝑥) and display the results.

T27:
a. Define a function that accepts one parameter x (decimal value) and returns its binary
equivalent
b. Define a function that accepts one parameter x (binary) and returns its decimal
equivalent
c. In the main algorithm, display a menu 1. Conversion from binary to decimal 2.
Conversion from decimal to binary, ask the user to select a valid item from the menu,
ask the number to be converted from the user and depending on the menu item selected,
call the corresponding function that does the conversion and display the result.

T28:
a. Define a function with appropriate parameters that prompts the user to fill numeric
values in an array
b. Define a function with appropriate parameters that returns the array
c. Define a function with appropriate parameters that displays the values found in the array
d. In the main algorithm, specify the size of the array and call the first function to serve
its purpose
i. Declare an array tab and initialize it with the square of each element of the array
early initialized by the user by calling the second function.
ii. Call the third function to display the values of the array tab.

T29:
a. Define a function with appropriate parameters that prompts the user to fill numeric
values in an array
b. Define a function with appropriate parameters that that sorts the values entered above
(use any sorting algorithm of your choice).
c. Define a function with appropriate parameters that displays the values found in the array
d. In the main algorithm, specify the size of the array and call the first function to serve
its purpose
i. Call the second function to serve the purpose
ii. Call the third function to serve the purpose

Page 6 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
T30:
The algorithm, represented using pseudo-code in Figure 1, describes a method
to test whether an integer greater than 2 is prime or not.

The MOD operator calculates the remainder resulting from an integer division,
for example, 10 MOD 3 = 1.
(a) Complete Table 1 by hand-tracing the algorithm in Figure 1. Use 25 as the
input value.
Table 1
Number Root d FactorFound r otput

(ii) (a) Why do we test a program? Explain the type of test data that can be used
during testing
(b) In case your program does not behave as expected what are the actions that
the programmer should take?
(c) Difference between testing and debugging, explain two testing and two
debugging techniques
(d) What is the purpose of destructive testing? Show how it can be performed on
the code above and amend only the section of the code concerned.

Page 7 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
(e) When is a program1 said to be more efficient than program2? Evaluate the
Complexity of the code above.
(iii) (a) Draw a binary tree representing the infix expression (a+b)*(c-d)
(b) What is the result of performing a post-order traversal of the above tree?
(c) Difference between static and dynamic data structure, give one example
in each case
(d) What is procedural abstraction? Illustrate it with a code snippet
T31:
The algorithm, represented using pseudo-code in Figure 2, outputs a series of
numbers. The contents of the series depends on the initial value entered by the
user.

The DIV operator calculates the result of an integer division, for example, 10
DIV 3 = 3. What you need to do:
1. Test the algorithm by showing the result of entering: -3, then 11, then 10
T32:
The algorithm, represented using pseudo-code in Figure 1, describes a method
to rearrange three numbers in a data structure.

Page 8 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]

1. Complete Table 1 by hand-tracing the algorithm in Figure 1.

2. What type of rearrangement does this algorithm perform?


T33:
The algorithm, represented using pseudo-code in Figure 2, outputs a series of
integers or the message No result. The output depends upon the value entered
by the user.

1. Dry run the code by entering the number 720


2. Dry run the code by entering the number 600

Page 9 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
T34:

b Explain the function of the flowchart in part a.

Page 10 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
T35:
Sorting Algorithms
Example1: A list of unsorted elements are: 23 78 45 8 32 56
Algorithm: Selection_Sort (A [ ], N)
Step 1: Repeat For I:= 0 to N–2 Begin
Step 2: Set Key := I
Step 3: Repeat for K := I+1 to N–1 Begin
If A[K] < A [Key]
Set Key := K
End For
Step 5: Swap A [I] with A [Key]
End For
Step 6: Exit Trace table for the selection sort
N I I < =N-2 Key K K < =N-1 A[K] < A [Key] A [I] ←→ A [Key]
Unsorted: 6 23 78 45 8 32 56
Pass No: 1 ComparisonNo: 1 0 True 0 1 True False
ComparisonNo: 2 2 True False
ComparisonNo: 3 3 3 True True
ComparisonNo: 4 4 True False
ComparisonNo: 5 5 True False
(6-1) = 5 compa. 6 False - A [0] ←→ A [3] 8 78 45 23 32 56
Pass No: 2 ComparisonNo: 1 1 True 2 True
ComparisonNo: 2 3 True
ComparisonNo: 3 4 True
ComparisonNo: 4 5 True
(6-2) = 4 compa. 6 False - A [1] ←→ A [3] 8 23 45 78 32 56
Pass No: 3 ComparisonNo: 1 2 True 3 True
ComparisonNo: 2 4 True
ComparisonNo: 3 5 True
(6-2) = 4 compa. 6 False - A [2] ←→ A [4] 8 23 32 78 45 56
Pass No: 4 ComparisonNo: 1 3 True 4 True
ComparisonNo: 2 5 True
(6-2) = 4 compa. 6 False - A [3] ←→ A [4] 8 23 32 45 78 56
Pass No: 5 ComparisonNo: 1 4 True 5 True
(6-2) = 4 compa. 6 False - A [4] ←→ A [5] 8 23 32 45 56 78
Sorted List 5 False 8 23 32 45 56 78

Page 11 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
Fill the following Trace table for the selection sort
N I I < =N-2 Key K K < =N-1 A[K] < A [Key] A [I] ←→ A [Key]
Unsorted: 8 77 33 44 11 88 22 66 55
Pass No: 1

Pass No: 2

Pass No: 3

Pass No: 4

Pass No: 5

Pass No: 6

Page 12 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]

Pass No: 7

Sorted List

INSERTION_SORT (A[ ])
1. FOR j ← 2 TO length[A]
2. DO key ← A[j]
3. // {Put A[j] into the sorted sequence A[1 . . j − 1]}
4. i←j−1
5. WHILE i > 0 and A[i] > key
6. DO A[i +1] ← A[i]
7. i←i−1
8. end while
A[i+1] ← key
End for
Example: the following figure shows the operation of INSERTION-SORT on the array A= {5, 2, 4, 6, 1, 3}.

Page 13 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
Trace table for the insertion sort
N j j < =N Key i i > 0 and A[i] > key A[i+1] A[i]
Unsorted: 6 5 2 4 6 1 3
Pass No: 1 2 True A[2]=2 1 True A[2]=5 A[1]=2 2 5 4 6 1 3
0 False 2 5 4 6 1 3
Pass No: 2 3 True A[3]=4 2 True A[3]=5 A[2]=4 2 4 5 6 1 3
1 False 2 4 5 6 1 3
Pass No: 3 4 True A[4]=6 3 False A[3]=5 A[2]=4 2 4 5 6 1 3
Pass No: 4 5 True A[5]=1 4 True A[5]=6 A[4]=1 2 4 5 1 6 3
3 True A[4]=5 A[3]=1 2 4 1 5 6 3
2 A[3]=4 A[2]=1 2 1 4 5 6 3
1 A[2]=2 A[1]=1 1 2 4 5 6 3
0 False 1 2 4 5 6 3
Pass No: 5 6 True A[6]=3 5 True A[6]=6 A[5]=3 1 2 4 5 3 6
4 True A[5]=5 A[4]=3 1 2 4 3 5 6
3 True A[4]=4 A[3]=3 1 2 3 4 5 6
2 False 1 2 3 4 5 6
Sorted List 7 False 1 2 3 4 5 6

Fill the following Trace table for the insertion sort


N j j < =N Key i i > 0 and A[i] > key A[i+1] A[i]
Unsorted: 6 15 6 12 18 3 9
Pass No: 1

Pass No: 2

Pass No: 3
Pass No: 4

Pass No: 5

Sorted List

Page 14 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
Bubble_Sort (A [ ] , N )
For i := 1 to N
For J := 1 to N – i
If (A [ J ] > A [ J + 1 ] )
Swap (A [ J ] , A [ J – 1 ] )
End For
End For
Exit

Consider the above Bubble sort Algorithm. Obtain a trace table to sort the following array A= {77, 33, 44, 11, 88, 22, 66, 55}

Page 15 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
T35:
Write an algorithm which read an array of N students names of Beng1. The
algorithm read also an array of N real numbers representing the Algorithm CA marks
of those students. The name and students marks are read simultaneously.
The algorithm will sort the array of marks in descending order and process the
ranking in order of merit( from the highest mark to the lowest). The result should be
displayed as shown on below:
RANK NAME MARK GRADE

The algorithm will display the general average, the total number of pass, the total
number of fail and overall best student in class for the course algorithm.

For the grade, consider the followings:


1. Grade:
 If the mark is between 0 and 4/20, the grade to display is "weak"
 If the mark is between 5 and 10/20, the grade to display is "below average"
 If the mark is between 10 and 12/20, the grade to display is "Average"
 If the mark is between 12 and 14/20, the grade to display is "Fairly Good"
 If the mark is between 14 and 16/20, the grade to display is "Good"
 If the mark is between 16 and 18/20, the grade to display is "Very Good"
 If the mark is above 18/20, the grade to display is "Excellent"
2. Name:
 Just one of the student’s name of max size 15 is enough for the exercise
Eg: paul ….
3. Marks:
 Marks range from 0 to 20
Guidance for the Work to do
1. identify the type of data to be used to accommodate all students’ names, marks
and grade and use them as global data throughout the algorithm.
2. Define a procedure sortMark( parameters ) with appropriate parameters that will
sort the marks in descending order (use any sorting algorithm of your choice)
make sure that when the mark is displaced, the name of the student associated to
it is moved at the same position or index on its own side (this is to avoid
mismatch of original data collected).
3. Define a procedure setGrade( parameters ) with appropriate parameters that will
put the grade corresponding to each mark.
4. Define a procedure display( parameters ) with appropriate parameters that will
display the rank, the name, the mark and grade of all the students.
5. In the main algorithm, request the user to define the size of the class
6. Then request the marks of all students in the class alongside their names

Page 16 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
7. And call all the above procedure in a correct order and where needed to print the
final result.
8. A test/dry run of the algorithm will produce a sample below:
We want to modify this algorithm so that we can add a structure to accommodate
more course conveniently. Do the modification

Enter the size of the class


5
Enter Name of Student No: 1
paul
Enter Mark of paul:
12
Enter Name of Student No: 2
peter
Enter Mark of peter:
4
Enter Name of Student No: 3
lima
Enter Mark of lima:
18
Enter Name of Student No: 4
sama
Enter Mark of sama:
17
Enter Name of Student No: 5
simo
Enter Mark of simo:
15

Initial data collected from the user

Rank Name CA Grade


--------------------------------------
1 | paul | 12 |
--------------------------------------
2 | peter | 4 |
--------------------------------------
3 | lima | 18 |
--------------------------------------
4 | sama | 17 |
--------------------------------------

Page 17 of 18
B.Eng – CSE/Level 200 Tutorial Sheet – Algorithms [email protected]
5 | simo | 15 |
--------------------------------------

Processing Marks, sorting...


Processing Marks, grade...
Final Processing...

Rank Name CA Grade


-------------------------------------------------
1 | lima | 18 | EXCELLENT
-------------------------------------------------
2 | sama | 17 | VERY GOOD
-------------------------------------------------
3 | simo | 15 | GOOD
-------------------------------------------------
4 | paul | 12 | FAIRLY GOOD
-------------------------------------------------
5 | peter | 4 | WEAK
-------------------------------------------------

Page 18 of 18

You might also like