You are on page 1of 26

2.

Introduction to Algorithm and Programming

Algoritma dan Pemrograman Teknik Informatika UK Petra 2009


1

Flowcharts and Pseudo Code


A Standard way of describing an algorithm must exist if we expect our solution to be understood by others easily There are two standards in programming:

FLOWCHARTS PSEUDOCODE

Flowcharts
A Flowchart is a Visual Representation of an algorithm A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data Flowcharts aid in breaking down a problem into simple steps

Pseudo Code
Pseudo means pretend or false Pseudo Code is pretend or false computer code; generic English-like terms that are somewhat like computer code Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does

The Three Computer Programming Constructs

Any problem, regardless of how complex, can be broken down into three basic CONSTRUCTS SEQUENCE SELECTION ITERATION

Sequence
Sequence is the most basic of the constructs It is simply performing one step after another Each step is followed in a specific sequence, hence the name Sequence can be thought of as do this, then do this, then do this

Selection
Selection is the decision-making construct It is used to make yes/no or true/false decisions logically Selection can be thought of as if something is true take this action, otherwise take that action

Iteration
Iteration comes from the word reiterate, which means to repeat Iteration is a looping construct Iteration is a combination of decision and sequence and can repeat steps Iteration can be thought of as while something is true, do this, otherwise stop

Visualizing The Constructs


By using Flowchart symbols, we can visualize the three basic computer constructs Every algorithm ever created is made up of only these three constructs

Basic Flowchart Symbols


Terminator Process
Input/Output

Connector
Decision Data Flow Initialization
10

The Basic Flowchart Symbols


The terminator shows where a specific piece of an algorithm beings and ends A process is a basic action or actions on data A decision is a logical test with two possible data paths resulting from one

11

The Basic Flowchart Symbols


Input/Output gets data or displays information to the user of the algorithm A connector connects two or more data flows into one A data flow shows the direction of datas movement through the algorithm

12

Sequence

13

Selection
True

False

14

Selection

Single Selection (if)


If

Won lottery ?

True

youve won the lottery: raise your hand

False

Raise hand

Double Selection (if-else)


If

youre happy: smile else: frown

Happy ?

True

False

Frown

Smile

15

Selection (continued)
Multiple Selection (switch) If the light is ... red -> stop green -> go yellow -> slow down

Light red ? True Stop

False

Light green ?

True

Go

False

Light yellow ?

True

Slow down

False

16

Iteration/ Repetition

True

False

17

Iteration/ Repetition

While

Mixture clumpy ?

True

stir

while its still clumpy Stir the mixture

False

Do-while

ask parents if must eat


Ask Must I eat veggie ? True

vegetables

while parents say Yes


Parents say Yes

False
18

Iteration/ Repetition

For
Counter = 1

Teaching a baby to count from 1 to 10:

counter = 1
Add 1 to Counter

if counter <= 10: increment counter

print counter number


Counter <= 10 ? True Print Counter

False

19

Contoh : Arnie sangat menyukai jogging pagi. Selama ia jogging, ia menghitung berapa langkah yang ditempuh selama menit pertama dan menit terakhir. Kemudian Arnie menghitung rata-rata dari menit pertama dan terakhir kemudian menganggap bahwa rata-rata ini dapat mewakili rata-rata langkah setiap menitnya. Buat program yang menerima rata-rata langkah yang dibuat setiap menitnya dan total waktu yang ditempuh Arnie untuk jogging dalam jam dan menit, lalu menampilkan jarak yang ditempuh Arnie dalam mil. Asumsi 1 langkah yang dibuat Arnie adalah 2,5 feet. (1mil sama dengan 5280 feet).

20

Jawab : Baca problem


Buat program untuk menghitung jarak yang ditempuh Arnie (dalam mil) sewaktu ia lari pagi, jika rata-rata banyaknya langkah yang dibuat dalam tiap menit dan waktu yang diperlukan untuk joging (dalam jam dan menit) diinput. Asumsi 1 langkah = 2.5 feet dan 1 mil = 5280 feet.

Analisis Input : rata-rata langkah yang dibuat dalam 1 menit, waktu joging dalam jam dan menit. Output : Jarak yang ditempuh sewaktu joging (mil)
21

Jawab : Baca problem


Buat program untuk menghitung jarak yang ditempuh Arnie (dalam mil) sewaktu ia lari pagi, jika rata-rata banyaknya langkah yang dibuat dalam tiap menit dan waktu yang diperlukan untuk joging (dalam jam dan menit) diinput. Asumsi 1 langkah = 2.5 feet dan 1 mil = 5280 feet.

Analisis Input : rata-rata langkah yang dibuat dalam 1 menit, waktu joging dalam jam dan menit. Output : Jarak yang ditempuh sewaktu joging (mil)
22

Informasi tambahan : 1 mil = 5280 feet, 1 langkah = 2.5 feet Format output : Perancangan : Baca Rata, Jam, menit Hitung Jarak Jarak = (Rata (60 * jam + menit) 2.5)/5280 Cetak Jarak
23

Start

Baca Jam, menit, Rata

Jarak = (Rata (60* jam + menit) 2.5)/5280

Cetak Jarak

Stop
24

Latihan
1.

2.

Budi sekarang duduk di kelas 2 sekolah dasar AyahBunda. Salah satu pelajaran yang disukai Budi adalah matematika. Pada pokok bahasan belajar perkalian, semua murid kelas 2 mendapat tugas untuk menghitung luas kamar tidur masing-masing. Tolong Anda bantu Budi menghitung luas kamar tidurnya!. Bu Dora menjual mangga dengan harga Rp 500 per buah. Seorang pembeli akan mendapatkan diskon sebesar 10% jika total pembeliannya di atas Rp 50.000. Tampilkan total yang harus dibayar seorang pembeli, besarnya diskon yang didapat dan total yang harus dibayar sesudah dikurangi dengan diskon.
25

3.

4.
5. 6.

Menentukan apakah sebuah bilangan yang diinputkan merupakan bilangan positif atau negatif (asumsi 0 = bilangan positif) Hitung nilai dari n2, jika operator aritmatika yang diperbolehkan adalah penjumlahan. Menghitung jumlah deret : 1 + 2 + 6 + 24 + + (suku ke-(n-1) * suku ke-n) Tampilkan suku-suku dari deret berikut selama jumlahnya masih kurang dari n 1, 2, 4, 8, 16, .
26

You might also like