You are on page 1of 16

# Computer and Information Technology for (HKCEE) Module A2

## 9.1Solving Programming Problems

9.2 Solving the Same Problem Using
Different Algorithms
Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Steps of solving programming problems
Specify a problem
Formulate an algorithm for the problem
Realise the algorithm into a program

## © Longman Hong Kong Education Page2

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Case Study 1
Problem definition:
Input the ages which are less than 12
Calculate and print the average age
First-level pseudocode
1. Get input
2. Record number input
3. Perform computations
4. Display result in a real number
© Longman Hong Kong Education Page3
Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Second-level pseudocode
1. Get data
1.1 Input data value <12
2. Record number input
3. Perform computations
3.1 Sum up input value
3.2 Average the sum obtained
4. Display result in a real number

## © Longman Hong Kong Education Page4

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Structural chart for finding the average age

## © Longman Hong Kong Education Page5

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Case Study 2
Problem definition:
Calculate the future value using the formula
future value
= loan * (1 + interest rate) ^ number of year
First-level pseudocode
1. Get data
2. Perform computations
3. Display results in two decimal places

## © Longman Hong Kong Education Page6

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Second-level pseudocode
1. Get data
1.1 Get loan
1.2 Get number of year
2. Perform computations
2.1 future value
= loan * (1+interest rate) ^ number of year
3. Display results in two decimal places

## © Longman Hong Kong Education Page7

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Structural chart for finding the future value

## © Longman Hong Kong Education Page8

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Case Study 3
Problem definitions:
Convert a line of letters into cryptograms based
upon:
Letter from A to N:
new_letter := chr(ord(‘original_letter’) – 15);
Letter from O to Z:
new_letter := chr(ord(‘original_letter’) + 30);

## © Longman Hong Kong Education Page9

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Algorithm development
1. Get data
2. Perform encryption
3. Display cryptogram

## © Longman Hong Kong Education Page10

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.1 Solving Programming Problems

Structural chart for finding the future value

## © Longman Hong Kong Education Page11

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.2 Solving the Same Problem

Using Different Algorithms
Guidelines of choosing the best algorithms
easy to understand the algorithm, program it and
debug it
minimize the programming effort
apply on the computer resources efficiently (time &
space)

## © Longman Hong Kong Education Page12

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.2 Solving the Same Problem

Using Different Algorithms
Case Study 4
Problem definition
Find and print the maximum and the minimum
values of 10 positive integers.

## © Longman Hong Kong Education Page13

Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.2 Solving the Same Problem

Using Different Algorithms
Algorithm 1
1. Set both search keys for the maximum and the
minimum to zero.
2. Compare the search key with the other items one-by-
one and replace the value of the search key with that
of the item if the value of the item is larger.
3. Compare the search key with the other items one-by-
one and replace the value of the search key with that
of the item if the value of the item is smaller.
4. Display both search keys.
© Longman Hong Kong Education Page14
Computer and Information
Technology for (HKCEE)
Module A2: Part C

## 9.2 Solving the Same Problem

Using Different Algorithms
Algorithm 2
1. Sort the list of items in ascending order.
2. Display the values of the first and the last items.

## © Longman Hong Kong Education Page15

Computer and Information Technology for (HKCEE) Module A2

END