Dasar Pemrograman 4

44
Logic and Algorithm

description

nsns

Transcript of Dasar Pemrograman 4

Page 1: Dasar Pemrograman 4

Logic and Algorithm

Page 2: Dasar Pemrograman 4

Developing an algorithm

• To help the initial analysis, the problem should be divided into 3 separate components:

1. Input: a list of the source data provided to the problem

2. Output: a list of the outputs required3. Processing: a list of actions needed to

produce the required outputs.

Page 3: Dasar Pemrograman 4

Example 1. Add three numbers

A program is required to read three numbers, add them together and print their total.

Page 4: Dasar Pemrograman 4

Solution:

1. Underline the nouns and adjectives used in the specification establish the input, output component and any object that are required.

A program is required to read three numbers, add them together and print their total.

Page 5: Dasar Pemrograman 4

• Defining diagram

Input Processing Output

Number1Number2Number3

total

Page 6: Dasar Pemrograman 4

2. Underline the verbs and adverbs used in the specification establish the action required.

A program is required to read three numbers, add them together and print their total.

Page 7: Dasar Pemrograman 4

• Defining diagram

Input Processing Output

Number1Number2Number3

Read three numbersAdd numbers togetherPrint total number

total

Page 8: Dasar Pemrograman 4

3. Writing down the processing steps in an algorithm,

Read three numbersAdd numbers togetherPrint total number

Page 9: Dasar Pemrograman 4

Solution Algorithm

• Add_three_numbersRead number1, number2, number3Total = number1 + number2 + number3Print total

END

Page 10: Dasar Pemrograman 4

Example 2. Find average temperature• A program is required to prompt the

terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Page 11: Dasar Pemrograman 4

Step 1• A program is required to prompt the

terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Page 12: Dasar Pemrograman 4

• Defining diagram

Input Processing OutputMax_tempMin_temp

Avg_temp

Page 13: Dasar Pemrograman 4

Step 2• A program is required to prompt the

terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Page 14: Dasar Pemrograman 4

• Defining diagram

Input Processing OutputMax_tempMin_temp

Prompt for temperaturesGet temperaturesCalculate average temperatureDisplay average temperature

Avg_temp

Page 15: Dasar Pemrograman 4

Solution Algorithm

• Find average_temperaturePrompt operator for max_temp, min_tempGet max_temp, min_tempAvg_temp= (max_Temp + min_temp)/2Output avg_temp to the screen

END

Page 16: Dasar Pemrograman 4

Example 3. Compute Mowing Time A program is required to read from the

screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute

Page 17: Dasar Pemrograman 4

Step 1• A program is required to read from the

screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.

Page 18: Dasar Pemrograman 4

• Defining diagram

Input Processing Output

Block_lenghtBlock_widthHouse_lenghtHouse_width

Mowing_time

Page 19: Dasar Pemrograman 4

Step 2• A program is required to read from the

screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.

Page 20: Dasar Pemrograman 4

• Defining diagram

Input Processing OutputBlock_lenghtBlock_widthHouse_lenghtHouse_width

Prompt for block measurementsGet block measurementsPrompt for house measurementsGet house measurementsCalculate mowing areaCalculate mowing time

Mowing_time

Page 21: Dasar Pemrograman 4

Solution AlgorithmCalculate_mowing_time

Prompt operator for block_lenght, block_widthGet block_length, block_widthblock_area = block_lenght*block_widthPrompt operator for house_lenght, house_widthGet house_lenght, house_widthhouse_area=house_lenght*house_widthMowing_area=block_area-house_areaMowing_time=mowing_area/2Output mowing_time to screen

END

Page 22: Dasar Pemrograman 4

Desk Checking

Page 23: Dasar Pemrograman 4

Checking the solution algorithm(Desk Checking)

• Tracing through the logic of the algorithm with some chosen data..

Page 24: Dasar Pemrograman 4

Step in desk Checking an algorithm

1. Choose valid simple input test case (2-3 enough)2. Establish what the expected result should be.3. Make a table of relevant variable names4. Checking the test case line by line, step by step5. Repeat process 4 for other test case6. Check if expected result 2 matches with actual

result 5

Page 25: Dasar Pemrograman 4

Example 4. Desk Chek for example 1

A program is required to read three numbers, add them together and print their total.

Page 26: Dasar Pemrograman 4

Solution Algorithm

• Add_three_numbersRead number1, number2, number3Total = number1 + number2 + number3Print total

END

Page 27: Dasar Pemrograman 4

Desk Checking

1. Choose two sets input test data. Set 1: 10,20, 30 and Set 2: 40, 41, 42

Data Set 1 Data Set 2

Number 1 10 40

Number 2 20 41

Number 3 30 42

Page 28: Dasar Pemrograman 4

2. Establish the expected result for each test case

Data Set 1 Data Set 2

Total 60 123

Page 29: Dasar Pemrograman 4

3. Set up a table of relevant variable names, and pass each test data set statement by statement.

Statement number

number1 number2 number3 total

First Pass

1 10 20 30

2 60

3 Print

Second Pass1 40 41 42

2 123

3 Print

Page 30: Dasar Pemrograman 4

4. Check the expected results (60 and 123) match the actual results.

Page 31: Dasar Pemrograman 4

Desk Check of Example 2.

• A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Page 32: Dasar Pemrograman 4

Solution Algorithm

• Find average_temperaturePrompt operator for max_temp, min_tempGet max_temp, min_tempAvg_temp= (max_Temp + min_temp)/2Output avg_temp to the screen

END

Page 33: Dasar Pemrograman 4

Desk Checking

1. Choose two sets input test data. Set 1: 30, 10 and Set 2: 40, 20

Data Set 1 Data Set 2

Max_temp 30 40

Min_temp 10 20

Page 34: Dasar Pemrograman 4

2. Establish the expected result for each test case

Data Set 1 Data Set 2

Avg_temp 20 30

Page 35: Dasar Pemrograman 4

3. Set up a table of relevant variable names, and pass each test data set statement by statement.

Statement number

Max_temp Min_temp Avg_temp

First Pass

1,2 30 10

3 20

4 0utput

Second Pass

1,2 40 20

3 30

4 output

Page 36: Dasar Pemrograman 4

4. Check the expected results match the actual results.

Page 37: Dasar Pemrograman 4

Assignment 2:Desk Checking for

Compute mowing time

• A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.

Page 38: Dasar Pemrograman 4

Solution AlgorithmCalculate_mowing_time

Prompt operator for block_lenght, block_widthGet block_length, block_widthblock_area = block_lenght*block_widthPrompt operator for house_lenght, house_widthGet house_lenght, house_widthhouse_area=house_lenght*house_widthMowing_area=block_area-house_areaMowing_time=mowing_area/2Output mowing_time to screen

END

Page 39: Dasar Pemrograman 4

Assignment 3 – Desk Checking for Mowing_time which now contains a logic error

Calculate_mowing_timePrompt operator for block_lenght, block_widthGet block_length, block_widthblock_area = block_lenght * block_widthPrompt operator for house_lenght, house_widthGet house_lenght, house_widthhouse_area=block_lenght * block_widthMowing_area=block_area - house_areaMowing_time=mowing_area/2Output mowing_time to screen

END

Page 40: Dasar Pemrograman 4

Assignment 2 Review: Calculate_mowing_time1 Prompt operator for block_lenght, block_width2 Get block_length, block_width3 block_area = block_lenght*block_width4 Prompt operator for house_lenght, house_width5 Get house_lenght, house_width6 house_area=house_lenght*house_width7 Mowing_area=block_area-house_area8 Mowing_time=mowing_area/29 Output mowing_time to screen

END

Page 41: Dasar Pemrograman 4

Desk Checking

1. Input data:

Data Set 1 Data Set 2

Block_lenght 30 40

Block_widht 30 20

House_lenght 20 20

House_width 20 10

Page 42: Dasar Pemrograman 4

2. Expected result:

Data Set 1 Data Set 2

Mowing_time 250 minutes 300 minutes

Page 43: Dasar Pemrograman 4

3. Set up a table of relevant variable names, and pass each test data set statement by statement.

Statement number

Block_lenght Block_width House_lenght House_width Block_area

House_area Mowing_area

Mowing_time

First Pass

1,2 30 30

3 900

4,5 20 20

6 400

7 500

8 250

9 Output

Second Pass

1,2 40 20

3 800

4,5 20 10

6 200

7 600

8 300

9 Output

Page 44: Dasar Pemrograman 4

4. Check the expected results match the actual results.