You are on page 1of 45

CE143:COMPUTER CONCEPTS AND

PROGRAMMING
CHAPTER 1:
INTRODUCTION TO ‘C’ LANGUAGE
( F L OW C HA RT )
P R E PA R E D B Y:
A AY U S H I C H A U D H A R I
A S S I S TA N T P R O F E S S O R

Chandubhai S. Patel Institute of Technology


Algorithms And Flowcharts
Programming task can be divided into two phases:
Problem solving phase
◦ Produce an ordered sequence of steps that describe solution of
problem
◦ This sequence of steps is called an algorithm.
Implementation phase
◦ Implement the program in some programming language

Chandubhai S. Patel Institute of Technology


Problem Solving Phase

Chandubhai S. Patel Institute of Technology


Problem Solving Steps
Firstly, produce a general algorithm (Layman
Language)
Refine the algorithm successively to get step by step
detailed algorithm that is very close to a computer
language(Programming language).

Chandubhai S. Patel Institute of Technology


The Flowchart
•Flowchart shows the sequence of instructions in
a single program.
•Different symbols are used to draw each type of
flowchart.
•Flowchart:
◦ shows logic of an algorithm
◦ emphasizes individual steps and their interconnections

Chandubhai S. Patel Institute of Technology


What is a Flowchart?
A flowchart is a diagram that depicts the
“flow” of a program.
They can range from simple, hand-drawn
charts to comprehensive computer-
drawn diagrams depicting multiple steps
and routes.

Chandubhai S. Patel Institute of Technology


Basic Flowchart Symbols
Notice there are four types
of symbols in this flowchart:
◦ Oval/Rounded Rectangle
◦ Parallelograms
◦ A Rectangle
◦ Diamond
Each symbol represents a
different type of operation.

Chandubhai S. Patel Institute of Technology


Basic Flowchart Symbols
Terminals
◦Represented by oval/rounded rectangles
◦Indicate a starting or ending point

START END

Chandubhai S. Patel Institute of Technology


Basic Flowchart Symbols
Input/Output Operations:
◦Represented by parallelograms
◦Indicate an input or output operation

Input Output

Chandubhai S. Patel Institute of Technology


Basic Flowchart Symbols
Processes
◦Represented by rectangles
◦Indicates a process such as a mathematical
computation or variable assignment
Process

Chandubhai S. Patel Institute of Technology


Basic Flowchart Symbols
Decision
oRepresented by Diamond
oTakes the decision in Yes or No form

Decision

Chandubhai S. Patel Institute of Technology


Example for Flowchart (Pay Calculation)
START
Output Operation
Variable Contents: How many Display message “How
many hours did you work?”
hours did
Hours: ? you work?
Pay Rate: ? Read Hours

Gross Pay: ? Display message “How


much do you get paid per
hour?”

Read Pay Rate

Gross Pay
= Hours * Pay Rate

Display Gross Pay

END

Chandubhai S. Patel Institute of Technology


Example for Flowchart
START
How many Display message
hours did “How many hours
you work? did you work?”
40

Input Operation Read Hours


Variable Contents: (User types 40) Display message
Hours: 40 “How much do you
get paid per hour?”
Pay Rate: ?
Gross Pay: ? Read Pay Rate

Gross Pay
= Hours * Pay Rate

Display Gross Pay

END

Chandubhai S. Patel Institute of Technology


Example for Flowchart
START

How much Display message “How many


do you get hours did you work?”
paid per
Variable Contents: hour?

Hours: 40 Read Hours

Pay Rate: ? Display message “How much


Output Operation do you get paid per hour?”
Gross Pay: ?
Read Pay Rate

Gross Pay
= Hours * Pay Rate

Display Gross Pay

END

Chandubhai S. Patel Institute of Technology


Example for Flowchart
START

How much Display message “How


do you get many hours did you
paid per work?”
hour? 20
Read Hours
Variable Contents:
Display message “How
Hours: 40 much do you get paid per
hour?”
Pay Rate: 20 Input Operation
Gross Pay: ? (User types 20)
Read Pay Rate

Gross Pay
= Hours * Pay Rate

Display Gross Pay

END

Chandubhai S. Patel Institute of Technology


Example for Flowchart
START

How much Display message “How many


do you get hours did you work?”
paid per
hour?
Read Hours

Variable Contents: Display message “How much


do you get paid per hour?”
Hours: 40
Pay Rate: 20 Read Pay Rate
Gross Pay: 800
Gross Pay
Process: The product
= Hours * Pay Rate
of 40 times 20 is
stored in Gross Pay
Display Gross Pay

END

Chandubhai S. Patel Institute of Technology


Example for Flowchart
START

Your gross Display message


pay is 800 “How many hours
did you work?”

Read Hours
Variable Contents:
Hours: 40 Display message
“How much do you
get paid per hour?”
Pay Rate: 20
Gross Pay: 800 Read Pay Rate

Gross Pay
= Hours * Pay Rate
Output Operation
Display Gross Pay

END

Chandubhai S. Patel Institute of Technology


Four Flowchart Structures
•Sequence
•Decision
•Repetition
•Case

Chandubhai S. Patel Institute of Technology


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

Chandubhai S. Patel Institute of Technology


Decision Structure
One of two possible actions is
taken, depending on a condition.

Chandubhai S. Patel Institute of Technology


Decision Structure
A new symbol, the diamond,
indicates a yes/no question. If NO YES
the answer to the question is yes,
the flow follows one path. If the
answer is no, the flow follows
another path.

Chandubhai S. Patel Institute of Technology


Decision Structure
In the flowchart segment
below, the question “is x < y?”
is asked. If the answer is no, NO YES
then process A is performed. If x < y?

the answer is yes, then process


B is performed. Process A Process B

Chandubhai S. Patel Institute of Technology


Decision Structure
The flowchart segment, Flowchart C code
shows how a decision
structure is expressed in C NO YES if (x < y)
as an if/else statement. x < y? a = x * 2;
else
x+y x*2 a = x + y;

Chandubhai S. Patel Institute of Technology


Decision Structure
The flowchart segment, Flowchart C code
shows a decision structure
with only one action to NO YES
if (x < y)
perform. It is expressed as x < y?
a = x * 2;
an if statement in C code.
Calculate a
as x times 2.

Chandubhai S. Patel Institute of Technology


Repetition Structure
A repetition structure represents part of the program that repeats. This
type of structure is commonly known as a loop.
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.

Chandubhai S. Patel Institute of Technology


Repetition Structure
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.

YES
x < y? Process A

Chandubhai S. Patel Institute of Technology


Repetition Structure
The flowchart segment below shows a repetition
structure expressed in C as a while loop.
Flowchart C Code

while (x < y)
YES x++;
x < y? Add 1 to x

Chandubhai S. Patel Institute of Technology


Controlling a Repetition Structure
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 YES
changed. Once the loop starts, it will x < y? Display x
never end.
QUESTION: How can this
flowchart be modified so
it is no longer an infinite
loop?

Chandubhai S. Patel Institute of Technology


Controlling a Repetition Structure
By adding an action within the repetition that changes the
value of x.

YES
x < y? Display x Add 1 to x

Chandubhai S. Patel Institute of Technology


Post-Test vs Pre-Test Repetition Structure

Chandubhai S. Patel Institute of Technology


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

Chandubhai S. Patel Institute of Technology


Example of Case Structure
The structure below indicates actions to perform depending on the
value in years_employed.
If years_employed = 2, If years_employed = 3,
bonus is set to 200 bonus is set to 400
If years_employed = 1, If years_employed > 3
CASE
bonus is set to 100 years_employed any other value, bonus
is set to 800

1 2 3 Other

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

Chandubhai S. Patel Institute of Technology


Connectors
Sometimes a flowchart will not fit on one page.
A connector (represented by a small circle) allows you to
connect two flowchart segments.

Chandubhai S. Patel Institute of Technology


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

Chandubhai S. Patel Institute of Technology


Combining Structures
Structures are commonly combined to create more complex
algorithms.
The flowchart segment below combines a decision structure with a
sequence structure.

YES
x < y? Display x Add 1 to x

Chandubhai S. Patel Institute of Technology


Combining Structures
This flowchart segment
NO YES
shows two decision x > min?
structures combined.
Display “x is NO YES
outside the limits.”
x < max?

Display “x is Display “x is
outside the limits.” within limits.”

Chandubhai S. Patel Institute of Technology


Try this
Write an algorithm to determine a student’s final
grade and indicate whether the student is passing or
failing? The final grade is calculated as the average of
four marks If student’s grade is less than 60, then
he/she is failed else passed.

Chandubhai S. Patel Institute of Technology


Flowchart and Algorithm
START
Step 1: Input M1,M2,M3,M4
Input
M1,M2,M3,M4
Step 2: GRADE 
(M1+M2+M3+M4)/4
GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE <60) then
Print “FAIL”
N IS Y else
GRADE<60
Print “PASS”
endif
PRINT PRINT
“PASS” “FAIL”

STOP

Chandubhai S. Patel Institute of Technology


Flowchart Example
Write an algorithm and draw a flowchart to convert the length in feet to
centimeter.
Algorithm START

Step 1: Input Length_ft Input


Length_ft
Step 2: Lenth_cm Length_ft x 30
Step 3: Print Length_cm
Length_cm  Length_ft x 30

Print
Length_cm

STOP

Chandubhai S. Patel Institute of Technology


Flowchart Example
We want to create a flowchart that prints out the biggest of three inputted numbers, write the
algorithm.
START

Read A, B, C

Yes Yes No Yes


A>C? A>B? B>C?

No No

Print A Print C Print B

END

Chandubhai S. Patel Institute of Technology


Practice work
Draw a flowchart to depict the log in procedure of Gmail account.
Create a flowchart that prints out the word “Distinction” if the
number input is >= 70, if the number is less than 40 print out the
word “Fail”, otherwise print out the word “Pass”.
Create a flowchart that prints out the average value of seven numbers
input in.

Chandubhai S. Patel Institute of Technology


Practice Work
•The flowchart on the right is meant to • Save your work on disk
show the steps for stopping working
on a computer and shutting it down. • Note: Some of the instructions are not
required - you should
•Place the instructions given below in only include those which are relevant
the flow chart. [Draw it in your to the task.
notebook]
• Quit the Program
• Switch off the machine
• Finish working on document
• Start a new document
• Check your email
• Turn on your computer
• Select ‘shut down’

Chandubhai S. Patel Institute of Technology


Practice Work
Draw a flowchart, from the given algorithm that will calculate the roots of a
quadratic equation: ax2  bx  c  0
Hint: d = sqrt ( b  b  4  a  c ), Algorithm:
Step 1: Input a, b, c
and the roots are:
Step 2: d sqrt (
b  b  4  a  c)
If d<0; no solution Step 3: D < 0, go to step 4 else go to step 5
else Step 4: Stop
Step 5: x1  (–b + d) / (2 x a)
x1 = (–b + d)/2a and x2 = (–b – d)/2a
Step 6: x2  (–b – d) / (2 x a)
Step 7: Print x1, x2
Step 8: Stop

Chandubhai S. Patel Institute of Technology


Practice Work
Scenario: Draw a flowchart for the given algorithm:
•Collect employee name (NAME), overtime hours Step 1: Input NAME,OVERTIME,ABSENT
worked (OVERTIME), hours absent (ABSENT) and Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT 50
•Determine the bonus payment (PAYMENT). else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT 40
Bonus Schedule else if (OVERTIME–(2/3)*ABSENT > 20) then
OVERTIME – (2/3)*ABSENT Bonus Paid PAYMENT 30
else if (OVERTIME–(2/3)*ABSENT > 10) then
>40 hours $50 PAYMENT 20
>30 but ≤ 40 hours $40 else
>20 but ≤ 30 hours $30 PAYMENT 10
>10 but ≤ 20 hours $20 endif
≤ 10 hours $10 Step 3: Print “Bonus for”, NAME “is $”, PAYMENT

Chandubhai S. Patel Institute of Technology


Thank You

Chandubhai S. Patel Institute of Technology

You might also like