0% found this document useful (0 votes)
34 views

Notesunit 1

Uploaded by

CHARUMATHE N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Notesunit 1

Uploaded by

CHARUMATHE N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 160

Algorithms

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

Predefined Process Symbol:

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

Number1 Read three numbers total


Number2 Add number together
Number3 Print total number
Solution Algorithm
Start

Read
Number1
Number2
number3

Add numbers to total

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

• Write only one statement per line


• Capitalize initial keyword
• Indent to show hierarchy
• End multiline structures
• Keep statements language independent
One Statement Per Line

Each statement in pseudocode should express


just one action for the computer. If the task list
is properly drawn, then in most cases each task
will correspond to one line of pseudocode.
Task List Pseudocode
Read name, hours worked, rate of pay READ name, hoursWorked, payRate
Perform calculations gross = hoursWorked * payRate
gross = hours worked * rate of pay WRITE name, hoursWorked, gross
Write name, hours worked, gross
Capitalize Initial Keyword

In the example below note the words: READ and


WRITE. These are just a few of the keywords to use,
others include:

READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE

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

READ name, grossPay, taxes


IF taxes > 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net

See the IF/ELSE/ENDIF as constructed above,


the ENDIF is in line with the IF.

The same applies for WHILE/ENDWHILE etc…


Language Independence

Resist the urge to write in whatever language you are


most comfortable with, in the long run you will save
time. Remember you are describing a logic plan to
develop a program, you are not programming!
The Selection Structure

yes no
amount < 100

interestRate = .06 interestRate = .10

IF amount < 100


interestRate = .06
Pseudocode ➔ ELSE
Interest Rate = .10
ENDIF
The Looping Structure

In flowcharting one of the more confusing


things is to separate selection from looping.
This is because each structure use the diamond
as their control symbol. In pseudocode we
avoid this by using specific keywords to
designate looping
WHILE/ENDWHILE
REPEAT/UNTIL
WHILE / ENDWHILE
Start count = 0
WHILE count < 10
ADD 1 to count
count = 0
WRITE count
ENDWHILE
WRITE “The End”

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

Flowchart Advantages: Pseudocode Advantages


✓ Standardized ✓ Easily modified
✓ Visual ✓ Implements structured
concepts
✓ Done easily on Word Processor

Flowchart Disadvantages: Pseudocode Disadvantages:


✓ Hard to modify ✓ Not visual
✓ Structured design elements not ✓ No accepted standard, varies from
implemented company to company
✓ Special software required
Access of Data

The READ statement tells the computer to get a value from an input device and
store it in a memory location.

How to deal with memory locations?

Memory locations are identified by their addresses, we give them names


(field names / variable names) using words descriptive to us such as ctr as
opposed to a location addresses such as 19087.
Rules for Variable Names

• Begin with lowercase letter


• Contain no spaces
• Additional words begin with capital
• Unique names within code
• Consistent use of names
Working with Fields

Calculations Selection

+ add > greater than


- subtract < less than
* multiply = equal to
/ divide >= greater than or equal to
** or ^ exponentiation <= less than or equal to
() grouping <> not equal to

You might also like