Tutorial
ON PSEUDO WRITING
Algorithm 1.1
❖ Write an algorithm to find the sum of three given
numbers
– GIVENS: N1, N2, N3
– RESULTS: Total
– -------------------------
– METHOD:
input N1
input N2
input N3
Total = N1 + N2 + N3
output Total
int x; int y; int z; int sum;
Console.Write(“Enter x”);
x = Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter y”);
y = Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter z”);
z = Convert.ToInt32(Console.ReadLine());
sum = x + y + z;
Console.WriteLine(“{0}{1}”, “Sum=”, sum);
Algorithm 1.2
❖ Write an algorithm to find the result of a division
operation for the given two numbers X and Y
– GIVENS: X, Y
– RESULTS: Quotient
– -------------------------
– METHOD:
input X
input Y
Quotient = X/Y
output Quotient
Algorithm 1.3
❖ Write an algorithm to find the sum and product of
the two given numbers
– GIVENS:Num1, Num2
– RESULTS: Total, Product
– -------------------------
– METHOD:
input Num1
input Num2
Total = Num1 + Num2
Product = Num1 * Num2
output Total
output Product
Algorithm 1.4
❖ Find the sum and average of three given numbers
– GIVENS:Num1, Num2, Num3
– RESULTS: Sum , Average
– -------------------------
– METHOD:
input Num1
input Num2
input Num3
Sum = Num1 + Num2 + Num3
Average = Sum /3
output Sum
output Average
Algorithm 1.5
❖ Given 3 assignment marks (out of 50, 20, 70), find
the average (calculated as a mark out of 100)
❖ General Concept
– How does one figure out the percentage of several
marks?
• Add them all up
• Divide by the maximum possible mark (50+20+70)
• Multiply by 100
Algorithm 1.5 (3-4:30)
❖ Given 3 assignment marks (out of 50, 20, 70), find the average,
calculated as a mark out of 100
– GIVENS: A1, A2, A3
– RESULTS: Mark
– INTERMEDIATES: Total, MaxMark
– -------------------------
– METHOD:
MaxMark = 140
input A1
input A2
input A3
Total = A1 + A2 + A3
Mark = Total/MaxMark * 100
Output Mark
Algorithm 1.6
❖ Given a two digit number, find the
41 \ 10 = 4
sum of its digits
❖ General Concept
– How can we break apart a number?
• 41 = 4 Tens and 1 Ones
• so for the number 41, we want 4 + 1 = 5
– Use integer division
• DIV returns the integer part of a division 41 MOD 10 = 1
• MOD returns the remainder of a division
Algorithm 1.6
❖ Given a two digit number, find the sum of its digits
– GIVENS: N
– RESULTS: Sum
– INTERMEDIATES: Tens, Ones
– -------------------------
– METHOD:
input N
Tens = N div10
Ones = N mod 10
Sum = Tens + Ones
output Sum
Algorithm 1.8
❖ Write an algorithm which adds the given two numbers
(X and Y) and returns the sum in the given variable X
– GIVENS:X, Y
– RESULTS: X
– INTERMEDIATES: None
– -------------------------
– METHOD:
input X
input Y
X = X+ Y
output X
Tracing an Algorithm (8-9:30)
❖ The purpose of tracing an algorithm is to ensure
that it works
❖ This is a paper test. As we will see later, it should
be completed before writing the computer code
❖ Tracing involves
– Executing the sequence of instructions with a sample
set of values
– Computing the value of each variable after each
instruction is executed
– Checking for the correct result
Tracing an Algorithm
❖ Step 1 - Number every instruction in the algorithm
❖ Step 2 – Make a table
– The first column of the table indicates which instruction
has been executed
– Subsequent columns list all the variables of the
algorithm (Givens, Results, Intermediates)
Tracing an Algorithm
❖ Step 3 – Complete the table
– Each column of the table represents a variable
– Start at the first line of your algorithm. Identify what
will happen to each variable as that instruction is
executed
• Change any values which the instruction changes and leave all
other columns blank
Trace 1.1
❖ Trace Algorithm 1.4 using the numbers 24, 31, and 35
Line Num1 Num2 Num3 Sum Avg
METHOD:
(1) input Num1 1 24
(2) input Num2 2 31
(3) input Num3
3 35
(4) Sum = Num1 + Num2 + Num3 4 90
(5) Average = Sum /3
5 30
(6) Output Sum 6 output 90
(7) Output Average
7 output 30
Trace 1.2
❖ Trace Algorithm 1.5 with the numbers 40, 18, 26
METHOD: Ln A1 A2 A3 MM Ttl Mark
(1) MaxMark =140 1 140
(2) input A1
(3) input A2 2 40
(4) input A3 3 18
4 26
(5) Total = A1 + A2 + A3 5 84
(6) Mark = Total/MaxMark * 100
6 60
(7) output Mark 7 output 60
THE ANSWER IS NOT 69 (40/50 + 18/20 + 26/70)/3