0 ratings 0% found this document useful (0 votes) 30 views 15 pages Alg, Flowchart and Pseudo Code
The document outlines algorithms, flowcharts, and pseudocode as methods for problem-solving in programming. It provides real-life and numerical examples, including tea preparation, calculating averages, and finding maximum values, illustrating how to structure algorithms and represent them visually. Additionally, it discusses guidelines for creating flowcharts and pseudocode, highlighting their advantages and limitations.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Carousel Previous Carousel Next
Save Alg, flowchart and pseudo code For Later a Algorithm Pseudocode
| Flowchart m@ Source Code
ALGORITHM
Iti is defined as a finite sequence of explicit instructions that, when provide with a sd
input values, produces an output and terminates. Algorithms can have steps that repeater
require decisions.
2.1.1 Simple Problems
Real Life Example: Algorithm to prepare a tea
Step 1: Start
Step 2: Getthe ingredients WATER, MILK, SUGAR and TEA POWDER
Step 3: Boil the WATER and add TEA POWDER
Step 4: Add SUGAR and pour MILK
Step 5: Stir well
Step 6: Pour into cup
Step 7: A CUP OF TEA is ready to drink
Step 8: Stop
Numerical Example: Algorithm to find total and average of three numbers
Step 1: Start
Step 2: Read the numbers, say A, B and C
Step 3: Add A, Band C and store it in SUM
Step 4: Divide the SUM by 3 and store it in AVERAGE
30Idea of Algorithm
Step 5: Display the SUM, AVERAGE
Step 6: Stop
2.4.2 Problems Involving Choice
Real Life Example: Algorithm to prepare a cup of tea with a choice of black tea or tea
with milk
Step 1: Start
Step 2: Get the INGREDIENTS WATER, MILK, SUGAR and TEA POWDER
Step 3: Get the choice of the user say CHOCE i.e.) whether he wants black tea or tea with
milk
Step 4: Boil the WATER, add TEA POWDER and SUGAR
Step 5: If CHOICE is tea with milk then add MILK
Step 6: Stir well
Step 7: Pour into cup
Step 8: A CUP OF TEA is ready to drink
Step 9: Stop
Numerical Example: Algorithm to find maximum of three numbers
Step 1: Start
Step 2: Read the three numbers, say A, B and C
Step 3: Check whether A is greater than B and C. If, so display A is biggest number and
Stop
Step 4: Otherwise, check whether B is greater than C. If, so display B is the biggest number
and stop.
Step 5: Otherwise, display C is the biggest number
Step 6: Stop
2.1.3 Problems Involving Repetitions
Real Life Example: Algorithm to prepare a cup of tea with enough sweetness
Step 1: Start :
Step 2: Get the INGREDIENTS WATER, MILK, SUGAR and TEA POWDER
Step 3: Boil the WATER, add TEAPOWDER, SUGAR and MILK
Step 4: Taste the tea
Step 5: If sweetness is not enough add SUGAR and go to Step 4.
Step 6: Stir well
Step 7: Pour into cupm Solving Using C
Programming for Proble!
Step 8: A CUP OF TEA is ready to drink
Step 9: Stop
Numerical Example: Calculating cut-off mark for N students
Step 1: Start
Step 2: Read the number of students
Step 3: Initialise the COUNT to 1 ;
Step 4: If COUNT is less than or equal to N, Read MATHS, PHYSICS ang
marks of a student. Otherwise go to Step 10
Step 5: Calculate CUTOFF by dividing the MATHS mark by 2, PHYSICS and Chg
marks by 4 and adding them together (i.e.)
CUTOFF = MATHS/2+PHYSICS/4+CHEMISTRY/4
Step 6: Increment the COUNT by 1
Step 7: Display the CUTOFF
Step 8: Initialise the CUTOFF to 0
Step 9: Go to Step 4
Step 10:Stop
Additional Examples
Algorithm to find Area of a Rectangle
Step 1: Start
Step 2: Read the LENGTH and BREADTH of the rectangle
Step 3: Multiply LENGTH and BREADTH to compute the AREA
Step 4: Display the AREA
Step 5: Stop
Algorithm to find sum of all the numbers till -1 is entered
Step 1: Start
Step 2: Initialise SUM to 0
Step 3: Geta value, say A
Step 4: If the A is equal to -1, go to Step 7
Step 5: Add A to SUM (i.e.) SUM=SUM+A
Step 6: Go to Step 3 to get next value
Step 7: Display the SUM
Step 8: StopIdea of Algorithm
Algorithm to find the largest number in a list of N numbers
Step 1: Start
Step 2: Read the number of elements among which maximum is to be found, say N
Step 3: Initialise the COUNT to 1
Step 4: Read the first number, NUMBER and consider it as the BIGGEST
Step 5: If COUNT is less than or equal to N, read the next number, NUMBER Otherwise
go to Step 9
Step 6: If NUMBER is greater than BIGGEST, the assign NUMBER to BIGGEST
Step 7: Increment the COUNT by 1
Step 8: Go to Step 5
Step 9: Display the BIGGEST
Step 10:Stop
22) FLOWCHART
A flowchart is a graphical representation of the steps followed for solving a given problem.
Table 2.1 shows the activities and symbol representation of the tea making process described
earlier.
Table 2.1 Activities and Representation of Symbols
Activity
Representation
Description —
Take the ingredients: Water,
milk, sugar, tea powder
This is the first and foremost
important step in the tea making
process. Without the ingredients
the tea making process will
not be completed. Hence it is
the input for the tea making
process.
Heat the water
Heat the water and
These steps represent the
processes involved in tea
Add tea powder ‘Add tea powder
making.
‘Add sugar ‘Add Sugar and Pour
Pour milk ae
Stir well a
Pour into cup oe
Cup of Tea is ready to drink The final output of the process
sae is that a cup of tea is made and
ready to drink.3a) Programming for Problem Solving Using C ae
A complete flowchart for the Tea making process is Illustrated bel
and i
Heat the WATER And
1908 TEA LEAVES
amt
‘Add SUGAR and pour
T
Sui well
Pour into Cup
‘CUP OF TEA
ready to drink
Here, initially a start symbol is used to indicate the beginning of the flowchart and the wy
symbol is used to represent the end of the flowchart. Other steps are represented in the od
described above. All these steps are connected by flow lines which illustrate the sequen
be followed. The commonly used flowchart symbols are shown in Figure 2.2.
[_symbot_ | Symbol Name symbol_| SymbolName |
Flow lines Flow lines
atl
Terminal Terminal
Inputfoutput
Processing
Fig. 2.1 Flowchart Symbolsee _ldan of Mgorithin
2.2.1 Guidelines for Proparing Flowcharts
The following are some of the guidelines for preparing flowcharts:
« Allnecessary requirements should be listed out in loglenl order,
The flowchart should be clear, nent and easy to follow, There should not be ary
ambiguity in understanding the flowchart,
The usual direction of the flow of a procedure or system Is from left to right or top
to bottom.
Only one flow line should come out from a process symbol,
Only one flow line should enter a decision symbol and two lines come out of the
decision symbol each representing true and false
If the flowchart becomes complex, it Is better to use connector symbols to reduce the number
of flow lines. Avoid the intersection of flow lines If you want to make it more effective and
better way of communication. It is necessary to ensure that the flawchart has a logical start
and end, It is useful to test the validity of the flowchart by passing through it with a sisnple
test data.
2.2.2 Benefits of Flowcharts
The following are the benefits of the flowchart:
* Makes logic clear
* Provide easy understanding
Effective analysis of the logic
¢ Useful in coding
« Enables proper testing and debugging
« Provides better documentation and maintenance
2.2.3 Limitations of a Flowchart
Besides the benefits, the flowchart has few drawbacks:
* Complex logic : leads to complex and clumsy flowcharts if the logic is complex
« Difficult to modify
Few examples for flowcharts are given below:
Numerical Example: Flowchart to find sum and average of three numbers(36) Programming for Problem Solving Using C
2.2.4 Problems Involving Choice
Real Life Example: Flowchart to pr
or tea with milk
epare a cup of tea with a cholce of bi, cy
Get ingredients:
WATER, MILK,
and
Heat the WATER, add TEA LEAVES and SUGAR
If CHOICE is
tea with milk
‘Add milk
Yes
Stir well
Pour into CupIdea of Algorithm ['37))
Numerical Example: Flowchart to find maximum of three numbers
2.2.5 Problems Involving Repetitions
The final output of the process is that a cup of tea is made and ready to drink.
Get ingredients: WATER,
MILK, SUGAR and
TEA LEAVES
Heat the WATER, add TEA LEAVES, SUGAR and MILK
—————
Taste it
eweetness if
not enough,
Yes
Taste it
J
‘Add Sugar
T
4
‘Add milk
t
‘Stir well
t
Pour into cup
‘ACUP OF TEAIs
ready to drinkstudents
‘COUNT=1
CUTOFF=0
Road MATHS,
PHYSICS and
‘CHEMISTRY
marks
(CUTOFF=MATHS/2+PHYSICS/4+CHEMISTRY/4
t
‘COUNT=COUNT +1
t
Print CUTOFF
Additional Examples
Flowchart to find the area of a rectangle
AREALENGTH"
HEIGHT
a(40)) Programming for Problem Solving Using C
Flowchart to find largest number in a list of numbers
Read the number
of elements N
Read the first
number, NUMBER
COUNT=1
BIGGEST=-NUMBER
No
Read the next
number, NUMBER
COUNT=COUNT+1
Print the BIGGEST
it
NUMBER>
BIGGEST
BIGGEST=NUMBER
toe
PSEUDOCODE
Pseudo mean jon and code refers to instruction:
s n s. Itis a way of describing an alg
without using any specific programming language related notations. It is vuitton using Ba
English statements. It is also known as PDL (Programming Design Language) 32.3.4
.
.
2.3.2
2.3.3
.
2.3.4
Idea of Algorithm [44]
Guldolines for Writing Psaudocode
Should be written In simple English
Stops must be understandable
Should be concise
Advantages of Pseudocode
Allows the developer to express the design In natural language
Itis easier to develop program from pseudo code than with a flowchart
It is easy to translate the pseudo code Into programming language
Compact and concise
Limitations of Pseudocode
Does not provide visual representation of the program logic
No accepted standards for writing pseudo code
cannot be complied or executed
Keywords
Input: Read, Obtain, Get
Output: Print, Display, Show
Assignment/Process: Compute, Calculate. These keywords may be replaced with the
assignment operation. The format of the assignment operation is as follows:
Left side < Right Side
Right side value is assigned to the left side variable.
2.3.5
Constructs
Selection: if-then-else, case
Repetition: while, eepeat-until, for
(a) If-Then-Else: The If construct is used to execute the selected sequence based on the
condition. The If construct has four keywords: If, Then, Else, and Endif. The general
form is:
If condition Then
sequence 1
Else
sequence 2
Endif
The Else keyword and ‘sequence 2’ are optional. If the condition is true, sequence 1
is performed, otherwise sequence 2 is performed.Pyogramming for Problem Salving Using © en
(b) Whiter The white consiact le used to apacity a tong with 9 18et at the tp,
Doginning and ending of the loop are indicated by Wo Keywords while and end
‘The general for ts
while condition
texquence
endwhile
‘The loop is entered only If the e
iteration, At the conchision of each Iteration, th
continues as long as the condition Is tte.
(©) Case: A case constrict indicates 4 multiway
four keywords, enwe, of, others, and endease,
the various alternatives, The general form Is:
ondition Is trie, The “gacquence” 1s performed for ench y
y@ condition Is evaluated and the Joop |
branch based on conditions. It consigts of
and conditions are used to Indieaig
‘ease expression of
condition]; sequence 1 condlitlon
others: default sequence
Endcase
The others clause with its default sequence 1s optional. Conditions are normally
numbers or characters,
(a) Repeat-untll: This loop Is similar to the While loop except that the test is performed at
the bottom of the loop Instead of at the top. Two keywords, repeat and until are used,
The general form Is:
repeat
sequence
until condition
The “sequence” In this type of loop is always performed at least once, because the
test Is performed after the sequence is executed. At the conclusion of each iteration,
the condition is evaluated, and the loop repeats if the condition is false. The loop
terminates when the condition becomes true. i
(e) for: This loop is a specialised construct for iterating a specific number of times. Two
keywords, for and Endfor are used. The general form is: “
For variable <— start to end increment/decrement by number
sequence
Endfor
The constructs that are embedded within each other are called nested constructs. The
pseudocode is placed within begin and end.
2: sequence 2 .., condition n: sequence pIdea of Algorithm el
‘Simple Problems
Real Life Example: Pseudo code to prepare a ten
Begin
Get WATER, MILK, SUGAR, TEA POWDER
Boll WATER
Add TEA POWDER
Add SUGAR
Pour MILK
Stir
Pour into cup
Serve TEA
End
Numerical Example: Pseudo code to find sum and Average of Three Numbers
Begin
Get A,B,C
SUM-A+B+C
AVERAGE < SUM/3
Display SUM
End
Problems involving choice
Real Life Example: Pseudo code to Prepare a cup of tea with a choice of black
tea or tea with milk
Begin
Get WATER, MILK, SUGAR, TEA POWDER,
Get CHOICE
Heat WATER, add TEA POWDER, SUGAR
If CHOICE = tea with milk then add MILK
Endif
Stir
Pour into cup
Serve TEA
End
Numerical Example: Pseudo code to find maximum of three numbera
Begin :
Get A, B and CProgramming for Problem Solving Using C
IfA > Band A > C then display A
Else If B > C then display B
Else Display C
Endif
Endif
End
Problems involving repetitions
Real Life Example: Algorithm to prepare a cup of tea with enough sweetnes,
Begin
Get MILK, SUGAR, TEA POWDER
Boil WATER
Add TEA POWDER, SUGAR, MILK
Repeat
Taste tea
Add SUGAR
Until Sweetness is not enough
Stir
Pour into cup
Serve TEA
End
Numerical Example: Calculating cut-off mark for N students
Begin
GetN
For COUNT < 1 toN
Read MATHS, PHYSICS, CHEMISTRY
CUTOFF — MATHS/2 + PHYSICS/4+CHEMISTRY/4.
Display CUTOFF
Endfor
End
Additional Examples
Pseudo code to find sum till -1 is entered
Begin
SUM <0Idea of Algorithm
- Repeat
Get A
MWA<> -1 then
SUM + SUM+A
Endif
Until A = = -1
Display SUM
End
Pseudo code to find largest number in a list of N numbers
Begin
GetN
Get NUMBER
BIGGEST < NUMBER
For COUNT < 2 toN
Get NUMBER
If NUMBER> BIGGEST then
BIGGEST < NUMBER
Endif
Endfor
Display BIGGEST
End