Professional Documents
Culture Documents
Dasar Pemrograman 4
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
required
3. Processing: a list of actions needed
to produce the required outputs.
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.
Defining diagram
Input
Number1
Number2
Number3
Processing
Output
total
Defining diagram
Input
Processing
Output
total
Solution Algorithm
Add_three_numbers
Read number1, number2, number3
Total = number1 + number2 + number3
Print total
END
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.
Defining diagram
Input
Max_temp
Min_temp
Processing
Output
Avg_temp
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.
Defining diagram
Input
Max_temp
Min_temp
Processing
Prompt for temperatures
Get temperatures
Calculate average temperature
Display average temperature
Output
Avg_temp
Solution Algorithm
Find average_temperature
Prompt operator for max_temp,
min_temp
Get max_temp, min_temp
Avg_temp= (max_Temp + min_temp)/2
Output avg_temp to the screen
END
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
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.
Defining diagram
Input
Block_lenght
Block_width
House_lenght
House_width
Processing
Output
Mowing_time
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.
Defining diagram
Input
Block_lenght
Block_width
House_lenght
House_width
Processing
Prompt for block measurements
Get block measurements
Prompt for house measurements
Get house measurements
Calculate mowing area
Calculate mowing time
Output
Mowing_time
Solution Algorithm
Calculate_mowing_time
Prompt operator for block_lenght, block_width
Get block_length, block_width
block_area = block_lenght*block_width
Prompt operator for house_lenght, house_width
Get house_lenght, house_width
house_area=house_lenght*house_width
Mowing_area=block_area-house_area
Mowing_time=mowing_area/2
Output mowing_time to screen
END
Desk Checking
Solution Algorithm
Add_three_numbers
Read number1, number2, number3
Total = number1 + number2 + number3
Print total
END
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
Total
Data Set 1
Data Set 2
60
123
number1
number2
number3
10
20
30
total
First Pass
1
2
60
Second
Pass
1
40
41
42
123
Solution Algorithm
Find average_temperature
Prompt operator for max_temp,
min_temp
Get max_temp, min_temp
Avg_temp= (max_Temp + min_temp)/2
Output avg_temp to the screen
END
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
Avg_temp
Data Set 1
Data Set 2
20
30
Max_temp
Min_temp
30
10
Avg_temp
First Pass
1,2
3
20
0utput
Second Pass
1,2
40
20
30
output
Assignment 2:
Desk Checking for
Compute mowing time
Solution Algorithm
Calculate_mowing_time
Prompt operator for block_lenght, block_width
Get block_length, block_width
block_area = block_lenght*block_width
Prompt operator for house_lenght, house_width
Get house_lenght, house_width
house_area=house_lenght*house_width
Mowing_area=block_area-house_area
Mowing_time=mowing_area/2
Output mowing_time to screen
END
END
Assignment 2 Review:
1
2
3
4
5
6
7
8
9
Calculate_mowing_time
Prompt operator for block_lenght, block_width
Get block_length, block_width
block_area = block_lenght*block_width
Prompt operator for house_lenght, house_width
Get house_lenght, house_width
house_area=house_lenght*house_width
Mowing_area=block_area-house_area
Mowing_time=mowing_area/2
Output mowing_time to screen
END
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
2. Expected result:
Mowing_time
Data Set 1
Data Set 2
250 minutes
300 minutes
Block_lenght
Block_width
House_lenght
House_width
Block_are
a
House_area
Mowing_are
a
Mowing_time
First Pass
1,2
30
30
900
4,5
20
20
400
500
250
Output
Second Pass
1,2
40
20
3
4,5
6
7
800
20
10
200
600
300
Output