CSC103: Introduction to Computer and Programming Lecture 3.

55
CSC103: Introduction to Computer and Programming Lecture 3

Transcript of CSC103: Introduction to Computer and Programming Lecture 3.

Page 1: CSC103: Introduction to Computer and Programming Lecture 3.

CSC103: Introduction to Computer and Programming

Lecture 3

Page 2: CSC103: Introduction to Computer and Programming Lecture 3.

What is a Flowchart?

• A flowchart is a diagram that depicts the “flow” of a program.

• The figure shown here is a flowchart for the pay-calculating program

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Page 3: CSC103: Introduction to Computer and Programming Lecture 3.

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Basic Flowchart Symbols

• Notice there are three types of symbols in this flowchart:– rounded rectangles– parallelograms– a rectangle

• Each symbol represents a different type of operation.

Rounded Rectangle

Parallelogram

Rectangle

Rounded Rectangle

Page 4: CSC103: Introduction to Computer and Programming Lecture 3.

Basic Flowchart Symbols

• Terminals– represented by rounded

rectangles– indicate a starting or

ending point

Terminal

START

END

Terminal

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Page 5: CSC103: Introduction to Computer and Programming Lecture 3.

Basic Flowchart Symbols

• Input/Output Operations– represented by

parallelograms– indicate an input or

output operation

Display message “How many

hours did you work?”

Read Hours

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Input/output operations

Page 6: CSC103: Introduction to Computer and Programming Lecture 3.

Basic Flowchart Symbols

• Processes– represented by rectangles– indicates a process such as

a mathematical computation or variable assignment

Multiply Hours by Pay Rate.

Store result in Gross Pay.

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Process

Page 7: CSC103: Introduction to Computer and Programming Lecture 3.

Variable Contents:

Stepping Through the Flowchart

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Output operation

HoursPay RateGross Pay

Page 8: CSC103: Introduction to Computer and Programming Lecture 3.

Input Operation

HoursPay RateGross Pay

40

Variable Contents:

Stepping Through the Flowchart

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Page 9: CSC103: Introduction to Computer and Programming Lecture 3.

Stepping Through the Flowchart

HoursPay RateGross Pay

40

Variable Contents:

Output operation

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Page 10: CSC103: Introduction to Computer and Programming Lecture 3.

Stepping Through the Flowchart

HoursPay RateGross Pay

40

15

Variable Contents:

Input Operation

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Page 11: CSC103: Introduction to Computer and Programming Lecture 3.

Stepping Through the Flowchart

HoursPay RateGross Pay

40

15

600

Variable Contents:Process

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Page 12: CSC103: Introduction to Computer and Programming Lecture 3.

Stepping Through the Flowchart

Output operation

HoursPay RateGross Pay

40

15

600

Variable Contents:

START

Display message “How many hours

did you work?”

Input Hours

Display message “How much do you get paid

per hour?”

Input Pay Rate

Multiply Hours by Pay Rate. Store

result in Gross Pay.

Display Gross Pay

END

Page 13: CSC103: Introduction to Computer and Programming Lecture 3.

Four Flowchart Structures

• Sequence• Decision• Repetition• Case

Page 14: CSC103: Introduction to Computer and Programming Lecture 3.

Sequence Structure

• A series of actions are performed in sequence• The pay-calculating example was a sequence

flowchart.

Page 15: CSC103: Introduction to Computer and Programming Lecture 3.

• if ... then• if ... then ... else• One of two possible actions is taken, depending on a

condition.

TrueFalse

TrueFalse

Decision Structure

Page 16: CSC103: Introduction to Computer and Programming Lecture 3.

• A new symbol, the diamond, indicates a yes/no question.

• If the answer to the question is yes, the flow follows one path.

• If the answer is no, the flow follows another path

Decision Structure

TrueFalse

Page 17: CSC103: Introduction to Computer and Programming Lecture 3.

• In the flowchart segment below, the question “is x < y?” is asked.

• If the answer is no, then process A is performed. • If the answer is yes, then process B is performed.

Decision Structure

x<y?

Process B

TrueFalse

Process A

Page 18: CSC103: Introduction to Computer and Programming Lecture 3.

• The flowchart segment below shows a decision structure with only one action to perform. It is expressed as an if statement in C code.

if (x < y)

a = x * 2;

C Code

Decision Structure

Flowchart

Calculate a as x times 2

TrueFalsex < y?

Page 19: CSC103: Introduction to Computer and Programming Lecture 3.

• The flowchart segment below shows how a decision structure is expressed in C as an if/else statement.

Calculate a as x plus y

Calculate a as x times 2

TrueFalsex < y?

if (x < y)

a = x * 2;

else

a = x + y;

FlowchartC Code

Decision Structure

Page 20: CSC103: Introduction to Computer and Programming Lecture 3.

• Formula 1 car race• There is a path/track• Each car has to complete a

certain no of rounds say 10• In each round, when a car cross

the finish line– the condition is check whether the

car has completed total no of round or not.

Repetition - Example

Page 21: CSC103: Introduction to Computer and Programming Lecture 3.

• A repetition structure represents part of the program that repeats. This type of structure is commonly known as a loop.

Repetition Structure

Page 22: CSC103: Introduction to Computer and Programming Lecture 3.

• Notice the use of the diamond symbol. • A loop tests a condition, and if the condition exists, it

performs an action. • Then it tests the condition again. • If the condition still exists, the action is repeated. • This continues until the condition no longer exists.

Repetition Structure

Page 23: CSC103: Introduction to Computer and Programming Lecture 3.

• In the flowchart segment, the question “is x < y?” is asked• If the answer is yes, then Process A is performed• The question “is x < y?” is asked again• Process A is repeated as long as x is less than y• When x is no longer less than y, the repetition stops and the

structure is exited.

Repetition Structure

x < y ? Process Atrue

false

Page 24: CSC103: Introduction to Computer and Programming Lecture 3.

• The flowchart segment below shows a repetition structure expressed in C as a while loop.

while (x < y)

x = x+1;

Flowchart C Code

Repetition Structure

x < y ? Add 1 in xtrue

false

Page 25: CSC103: Introduction to Computer and Programming Lecture 3.

• The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created

• In this flowchart segment, x is never changed. Once the loop starts, it will never end

• QUESTION: How can thisflowchart be modified soit is no longer an infiniteloop?

Controlling a Repetition Structure

x < y ?true

false

Display x

Page 26: CSC103: Introduction to Computer and Programming Lecture 3.

• ANSWER: By adding an action within the repetition that changes the value of x.

Controlling a Repetition Structure

x < y ?true

false

Display x Add 1 in x

Page 27: CSC103: Introduction to Computer and Programming Lecture 3.

• This type of structure is known as a pre-test repetition structure.

• The condition is tested BEFORE any actions are performed.

A Pre-Test Repetition Structure

x < y ?true

false

Display x Add 1 in x

Page 28: CSC103: Introduction to Computer and Programming Lecture 3.

• In a pre-test repetition structure, if the condition is false, the loop will never begin.

A Pre-Test Repetition Structure

x < y ?true

false

Display x Add 1 in x

Page 29: CSC103: Introduction to Computer and Programming Lecture 3.

• This flowchart segment shows a post-testrepetition structure.

• The condition is tested AFTER the actionsare performed.

• A post-test repetition structure alwaysperforms its actions at least once.

A Post-Test Repetition Structure

Display x

Add 1 to x

YESx < y?

Page 30: CSC103: Introduction to Computer and Programming Lecture 3.

• The flowchart segment below shows a post-test repetition structure expressed in C as a do-while loop.

do{

printf(“%d”, x);x = x +1;

} while (x < y);

FlowchartC Code

A Post-Test Repetition Structure

Display x

Add 1 to x

YESx < y?

Page 31: CSC103: Introduction to Computer and Programming Lecture 3.

• One of several possible actions is taken, depending on the contents of a variable.

Case structure

Page 32: CSC103: Introduction to Computer and Programming Lecture 3.

• The structure below indicates actions to perform depending on the value in years_employed.

CASE:years_emplo

yed

1 2 3 Other

bonus = 100 bonus = 200 bonus = 400 bonus = 800

Case structure

Page 33: CSC103: Introduction to Computer and Programming Lecture 3.

If years_employed = 1, bonus is set to 100

If years_employed = 2, bonus is set to 200 If years_employed =

3, bonus is set to 400

If years_employed is any other value, bonus is set to 800

Case structure

CASE:years_emplo

yed

1 2 3 Other

bonus = 100 bonus = 200 bonus = 400 bonus = 800

Page 34: CSC103: Introduction to Computer and Programming Lecture 3.

• Sometimes a flowchart will not fit on one page.

• A connector (represented by a small circle) allows you to connect two flowchart segments.

A

Connectors

Page 35: CSC103: Introduction to Computer and Programming Lecture 3.

A

Start

A

End

The “A” connector indicates that the second flowchart segment begins where the first segment ends.

Connectors

Page 36: CSC103: Introduction to Computer and Programming Lecture 3.

• Structures are commonly combined to create more complex algorithms

• The flowchart segment below combines a decision structure with a sequence structure

x < y? Display x Add 1 to xYES

Combining Structures

Page 37: CSC103: Introduction to Computer and Programming Lecture 3.

• This flowchart segment shows two decision structures combined

Display “x is within limits.”

TrueFalse x > min?

x < max?Display “x is outside

the limits.” TrueFalse

Display “x is outside the limits.”

Combining Structures

Page 38: CSC103: Introduction to Computer and Programming Lecture 3.

• What do each of the following symbols represent?

(Answer on next slide)

Review

Page 39: CSC103: Introduction to Computer and Programming Lecture 3.

• What do each of the following symbols represent?

Terminal

Input/Output Operation

Process

Decision

Connector

Answer

Page 40: CSC103: Introduction to Computer and Programming Lecture 3.

• Name the four flowchart structures.

(Answer on next slide)

Review

Page 41: CSC103: Introduction to Computer and Programming Lecture 3.

• Sequence• Decision• Repetition• Case

Answer

Page 42: CSC103: Introduction to Computer and Programming Lecture 3.

• What type of structure is this?

(Answer on next slide)

Review

Page 43: CSC103: Introduction to Computer and Programming Lecture 3.

• Repetition

Answer

Page 44: CSC103: Introduction to Computer and Programming Lecture 3.

• What type of structure is this?

(Answer on next slide)

Review

Page 45: CSC103: Introduction to Computer and Programming Lecture 3.

• Sequence

Answer

Page 46: CSC103: Introduction to Computer and Programming Lecture 3.

• What type of structure is this?

(Answer on next slide)

Review

Page 47: CSC103: Introduction to Computer and Programming Lecture 3.

• Case

Answer

Page 48: CSC103: Introduction to Computer and Programming Lecture 3.

• What type of structure is this?

(Answer on next slide)

Review

Page 49: CSC103: Introduction to Computer and Programming Lecture 3.

• Decision

Answer

Page 50: CSC103: Introduction to Computer and Programming Lecture 3.

Flow chart examples

Page 51: CSC103: Introduction to Computer and Programming Lecture 3.

Make a flow chart for the process of crossing the street.

Start

Look on your left and right side

If a vehicle

near

Wait for few seconds

Cross the street

End

No

Yes

Page 52: CSC103: Introduction to Computer and Programming Lecture 3.

Make a flowchart to fill the bath tub with water

Start

End

Turn on hot and cold water

Too hot or cold

Adjust hot and cold taps

Wait for 2 minutes

Is the bath full

A

A

Adjust hot and cold taps

Y

N

N

Y

1. Turn on the hot and cold taps.

2. Is it too hot or cold? If it is, go to step 3, otherwise go to step 4.

3. Adjust the hot and cold taps and go back to step 2.

4. Wait for 2 minutes.5. Is the bath tub full? If it is,

go to step 7, otherwise go to step 4.

6. Turn off the hot and cold taps.

Page 53: CSC103: Introduction to Computer and Programming Lecture 3.
Page 54: CSC103: Introduction to Computer and Programming Lecture 3.
Page 55: CSC103: Introduction to Computer and Programming Lecture 3.