pengenalan kepada pengaturcaraan berstruktur

37
MTS 3013 STRUCTURED PROGRAMMING INTRODUCTION

Upload: unit-kediaman-luar-kampus

Post on 22-Apr-2015

221 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Pengenalan kepada pengaturcaraan berstruktur

MTS 3013 STRUCTURED

PROGRAMMING

INTRODUCTION

Page 2: Pengenalan kepada pengaturcaraan berstruktur

Never tire of learning new things. Stretch your mind. Broaden your experience.

Page 3: Pengenalan kepada pengaturcaraan berstruktur

Introduction

A computer is an electronic device capable of performing commands given by human.

Program – Are sequences of instructions and decisions that the computer carries out to achieve a task

Programmer – person responsible in writing a program

Page 4: Pengenalan kepada pengaturcaraan berstruktur

Defining Control Structures

All computer programs are written using one or more of three basic structures: Sequence Repetition Selection

These are called control structures or logic structures because they control the flow of a program’s logic.

Page 5: Pengenalan kepada pengaturcaraan berstruktur

The sequence structure

You use it each time you follow a set of directions, in order, from beginning to end.

Example – a cookie recipe – you need to follow each recipe instruction in order, beginning with the first instruction and ending the last

Page 6: Pengenalan kepada pengaturcaraan berstruktur

Example – a robot named Didi. Didi can understand only a specific number of instructions / commands : walk, turn, sit Walk – takes one complete step forward Turn – turns 180 degree Sit – sits down

Assume that Didi is facing a chair that is two steps away from him.

Write the instructions using only commands that Didi understands, that direct Didi to sit in the chair.

Page 7: Pengenalan kepada pengaturcaraan berstruktur

1. Walk

2. Walk

3. Turn

4. Sit 2 steps

algorithm

A set of step-by-step instructions that

accomplish a task

Page 8: Pengenalan kepada pengaturcaraan berstruktur

The repetition structure

Example – shampoo bottles typically include repetition structure in the directions for washing your hair. Those direction usually tell you to repeat the “apply shampoo to hair”, “lather”, and “rinse” steps until your hair is clean.

Repetition structure referred to as loop, directs the computer to repeat one or more instructions until some condition is met, at which time the computer should stop repeating the instructions.

Page 9: Pengenalan kepada pengaturcaraan berstruktur

Example : Didi is facing a chair that is 50 steps away from him. Write the algorithm that directs Didi to sit in the chair.

Extra command : repeat x times Solution:

Write “walk” instruction 50 times Use the “repeat 50 times.”

1. repeat 50 times:

walk

2. turn

3. sit

Page 10: Pengenalan kepada pengaturcaraan berstruktur

The selection structure

Also called decision structure Makes a decision, and then takes appropriate

action based on that decision Example: you drive your car and approach an

intersection, whether you need to stop or proceed.

Page 11: Pengenalan kepada pengaturcaraan berstruktur

Example

Example: Didi is holding either a red or yellow balloon and that he is facing two boxes. One of the box is colored yellow and the other is colored red. The two boxes are located 20 steps away from Didi. Your task is have Didi to drop the balloon into the appropriate box. The yellow balloon belongs to the yellow box and the red balloon belongs to the red box. After Didi drops the balloon you should return him to the original position.

You may add additional instructions to Didi’s instruction set

Page 12: Pengenalan kepada pengaturcaraan berstruktur

The algorithm

1. repeat 20 times:walk

2. if the balloon is red, do this:drop the balloon in the red box

otherwisedrop the balloon in the yellow box

3. turn4. repeat 20 times

walk5. turn

Page 13: Pengenalan kepada pengaturcaraan berstruktur

Calculate the bonus by multiplying the salary 1% Calculate the bonus by multiplying the salary 2% If the years employed are greater than or equal to 5,

do this: If the years employed are less than 5, do this: Otherwise, do this: Print the bonus Read the salary and years employed Repeat for each employee:

Page 14: Pengenalan kepada pengaturcaraan berstruktur

Exercise Assume a company pays an annual bonus to

its employees. The bonus is based on the number of years the employee has been with the company. Employees working at the company for less than 5 years receive a 1% bonus, all others receive a 2% bonus. Write an algorithm that prints each employee’s bonus. Use only the instructions given:

Page 15: Pengenalan kepada pengaturcaraan berstruktur

Answer??

Page 16: Pengenalan kepada pengaturcaraan berstruktur

Problem Solving

Problem solving involves: Analysis Algorithm or pseudo code or flow chart or

combination of them.

Page 17: Pengenalan kepada pengaturcaraan berstruktur

Problems Analysis

Identify problems: Input Output Process

INPUT PROCESS OUTPUT

Page 18: Pengenalan kepada pengaturcaraan berstruktur

Problem

Write a program obtain average of 3 numbers given by a user. Display the 3 numbers and the average of the numbers.

Page 19: Pengenalan kepada pengaturcaraan berstruktur

Problems Analysis

Input : 3 numbers Process : 1. Add 3 numbers

2. Divide sum of the numbers by 3

Output : Print 3 numbers & average

Page 20: Pengenalan kepada pengaturcaraan berstruktur

Algorithm

Algorithm Any computing problem can be done by executing

a series of actions in a specific order. 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 is called an algorithm.

Is a logical solution that is inline with our daily language or mother tongue language.

Page 21: Pengenalan kepada pengaturcaraan berstruktur

Algorithm

Start

1. Set sum = 0 and average = 0

2. Read 3 numbers: nom1, nom2, nom3

3. Add 3 numbers

4. Calculate the average, average = (sum)/3

5. Print 3 numbers(nom1, nom2, nom3) and the average

End

Page 22: Pengenalan kepada pengaturcaraan berstruktur

Pseudo code

Pseudo code Is an artificial and informal language Usually it is use English languageQuite similar to programming languageThe purpose of pseudo code is to make

humans who do not understand computer programming can easily understand the flow of the problem solving.

Page 23: Pengenalan kepada pengaturcaraan berstruktur

Pseudo code

START

SET sum = 0, average = 0

INPUT nom1, nom2, nom3

sum = nom1 + nom2 + nom3

average = sum / 3

PRINT nom1, nom2, nom3

PRINT average

END

Page 24: Pengenalan kepada pengaturcaraan berstruktur

Flow Chart

Flow Chart It is represents by using geometry shapes with

connected line Use a standard symbol

Page 25: Pengenalan kepada pengaturcaraan berstruktur

Flow Chart

TERMINAL

Indicates the beginning or end of an algorithm

PROCESS

Indicates an input computational or data manipulation.

INPUT / OUTPUT

Indicates an input or output operation

Page 26: Pengenalan kepada pengaturcaraan berstruktur

Flow ChartDECISION

Indicates a decision point in the algorithm

CONNECTOR

Indicates an entry to or exit from another part of the flowchart

FLOW LINES

Used to connect the flowchart symbols and indicate the logic flow

LOOP

Indicates the initial, final and increment values of a loop

Page 27: Pengenalan kepada pengaturcaraan berstruktur

Example of a flow chartSTART

END

sum = 0, average = 0

Input nom1,nom2, nom3

sum = nom1 + nom2 + nom3

average = sum / 3

print nom1,nom2, nom3print average

Page 28: Pengenalan kepada pengaturcaraan berstruktur

Exercise

Write an algorithm and a flow chart to calculate and display a volume of a sphere.

Volume = 4/3 x pi x radius x radius , where pi = 3.41

Page 29: Pengenalan kepada pengaturcaraan berstruktur

Solution

Analyze the problem Input :

Process :

Output :

radius

Calculate volume of a sphereVolume = 4/3 x pi x radius x radius

print volume of a sphere

Page 30: Pengenalan kepada pengaturcaraan berstruktur

Solution (algorithm)

Start

1. Input radius

2. Calculate volume of a sphere

Volume = 4/3 x pi x radius x radius

3. print volume of a sphere

End

Page 31: Pengenalan kepada pengaturcaraan berstruktur

Solution (Flow Chart)

START

END

pi = 3.41

Input radius

Volume = 4/3 x pi x radius x radius

Print volume

Page 32: Pengenalan kepada pengaturcaraan berstruktur

Control Structures Flow Chart

3 types of control structures: Sequence structure Selection structure

If If… else

Repetition structure For While Do… while

Page 33: Pengenalan kepada pengaturcaraan berstruktur

Flow Chart for Selection Structure 1

Pseudo code

if condition

statement 1Condition / Boolean

operatorStatement 1 (inside if

structure)

True

False

Example Pseudo code

if student’s grade is greater than or equal to 60

Print “Passed”

Page 34: Pengenalan kepada pengaturcaraan berstruktur

Flow Chart for Selection Structure 2

Condition / Boolean operator

true

false

Statement 2 Statement 2

Pseudo code

if condition

statement 1

Else

statement 2

Example Pseudo code

if student’s grade is greater than or equal to 60

Print “Passed”

Else

Print “Failed”

Page 35: Pengenalan kepada pengaturcaraan berstruktur

Flow Chart for Repetition Structure

Condition / Boolean operator

Statement 1 (inside if structure)

True

False

Example Pseudo code

While there are more items on the shopping list

Purchase next item and cross it off my list

Page 36: Pengenalan kepada pengaturcaraan berstruktur

Module Structure

Used for a big and complex problem Divide the problem into smaller problem –

sub module To simplify a problem solving

Module structure Pseudo code

Main module

Call sub module

End of main module

Sub module name

Statements

End of sub module

Main module

Sub module 1

Sub module 2

Page 37: Pengenalan kepada pengaturcaraan berstruktur

TQ