Notesunit 1
Notesunit 1
8.1
CONCEPT
Figure 8-1
Informal definition of an algorithm
used in a computer
Figure 8-3
Defining actions in FindLargest algorithm
8.2
THREE CONSTRUCTS
Figure 8-6
Three constructs
8.3
ALGORITHM
REPRESENTATION
Average of two
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Pass/no pass Grade
Pass/NoPassGrade
Input: One number
1. if (the number is greater than or equal to 70)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “nopass”
End if
2. Return the grade
End
Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
Flowcharts
Symbol
Terminal Symbol:
indicates the starting or stopping pointin the logic.
Input/Output Symbol:
Represents an input or output process in an algorithm
Process Symbol:
Represents any single process in an algorithm
Decision Symbol:
Represents a decision in the logic involving the comparison
Of two values.
The three basic control
structures
1. Sequence
Statemement a
Statemement b
Statemement c
2. Selection
T F
Condition p?
Statemement a Statemement b
3. Repetition
F
Condition p?
Statemement block
Example 12.1 Add three numbers
▪ A program is required to read
three numbers, add them together
and print their total.
• Defining diagram
Input Processing Output
Read
Number1
Number2
number3
Print total
Stop
Flowchart and the selection
control structure
Nested IF statement
T F
Record
Code =`A‘ ?
F
T
Record
Increment Code =`A‘ ?
Counter_A
F
Record
Code =`A‘ ?
Increment T
Counter_B
Increment
Increment Error_counter
Counter_C
Control Structures required
1. An array to store the exam scores –
called ´scores´
2. An index to identify each element in the
array
3. A DO loop to accept the scores
4. Another DO loop to display the scores to
the screen.
Solution Algorithm
Start
Calculate Display
Total_score = average
zero average
I=1
I=1
Stop
Prompt and
Display
get
Scores (I)
Scores (I)
Add scores(I)
to I=I+1
Total score
T
I=I+1 I <= 18 ?
F
T
I <= 18 ?
F
Pseudocode
An Introduction
Flowcharts were the first design tool to be
widely used, but unfortunately they do not
reflect some of the concepts of structured
programming very well. Pseudocode, on the
other hand, is a newer tool and has features
that make it more reflective of the structured
concepts. The drawback is that the narrative
presentation is not as easy to understand
and/or follow.
Rules for Pseudocode
Pseudocode
READ name, hoursWorked, payRate
gross = hoursWorked * payRate
WRITE name, hoursWorked, gross
Indent to Show Hierarchy
Each design structure uses a particular indentation pattern
• Sequence:
Keep statements in sequence all starting in the same column
• Selection:
Indent statements that fall inside selection structure, but not the keywords that form the
selection
• Loop:
Indent statements that fall inside the loop but not keywords that form the loop
READ name, grossPay, taxes
IF taxes > 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net
End Multiline Structures
yes no
amount < 100
count
<10 Mainline Modular
count = 0
Write
WHILE count < 10
add 1 to “The End”
count DO Process
ENDWHILE
Stop
write count WRITE “The End”
Process
ADD 1 to count
WRITE count
REPEAT / UNTIL
Start count = 0
REPEAT
count = 0
ADD 1 to count
WRITE count
UNTIL count >= 10
add 1 to WRITE “The End”
count
Mainline Modular
write count count = 0
REPEAT
DO Process
count
<10 UNTIL count >= 10
WRITE “The End”
Write Process
“The End”
ADD 1 to count
Stop WRITE count
Advantages & Disadvantages
The READ statement tells the computer to get a value from an input device and
store it in a memory location.
Calculations Selection