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

Flowchart Basics

The document discusses non-iterative and iterative algorithms. Non-iterative algorithms perform simple calculations without loops, while iterative algorithms repeat calculations in loops that may be count-based or condition-based. Examples of iterative algorithms include calculating values for multiple inputs and finding extreme values from a data set. Practice questions provide examples of algorithms to convert currencies, calculate time differences, count values above a threshold, and calculate averages.

Uploaded by

panamasmas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Flowchart Basics

The document discusses non-iterative and iterative algorithms. Non-iterative algorithms perform simple calculations without loops, while iterative algorithms repeat calculations in loops that may be count-based or condition-based. Examples of iterative algorithms include calculating values for multiple inputs and finding extreme values from a data set. Practice questions provide examples of algorithms to convert currencies, calculate time differences, count values above a threshold, and calculate averages.

Uploaded by

panamasmas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Algorithm

Flow Chart Pseudocode

Non-Iterative Iterative

Formula/Value Count based Loop Condition based Loop


Calculation

Formula/Value
Totaling Counting Extreme Values
Calculation
Non-Iterative Algorithm
(Simple Input-
In non-iterative algorithm we usually accept/input data to find/calculate some values either through some
formula or from simple calculation. Once calculation is done we release the results. In some questions your

Process_Output
calculation might be conditional, some time your output would be depending on some criteria but in any
case there won’t be any loop involved.

Write an algorithm in the form of a flowchart which takes temperatures input in Fahrenheit. Convert the
temperature in Celsius with the help of following formula:

Temp (in Celsius)=(Temp (in Fahrenheit) - 32)/1.8

and output temperature in Celsius.

Start of
Flowchart

Start
Here we decide the variable to be used and
initialized them. In this particular question
we have taken TempA for Temperature (in
TempAç0 Fahrenheit) and TempB for Temperature (in
TempBç0
Celsius)

Input Dry running of flowcharts


Command Initialzation
Input TempA TempB
TempA
0 0
Process Box
Output
Input TempA TempB
TempB
100 37=(100-32)/1.8 37
TempBç (TempA-32)/1.8

Output
Command
Output
TempB
End of
Flowchart

End
Practice question:

Question 01:
Customers can withdraw cash from an Automatic Teller Machine (ATM).
• withdrawal is refused if amount entered > current balance
• withdrawal is refused if amount entered > daily limit
• if current balance < $100, then a charge of 2% is made
• if current balance $100, no charge is made
Write an algorithm which inputs a request for a sum of money, decides if a withdrawal can be
made and calculates any charges. Appropriate output messages should be included.

Input Amount
If Amount>Current Balance then Print “Withdrawal Refused”
If Amount>Daily Limit then Print “Withdrawal Refused”
Current Balance= Current Balance – Amount
If Current Balance<100 then Charges=Current Balance -2% Else Charges=0
Reason Variable Name Type Initial Value
Sum of Money Amount Number 0
Current Balance CB/Balance Number 0
Daily Limit DL/Limit/DailyLimit Number 0
Charges Ch/Charges Number 0
Question 02:
Regis lives in Brazil and often travels to USA, Europe and Japan. He wants to be able to convert
Brazilian Reais into US dollars, European euros and Japanese yen. The conversion formula is:

currency value = number of Reais X conversion rate

For example, if Regis is going to USA and wants to take 1000 Reais (and the exchange rate is 0.48)
then he would input USA, 1000 and 0.48 and the output would be: 480 US dollars.

Write an algorithm, using pseudocode, which inputs the country he is visiting, the exchange rate
and the amount in Brazilian Reais he is taking. The output will be value in foreign currency and the
name of the currency.

Question 03:
Daniel lives in Italy and travels to Mexico, India and New Zealand. The times differences are:

Country Hours Minutes


Mexico -7 0
India +4 +30
New Zealand +11 0

Thus, if it is 10:15 in Italy it will be 14:45 in India.

(a) Write an algorithm, using pseudocode or otherwise, which:


• Inputs the name of the country
• Inputs the time in Italy in hours (H) and minutes (M)
• Calculates the time in the country input using the data from the table
• Outputs the country and the time in hours and minutes

(b) Describe, with examples, two sets of test data you would use to test your algorithm.
Iterative Algorithm
(Simple Input-
In iterative algorithm you repeat one or more statement many times. Some time you know how many times
the process has to be repeated. This is so called count based loop/iteration i.e. calculate BMI of 30 students

Process_Output
or calculate tax of 5000 houses. But on many occasions you don’t know how many times you have to
repeat the process. In such cases repetition will be depending on some condition i.e. repeat while a certain
condition is True i.e. inputs a set of positive numbers (which end with -1) & outputs the value of the largest
(highest) number input. In this question you don’t know how many positive numbers are there rather you
have been given a condition i.e. enter -1 to stop or exit the loop.

In iterative algorithm we usually do:

1. Value calculation (either through any formula or by simple calculation)


2. Counting (counting a sub-set data based on some condition)
3. Totaling (usually done to calculate average)
4. Finding extreme values (to find single largest/smallest, highest/lowest/, maximum/minimum value)
Write an algorithm, using pseudocode or a flowchart, which Write an algorithm, using pseudocode or a flowchart, which
• inputs 50 positive numbers • inputs a set of positive numbers (which end with -1)
• outputs how many of the numbers were > 100 • outputs how many of the numbers were > 100
• outputs the value of the largest (highest) number input • outputs the value of the largest (highest) number input

Start Start

Numç0
Numç0 CountAç0
Countç0 Largestç0
CountAç0
Largest0ç0 Input
Num
Yes
Count=50
Yes Num= - 1
No
No
Input
Num
Yes Num>100 No

Yes No
Num>100

CountA ç CountA + 1
CountA ç CountA + 1

Yes Num>Largest No
Yes No
Num>Largest

Largest ç Num
Largest ç Num

Input
Num
Count ç Count + 1

Print CountA , Largest


Print CountA , Largest

End
End
Write an algorithm, using pseudocode or a flowchart, which Write an algorithm, using pseudocode or a flowchart, which
• inputs 50 positive numbers • inputs a set of positive numbers (which end with -1)
• outputs the average (mean) value of the input numbers • outputs the average (mean) value of the input numbers
• outputs the value of the largest (highest) number input • outputs the value of the largest (highest) number input
Start

Start

Numç0
Totalç0
Countç0 Countç0
Totalç0 Largest0ç0
Largest0ç0 Avgç0
Numç0
Avgç0
Input
Num
Yes
Count=50
Yes
No Num= - 1

No
Input
Num
TotalçTotal + Num

Total ç Total + Num

CountçCount + 1
Yes No
Num>Largest
Yes No
Num>Largest

Largest ç Num

Largest ç Num

Count ç Count + 1
Input
Num

Avg ç Total / 50

Avg ç Total / Count

Print Avg , Largest

Print Avg , Largest


End

End
1) Start of
Start Flowchart

Countç0
CountAç0
CountBç0 2) Here we decide the variables to be used
TempAç0 and initialized them. All numerical variable
TempBç0
Totalç0
will be initialized with 0 except for lowest
Avgç0 that will be initialized with 1000.
Highestç0
Lowestç1000

11) Always
Calculate 3) Start of the loop. Set loop exit
Yes
Average outside is
condition i.e. Count=100 if you have
Count=100
loop. ? to input temperature over 100 days.

No 4) Input
AvgçTotal/100 Command
Input
TempA
5) Value
Output calculation if
Avg,
CountA, required.
TempBç (TempA-32)/1.8
CountB
Highest,
Lowest 6) Output result
only if point 5
Output exists.
13) End of TempB
End
Flowchart 7) Add the input value or result
of value calculated if average
Totalç Total +TempB has been asked to calculate

12) Always Print


8) Set the counting condition if
Average,
Counting
tally is required and increment
Yes No
is
Variables, and TempB<20
the respective counting variable
Extreme Values ? with 1.
outside the loop.
CountAç CountBç
CountA+1 CountB+1
Question: Write an algorithm in the
form of a flowchart which takes
temperatures input in Fahrenheit 9) If extreme values such as
over a 100 day period (once per Yes No Highest/Lowest,
is
day). Convert the temperature in TempB>Hi Tallest/Shortest,
Celsius with the help of following ghest
Maximum/Minimum is required.
formula: ?
Temp (in Celsius)=(Temp (in Highestç
Fahrenheit) - 32)/1.8 TempB
and output temperature in Celsius,
also print:

(b) number of days when the


Yes No
temperature was below 20C is
TempB<Lo
and the number of days when west
the temperature was 20C and ?

above. Lowestç
TempB
(c) average temperature of 100
days. 10) Increment the loop counter
by 1 for the next iteration and
(d) the highest and lowest go back to loop exit condition.
Countç Count+1
temperature recorded in these
100days.
1) Start of
Start Flowchart

Countç0
CountAç0
CountBç0 2) Here we decide the variables to be used
TempAç0 and initialized them. All numerical variable
TempBç0
Totalç0
will be initialized with 0 except for lowest
Avgç0 that will be initialized with 1000.
Highestç0
Lowestç1000
Dayç” “
3) Input
Input
Command
Day,
TempA

12) Always
Calculate 4) Start of the loop. Use input
Yes
Average outside Day= variable to set loop exit condition i.e.
loop. ”Friday”
? Day=”Friday” or Num=-1 etc.

No 5) Value
AvgçTotal/Count calculation if
required.
TempBç (TempA-32)/1.8

6) Output result
Output
Avg, only if point 5
CountA, Output exists.
CountB TempB
Highest,
Lowest
7) Add the input value or result
of value calculated if average
Totalç Total +TempB has been asked to calculate
14) End of End
Flowchart
8) Nothing to do with loop but
required for computing Average.
Countç Count+1

13) Always Print


9) Set the counting condition if
Average,
Counting
tally is required and increment
Yes No
is
Variables, and TempB<20
the respective counting variable
Extreme Values ? with 1.
outside the loop.
CountAç CountBç
CountA+1 CountB+1

Question: Write an algorithm in the


form of a flowchart which takes
temperatures input in Fahrenheit
on certain days (stop by Friday). Yes
is
No

Convert the temperature in Celsius TempB>Hi


with the help of following formula: ghest
?
Temp (in Celsius)=(Temp (in
Fahrenheit) - 32)/1.8 Highestç
and output temperature in Celsius, TempB
also print: 10) If extreme values such as
Highest/Lowest,
(b) number of days when the Tallest/Shortest,
temperature was below 20C Yes is No Maximum/Minimum is required.
and the number of days when TempB<Lo
the temperature was 20C and west
?
above.
Lowestç
(c) average temperature of 100 TempB

days.
11) Input command for next
(d) the highest and lowest Input
temperature recorded in these Day, iteration before returning to
100days. TempA loop,
Value Calculation
In many questions you will be asked to calculate some values with the help of any given formula or through
simple calculation. Sometime this calculation is quite simple (for example convert temperature from
Fahrenheit to Celsius) but on many occasions it will be conditional (calculate tax based on their house
value). In any case once you find the value you have to release the result. Don’t mix concept of totaling,
counting, average etc with value calculation.

Simple Calculation/Simple Output:

Start Write an algorithm in the form of a flowchart which


takes temperatures input in Fahrenheit over a 100 day
period (once per day). Convert the temperature in
Celsius with the help of following formula:
Temp (in Celsius)=(Temp (in Fahrenheit) - 32)/1.8
Countç0
and output temperature in Celsius
TempAç0
TempBç0

Yes
is
Count=100
?

No

Input
TempA

TempBç (TempA-32)/1.8

Output
TempB

CountçCount+1

End
Value Calculation
Simple Calculation but Conditional Output:

Write an algorithm in the form of a flowchart which takes


Start temperatures input in Fahrenheit over a 100 day period (once per
day). Convert the temperature in Celsius with the help of following
formula: Temp (in Celsius)=(Temp (in Fahrenheit) - 32)/1.8
Countç0
TempAç0 and output temperature in Celsius with this comment.
TempBç0 If temperature exceeds 30C then comment “Hot summer day” else
print “Normal temperature”.

Yes
is
Count=100
?

No

Input
TempA

TempBç (TempA-32)/1.8

Yes No
is
TempB>30
?

Output Output
TempB, “Hot TempB, “Normal
Summer Day” Temperature”

CountçCount+1

End
Value Calculation
Conditional Calculation with Simple Output:

Write an algorithm in the form of a flowchart which takes temperatures with scale input over a 100 day
period (once per day). Convert the temperature in Celsius if input temperature is in Fahrenheit with the help
of following formula: Temp (in Celsius)=(Temp (in Fahrenheit) - 32)/1.8

And in Fahrenheit if input Temperature is in Celsius. and output converted temperature.

Temp (in Fahrenheit)=(Temp (in Celsius) + 32) x1.8

Start

Countç0
TempAç0
TempBç0

Yes is
Count=100
?

No

Input
TempA,
Scale

Yes is
No
Scale=
”Fahrenhei
t”
?

TempBç TempBç
(TempA+32)x1.8 (TempA-32)/1.8

Output
TempB

CountçCount+1

End
A town contains 5000 houses. Each house owner must pay tax based on the value of the house.
Houses over $200 000 pay 2% of their value in tax, houses over $100 000 pay 1.5% of their value in
tax and houses over $50 000 pay 1% of their value in tax. All others pay no tax. Write an algorithm
to solve this problem in the form of a flowchart.

Start

Countç0
HValueç0
Taxç0

Yes
Count=5000

No

Input HValue

Yes HValue>200 No
000

Tax ç (HValue * 2) /
100

Yes HValue>100000 and No


HValue<200000

Tax ç (HValue *
1.5) / 100

Yes HValue>50000 and No


HValue<100000

Tax ç (HValue * 1) /
100

Yes HValue<500 No
00

Tax ç 0

Print Tax

Count ç Count + 1

End
Practice Question:

Question 01:
A formula for calculating the body mass index (BMI) is:

Using pseudocode or otherwise, write an algorithm that will input the ID, weight (kg) and height
(m) of 30 students, calculate their body mass index (BMI) and output their ID, BMI and a comment
as follows:

A BMI greater than 25 will get the comment ‘OVER WEIGHT’, a BMI between 25 and 19 (inclusive)
will get ‘NORMAL’ and a BMI less than 19 will get ‘UNDER WEIGHT’.

Question 02:
Fuel economy for a car is found using the formula:

Write an algorithm, using pseudocode or otherwise, which inputs the Distance Travelled (km) and
the Fuel Used (litres) for 1000 cars. The Fuel Economy for each car is then calculated and
displayed.

Question 03:
The manufacturing cost of producing an item depends on its complexity. A company
manufactures three different types of item, with costs based on the following calculations:

Item type 1: item cost = parts cost * 1.5


Item type 2: item cost = parts cost * 2.5
Item type 3: item cost = parts cost * 5.0
The company makes 1000 items per day.

Write an algorithm, using pseudocode, flowchart or otherwise, which

• inputs the item type and parts cost of each item


• outputs the item cost for each item
Counting
In many question you will be asked to find some sub-set of data such as input 5 numbers and find
how many were positive and negative. Counting usually depends on some condition where we
increment the respective counter by 1 if condition gets true. We initialize each counter (i.e.
CountA, CountB) for respective category (i.e. CountA to record occurrence of positive numbers
and CountB to record occurrence of negative numbers) by 0 at first. Never mix counting with
Count Variable. Count Variable just controls the iteration; it got nothing to do with category
counting. Counting variables i.e. CountA, CountB etc will always be printed once loop gets over.
It will never be printed within loop.

Write an algorithm in the form of a


Start
flowchart which takes temperatures input
over a 100 day period (once per day) and
Countç0
CountAç0 outputs the number of days when the
CountBç0 temperature was below 20C and the
number of days when the temperature
was 20C and above.
Yes
is
Count=100
?
No

Input
Temp

Yes No
is
Temp>=20
?

CountAçCountA+1 CountBçCountB+1

CountçCount+1

Output
CountA,
CountB

End
Counting:

This algorithm inputs 5 values and outputs how many input numbers were negative and how many
were positive

Start

Countç0
CountAç0
CountBç0

Yes

is
Count=5
?

No

Input
Num

Yes No
is
Num>0
?

CountAçCountA+1 CountBçCountB+1

CountçCount+1

Output
CountA,
CountB,

End
Practice Questions:

Question 01:
A company has 5000 CDs, DVDs, videos and books in stock. Each item has a unique 5-digit code
with the first digit identifying the type of item, i.e.
1 = CD
2 = DVD
3 = video
4 = book
For example, for the code 15642 the 1 identifies that it is a CD, and for the code 30055 the 3
identifies that it is a video.

Write an algorithm, using pseudocode or otherwise, that


• Inputs the codes for all 5000 items
• Validates the input code
• Calculates how many CDs, DVDs, videos and books are in stock
• Outputs the four totals.

Question 02:
5000 numbers are being input which should have either 1 digit (e.g. 5), 2 digits (e.g. 36), 3 digits
(e.g. 149) or 4 digits (e.g. 8567).
Write an algorithm, using pseudocode or flowchart only, which
• inputs 5000 numbers
• outputs how many numbers had 1 digit, 2 digits, 3 digits and 4 digits
• outputs the % of numbers input which were outside the range

Question 03:
A company is carrying out a survey by observing traffic at a road junction. Each time a car, bus,
lorry or other vehicle passed by the road junction it was noted down.
10 000 vehicles were counted during the survey.
Write an algorithm, using pseudocode, which:
• inputs all 10000 responses
• outputs the number of cars, buses and lorries that passed by the junction during the survey
• outputs the number of vehicles that weren’t cars, buses or lorries during the survey
Totaling
Totaling is usually done when you have to calculate average. In totaling we add the value entered by user
or derived from any calculation. Calculation of average and result of total and average will be placed
outside the loop. Never calculate average or show result of total or average inside the loop.

Write an algorithm in the form of a flowchart which takes


Start
temperatures input over a 100 day period (once per day) and
outputs the number of days when the temperature was below
Countç0 20C and the number of days when the temperature was 20C
Tempç0 and above, also print average temperature of 100 days.
CountAç0
CountBç0
Totalç0
Avgç0

Yes
is
Count=100
?

No

Input
Temp

Total=Total+Temp

Yes No
is
Temp>=20
?

CountAçCountA+1 CountBçCountB+1

CountçCount+1

Output
CountA,
Avgç(Total/100) End
CountB,
Avg
Practice Questions:

Question 01:
A school is doing a check on the heights and weights of all its students. The school has 1000
students.
Write an algorithm, using pseudocode or a flowchart, which
• inputs the height and weight of all 1000 students
• outputs the average (mean) height and weight
• includes any necessary error traps for the input of height and weight

Question 02:

Write an algorithm, using pseudocode, which inputs rainfall (in cm) for 500 days and outputs the
average rainfall.

Question 03:

Fuel economy for a car is found using the formula:

Write an algorithm, using pseudocode or otherwise, which inputs the Distance Travelled (km) and
the Fuel Used (litres) for 1000 cars. The Fuel Economy for each car is then calculated and the
following outputs produced:
• Fuel Economy for each car
• average (mean) Fuel Economy for all of the cars input
Extreme Values
On many occasion you will be asked to find highest or lowest values or both. We call it finding extreme
values i.e. maximum/minimum, highest/lowest, fastest/slowest etc. For example Write an algorithm in the
form of a flowchart which takes temperatures input over a 100 day period (once per day) and outputs the
highest and lowest temperature recorded in these 100days.

Note: We always initialize lowest value with 1000 instead of 0 and values of Highest/Lowest will never be
released within the loop.
Start

Countç0
Highç0
Lowç1000

Yes
is
Count=100
?
No

Input
Temp

Yes No
is
Temp>High

HighçTemp

Yes No
is
Temp<Low

LowçTemp

CountçCount+1

Output
High,
Low

End
Practice Questions:

Question 01:
A car’s speed is measured between points A and B, which are 200 km apart.

The final speed of the car is calculated using the formula:

What is the final speed of a car if it takes 2 hours to get from A to B? [1]

(b) Write an algorithm, using pseudocode or otherwise, which inputs the times for 500 cars,
calculates the final speed of each car using the formula in part (a), and then outputs:
• the final speed for ALL 500 cars
• the slowest (lowest) final speed
• the fastest (highest) final speed

Question 02:
Write an algorithm, using pseudocode or flowchart only, which:
• inputs three numbers
• outputs the largest of the three numbers

Question 03:
As part of an experiment, a school measured the heights (in metres) of all its 500 students.
Write an algorithm, using pseudocode, which inputs the heights of all 500 students and outputs the
height of the tallest person and the shortest person in the school.

You might also like