Sie sind auf Seite 1von 8

LAB 2:

PROBLEM SOLVING

By the end of this section, students should be able to:

Understand basic techniques of problem solving Develop algorithms and pseudocode

2.1

INTRODUCTION
Problem solving refers to a process which is used to transform problem statement to solutions by

using knowledge that applicable to problem domains. In the case of a computer, the problem solution is usually in the form of programs that include the algorithm which explains to the computer a clearly defined procedure for achieving the solution. An algorithm is a representation of a solution to a problem. An algorithm also can be define as a procedure for solving a problem in terms of the actions to be executed and the order in which these actions are to be executed. The procedure can break down the problem solution into parts that can be execute and understand. Normally, the procedure will consist of smaller steps each of which the computers understand. By then a program is then just a specific transformation of an algorithm, which may be executed on a physical device. A computer is a physical device designed to carry out a collection of instruction. A procedure is a sequence of instructions written in terms of which instruct a proper operation. To make effective use of an algorithm on a computer, one must not only find and understand a solution to the problem but also give the correct sequence of understood commands that represent the same instruction to the computer. In general problem solving techniques will involve analyzing the problem, outline the problem requirements and design steps (algorithm) to solve the problem. Therefore before program is written, the programmer must know what data are to be used, the desired result, and the process and procedure to be used to produce the result. Normally the pseudocode or flowchart will be use to help programmers to develop the algorithm.

2.2

GENERAL INFORMATION
Algorithms

A problem can be solved by executing a series of actions in a specific order. Algorithm is a procedure for solving a problem in terms of the actions to be executed and the order in which these actions are to be executed. Algorithm can be written in pseudo codes or flowchart. Pseudocode

Pseudocode is informal language that helps programmers develop algorithms. Its purpose is to describe in precise algorithm detail, what the program being designed is to do. Pseudocode defined the steps to accomplish the tasks in sufficient details so that they can be converted to computer program. Written as a combination of English and programming constructs Based on selection (if, switch) and iteration (while, repeat) constructs in high-level programming languages Flowchart

A diagram that shows the step by step execution of a program. Each symbol indicates the type of operation to be performed. Flowchart graphically illustrates the sequence in which the operations are to be performed

Table 1: Common flowcharts symbols Symbol Name Terminal/Terminator Description The beginning or end of a program

Process

Computation manipulation.

or Indicates

data any

type of internal operation

Data

Used for any Input / Output (I/O) operation. Indicates that the computer is to obtain data or display output or results

Decision

A program branch point

On-page reference/ Connector

An entry to, or an exit from, another part of the flow chart

Flow lines

Connecting

the

flowcharts

symbols. Shows direction of flow.

Basic control structures: i. Sequence: is a series of statements that execute one after another

Statement 1

Statement 2

Statement 3

ii.

Selection (branch): is used to execute different statements depending on certain conditions. Eg: IF condition THEN statement1 ELSE statement2

ii.

Repetition (loop): is used to repeat statements while certain conditions are met.

CONDITION TRUE Statement

FALSE

2.3

EXAMPLE OF PSEUDOCODE AND FLOWCHART

Problem 1: Employees Salary Problem Given the employees working hour, a program will calculate salary for an employee. The pay rate for an hour is RM 6.50. The program will display the total salary of the employee.

SYSTEM ANALYSIS Input : employees working hour (Hour )

Output : salary Variables : salary, Hour Process/Formula : Hour*6.50

SYSTEM DESIGN

Pseudocode 1. Start 2. Set salary=0 3. Read employees working hour: Hour 4. salary = Hour*6.50 5. Print salary 6. End

Flowchart
START

SALARY=0

READ EMPLOYEES WORKING HOUR, HOUR

SALARY=HOUR*6.5

PRINT SALARY

END

Problem 2: Arithmetic Problem Problem: A program will add two numbers and print a statement whether the sum is an even number or an odd number. The program will repeat for five times.

Input

SYSTEM ANALYSIS : two numbers: A, B : an even number or an odd number C=A+B, If C = even number,print even number If C not even number, print odd number repeat for five times

Output

Process/ Formula : -

SYSTEM DESIGN Pseudocode: 1. Start 2. Set COUNTER=0 3. While COUNTER < 5 4. Read two numbers: A, B 5. C = A + B 6. If C is an even number Print EVEN NUMBER 7. Else Print ODD NUMBER 8. Add 1 to COUNTER 9. End

Flowchart
START

COUNTER = 0

COUNTER < 5?

False

END

True
READ A,B

C=A+B

C IS EVEN NUMBER?

False

PRINT ODD NUMBER

True
PRINT EVEN NUMBER

COUNTER = COUNTER + 1

2.4

EXERCISES
1. Define what is algorithm? 2. In what kind of form the algorithm can be written?

3. List out the basic control structures and briefly explain each of it.

4. Briefly explain three type of error. 5. List out and briefly explain the common flowcharts symbol? 6. Write the analysis ( input, output and process/formula ) and design ( pseudocode and flowchart ) for problems below:

6.1 A program will read four integers from the keyboard, calculates their sum, and then prints the numbers with the sum.

6.2 Represent the processes/activities/sequences of sending a message using mobile phone. 6.3 Warong Abu provides a menu of nasi lemak for breakfast. The price of the items are:

Nasi Lemak + sambal + ikan bilik + timun Option: Sambal sotong Sambal udang Fried/ Boiled Egg

RM1.50

RM1.50 RM1.00 RM0.60

Each customer may order nasi lemak with any option. Calculate the price of each order. 6.4 The program will print A for exam marks greater than or equal to 80, Print B for marks in the range 65 to 79, Print C for marks in the range 50 - 64, Print D for marks in the range 40 to 49 and E for all other marks.

6.5 The program will calculate the perimeter of a circle. The radius is 7.With the perimeter formula (2*radius*PI), print the output of the perimeter for the circle. Value of PI is 3.142.

Das könnte Ihnen auch gefallen