0% found this document useful (0 votes)
58 views

USTH Write Pseudocode - Practice

1. Write pseudocode to calculate and print the sum of even numbers from 2 to 10. The pseudocode initializes a sum variable to 0, a counter variable to 2, uses a while loop to add the counter to the sum and increment the counter by 2 if it is less than or equal to 10, and finally prints the sum. 2. Write pseudocode to read three numbers and print the largest. The pseudocode reads three numbers, compares them, and prints the largest. 3. Write pseudocode to read a positive integer n and print the product of all positive integers less than or equal to n. The pseudocode initializes a product variable to 1, uses a for loop to multiply the product by

Uploaded by

nhng2421
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

USTH Write Pseudocode - Practice

1. Write pseudocode to calculate and print the sum of even numbers from 2 to 10. The pseudocode initializes a sum variable to 0, a counter variable to 2, uses a while loop to add the counter to the sum and increment the counter by 2 if it is less than or equal to 10, and finally prints the sum. 2. Write pseudocode to read three numbers and print the largest. The pseudocode reads three numbers, compares them, and prints the largest. 3. Write pseudocode to read a positive integer n and print the product of all positive integers less than or equal to n. The pseudocode initializes a product variable to 1, uses a for loop to multiply the product by

Uploaded by

nhng2421
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Practice:

Write down algorithms using Pseudocode

1
Strongly recommend (1)
 Be consistent!
 Keywords should be standard English words
 Use keywords (if/then/else, while, for, repeat/until, case, return, go to)
 Notation (variable←value, Array[index],bigger > smaller, etc.).
 Indent everything carefully and consistently for nested loops and
conditionals.
 Don’t add braces or begin/end tags
 Short variable names are good, but readability is more important than
concision
 Short but complete words are better than single letters.
 Absolutely never use pronouns!

2
Strongly recommend (2)
 Use standard mathematical notation. For example,
 write x·y instead of x∗ y for multiplication;
 write x mod y instead of x% y for remainder;
 write instead of sqrt(x) for square roots;
 write ab instead of power(a,b) for exponentiation;
 Avoid mathematical notation if English is clearer. For example, ‘Insert a into X’ may
be preferable to INSERT(X,a) or X ←X∪{a}.
 Each statement, structuring element should fit on one line, such as
 for, while, if
 But Not i←i+1; j← j−1; k←0.
 Don’t use a fixed-width typeface to typeset pseudocode; such as
 use italics for variables,
 SMALL CAPS for algorithms and constants,
 and a different typeface for literal strings and comments. 3
Example

Write an algorithm that calculates and prints the


sum of the even integers from 2 to 10.

4
Pseudocode
Input: no input.
Output: the summation of all even numbers from 2 to 10.

Step 1: Start
Step 2: Create a variable to hold the sum.
Step 3: Initialize the sum to zero.
Step 4: Create a variable to hold the counter.
Step 5: Initialize the counter to 2.
Step 6: If counter is larger than 10, go to Step 10.
Step 7: Add counter to the sum.
Step 8: Add 2 to the counter.
Step 9: Go to Step 6.
Step 10: Print sum.
Step 11: Stop. 5
Shorter Pseudocode

BEGIN
Set sum to 0
Set count to 2
while (count <= 10) do
Add count to sum
Add 2 to count
endwhile
Print sum
END

6
Compact Pseudocode

sum  0 Set sum  0


count  2 for i  2 to 10 step 2 do
while (count <= 10) do sum  sum + i
sum  sum + count PRINT sum
count  count + 2
endwhile
Print sum

Set sum ← 0
Set counter ← 2
REPEATE
sum ← sum + counter
counter ← counter + 2
UNTIL counter > 30
PRINT sum
7
Trace table
Sum Count Count <= 10 Output
0
2
True
2
4 sum  0
True count  2
6 while (count <= 10)
6 sum  sum + count
True count  count + 2
12 endwhile
8
Print sum
True
20
10
True
30
12
False
30
Exercises
1. Write an algorithm to read three numbers then display the largest.
 Use a trace table to determine what is printed by the algorithm if the input values are the
following:
(a) 8,9,10 (b) 9,8,8 (c) 9,8,10
2. Given a positive integer number n from the user, write an algorithm to compute the
product of all positive integers smaller than or equal to n. Print the result.
 Use a trace table to determine what is printed by the algorithm when n = 5.
3. Write an algorithm to read two numbers then display all odd numbers between them.
 Use a trace table to determine what is printed by the algorithm if the input values are the
following:
(a) 3,10 (b) 9,3 (c) 9,9 (d) 6,6
4. Given an integer n from the user then display whether or not n is a prime number?
 Use a trace table to determine what is printed by the algorithm if n is the following:
 (a) 10 (b) 2 (c) 17

5. Given a positive integer number n from the user, write an algorithm to count all odd
numbers from 1 to n and print the result.
 Use a trace table to determine what is printed by the algorithm when n = 17.
9
Exercises (cont.)
6. Write an algorithm that asks the user for a positive number. If the input
is not a positive number, the program should keep asking until the user
enters a positive number.
 Use a trace table to determine what is printed by the algorithm if the user
enter following values:
10, 5.5, -10, abc, 0, 6.
7. Modify the algorithm in exercise 6 so that the user keeps track of how
many times the user enters a “wrong” number. Print this at the end.
 Use a trace table to determine what is printed by the algorithm if the
input values are the following:
(a) 10 (b) -10, -9, -8, abc, 10
8. Modify the algorithm in exercise 7 so that the program will be
terminated if the user does not enter a “right” number within 5
attempts.

10

You might also like