Week02 - Basic Element
Week02 - Basic Element
COMPUTER SCIENCE
0-47
CENTRAL QUESTIONS OF
COMPUTER SCIENCE (CONT’)
0-48
THE CENTRAL ROLE OF ALGORITHMS
IN COMPUTER SCIENCE
0-49
WHAT IS COMPUTER SCIENCE?
The study of algorithms: TERMINOLOGY
• their formal properties
• correctness, limits • Algorithm: A set of steps
that defines how a task is
• efficiency/cost performed
• their hardware realizations • Program: A representation
of an algorithm
• computer design
• Programming: The process
• their linguistic realizations of developing a program
• programming languages • Software: Programs and
algorithms
• their applications
• Hardware: Equipment
• network design, ocean modeling,
bioinformatics, ...
WHAT IS AN ALGORITHM?
… a well-defined procedure that allows an agent to solve a problem.
Example algorithms:
Cooking a dish
Making a peanut-butter jelly sandwich
Shampooing hair
Making a pie
EXAMPLE
Is this an algorithm?
An algorithm must:
Conditional operations
• If batter is too dry, add water
Repeat/looping operations
• Repeat step 1 and 2 three times
• Repeat steps 2,3,4,…10 until batter becomes soft.
EXPRESSING ALGORITHMS
Is natural language good?
• For daily life, yes…but for CS is lacks structure and would be
hard to follow
• Too rich, ambiguous, depends on context
Good compromise
• Simple, readable, no rules, don’t worry about punctuation.
Conditional operations
• Execute an operation if a condition is true
Repeat operations
• Execute a block of operation multiple times until a certain
condition is met
A MODEL FOR VISUALIZING AN
ALGORITHM
Algorithm
Variables
Operations
Examples:
i M
Example of operations
Set the value of i to 3
Set the value of M to i*3 + 12
Set the value of i to i+10
PRIMITIVE OPERATIONS
Get input from user
• Get the value of x from user
Problem: Given any value of radius from the user, compute and
print the circumference of a circle with that radius
Algorithm in pseudocode:
START
Read Hours
END
Rounded
START Rectangle
Display
Read Hours
Display
• parallelograms Multiply
Hours by Pay
Rectangle Rate. Store
• a rectangle result in
Gross Pay.
Display
Read Hours
Multiply Hours
by Pay Rate.
START Store result in
Gross Pay.
Display Gross
Pay
END Terminal
END
START
Display
Read Hours
Display message
• represented by parallelograms
Read Pay Rate
• indicate an input or output operation
Multiply Hours by
Pay Rate. Store
Display result in Gross
Pay.
message “How
Read Hours
many hours Display Gross
Pay
did you work?”
END
START
Display
Read Hours
Display message
• represented by rectangles
Read Pay Rate
• indicates a process such as a
mathematical computation or variable Multiply Hours
by Pay Rate.
assignment Process Store result in
Gross Pay.
Multiply Hours
Display Gross
by Pay Rate. Pay
Store result in
END
Gross Pay.
START
the Flowchart
many hours did
you work?”
Read Hours
Display message
“How much do
How many you get paid per
hour?”
hours did
you work?
Read Pay Rate
Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Variable Contents:
Hours: ? Display Gross
Pay
Pay Rate: ?
Gross Pay: ? END
START
Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Pay Rate: ?
END
Gross Pay: ?
START
Read Hours
How much do
you get paid Display message
“How much do
per hour? Output you get paid per
Operation hour?”
Multiply Hours by
Pay Rate. Store
result in Gross
Variable Contents: Pay.
Hours: 40
Display Gross
Pay Rate: ? Pay
Gross Pay: ?
END
START
Read Hours
How much do
Display
you get paid message “How
per hour? much do you
get paid per
20 hour?”
Read Hours
How much
do you get Display message
“How much do
paid per you get paid per
hour? hour?”
Multiply Hours
Process: The by Pay Rate.
Store result in
product of 40
Gross Pay.
times 20 is
Variable Contents: stored in
Hours: 40 Gross Pay Display Gross
Pay
Pay Rate: 20
Gross Pay: 800 END
START
Read Hours
Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Variable Contents:
Hours: 40 Output Display Gross
Pay Rate: 20 Operation Pay
Sequence
Decision
Repetition
Case
SEQUENCE STRUCTURE
if (x < y)
The flowchart segment a = x * 2;
below shows how a Flowchart
else
decision structure is
a = x + y;
expressed in C++ as an
NO YES
if/else statement. x < y?
Calculate a Calculate a
as x plus y. as x times 2.
DECISION STRUCTURE
C++ Code
if (x < y)
The flowchart segment Flowchart
a = x * 2;
below shows a decision
structure with only one
NO YES
action to perform. x < y?
It is expressed as an if Calculate a
statement in C++ code. as x times 2.
REPETITION STRUCTURE
while (x < y)
YES x++;
x < y? Add 1 to x
CONTROLLING A REPETITION
STRUCTURE
YES
x < y? Display x Add 1 to x
A PRE-TEST REPETITION
STRUCTURE
YES
x < y? Display x Add 1 to x
A POST-TEST REPETITION
STRUCTURE
Display x
This flowchart segment shows a post-test
repetition structure.
The condition is tested AFTER the actions Add 1 to
are performed. x
The
flowchart
segment C++ Code
Display x
below shows do
a post-test {
repetition Flowchart cout << x << endl;
Add 1 to x++;
structure x } while (x < y);
expressed in
C++ as a do- YES
x < y?
while loop.
CASE STRUCTURE
If years_employed = If years_employed =
2, bonus is set to 200 3, bonus is set to 400
If years_employed = If years_employed is
CASE
1, bonus is set to 100 years_employed any other value,
bonus is set to 800
1 2 3 Other
YES
x < y? Display x Add 1 to x
COMBINING STRUCTURES
Display “x is NO YES
outside the limits.”
x < max?
Display “x is Display “x is
outside the limits.” within limits.”
SUMMARY
1) Flowchart 2) Pseudocode
Start/End Begin/End (not necessary)
Input/Output Read/Print : read x,y
Process [An equation] : x=y+6
Comput: compute x as y+6
Decision If/ if-else / While
Predefined process [function_name()]: average(x,y)
Line Not Applicable
Connector Not Applicable
HOMEWORK