Pseudocode With VB by Farhan Rahmat PDF
Pseudocode With VB by Farhan Rahmat PDF
Flow Chart
Iterative
Non-Iterative
Formula/Value
Calculation
Pseudocode
Formula/Value
Calculation
Totaling
Counting
Extreme Values
Description
TempA0
TempB0
Weather
Input
TempA
Input
Output
TempB
Output
Process
Pseudocode
TempB (TempA-32)/1.8
Yes
is
TempB<20
?
CountA
CountA+1
No
CountB
CountB+1
VB/VB.Net
Example:
TempA0
TempB0
Weather
Example:
DIM TempA As Integer=0
DIM TempB As Integer =0
DIM Weather As String=
[Variable Name]=Console.ReadLine()
Example:
TempA=Console.ReadLine()
TempB (TempA-32)/1.8
TempB=(TempA-32)/1.8
Example:
IF Temp<20 THEN
CountA CountA+1
ELSE
CountB CountB+1
ENDIF
Example:
If Temp<20 Then
CountA=CountA+1
Else
CountB= CountB+1
End If
Example:
IF TempB>Highest THEN Highest TempB
Example:
If TempB>Highest Then Highest = TempB
Selection
Yes
Highest
TempB
is
TempB>
Highest
?
No
Yes
is
Weather=
Foggy
?
No
Foggy
Foggy+1
Yes
Selection
is
Weather=
Cloudy
?
No
Cloudy
Cloudy+1
Yes
Rainy
Rainy+1
is
Weather=
Rainy
?
No
Sunny
Sunny +1
Example:
Module Module1
Sub Main()
DIM Weather as String
DIM Foggy as Integer=0
DIM Cloudy as Integer=0
DIM Rainy as Integer=0
DIM Sunny as Integer=0
Weather=Console.ReadLine()
Select Weather
Case "Foggy"
Foggy=Foggy+1
Case "Cloudy"
Cloudy=Cloudy+1
Case "Rainy"
Rainy=Rainy+1
Case Else
Sunny=Sunny+1
End Select
Console.WriteLine (Foggy)
Console.WriteLine (Cloudy)
Console.WriteLine (Rainy)
Console.WriteLine (Sunny)
End Sub
End Module
(You can try this code online at
http://ideone.com/EOBwlK,)
Counter could be
Count, X, etc.
Start
Count 0
Yes
Count=100
No
Output
"Hello World"
Count Count + 1
End
Counter could be
Count, X, etc.
DIM Counter As Datatype
FOR Counter = Start TO End
-------Things to do within
-------loop
---------------------NEXT
Things to do outside
-------the loop
--------------Example:
Print Hello World 100 times:
Module Module1
Sub Main()
Dim Count As Integer
For Count= 1 To 100
Console.WriteLine("Hello World)
Next
End Sub
End Module
Start
Count 0
Input
Code
Yes
Code=9999
No
Print
"Hello World"
Input
Code
End
While condition
[ statements ]
[ Continue While ]
[ statements ]
[ Exit While ]
[ statements ]
End While
Example:
Print Hello World till code 9999 was entered:
Module Module1
Sub Main()
Dim Code As Integer=0
Code=Console.ReadLine()
While Code<>9999
Console.WriteLine(Hello World)
Code=Console.ReadLine()
End While
End Sub
End Module
1) Start of
Flowchart
Start
Count0
CountA0
CountB0
TempA0
TempB0
Total0
Avg0
Highest0
Lowest1000
11) Always
Calculate
Average outside
loop.
Yes
is
Count=100
4) Input
Command
No
AvgTotal/100
Input
TempA
Output
Avg,
CountA,
CountB
Highest,
Lowest
13) End of
Flowchart
5) Value
calculation if
required.
TempB (TempA-32)/1.8
6) Output
result only if
point 5 exists.
Output
TempB
End
Yes
is
TempB<20
?
CountA
CountA+1
Yes
No
CountB
CountB+1
is
TempB>Hi
ghest
?
No
Highest
TempB
Yes
is
TempB<Lo
west
?
No
Lowest
TempB
Count Count+1
Initialization
Start
([Variable]Initial Value)
For Count Starting Value to End Value
Initialization Box
Input [Variable]
Value Calculation (if required)
Loop exit
Condition Check
Yes
No
Input Value
(TotalTotal+[Variable])
Value Calculation
Output Result
Totaling
(Add Input value or result to
Total Variable)
Yes
Increment the
counter by 1
Yes
Counting
No
Condition Check
find)
Increment the
counter by 1
No
Calculate Average
(AvgTotal/Loop End Value)
Calculate Percentage
Calculate Average
Output Average,
Counting, Extreme Values
End
Values
Start
Question:
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
and output temperature in Celsius,
also print:
(b) number of days when the
temperature was below 20C and
the number of days when the
temperature was 20C and above.
(c) average temperature of 100 days.
(d) the highest and lowest
temperature recorded in these
100days.
Variables to be
used
Count
TempA
TempB
CountA
CountB
Total
Avg
Highest
Lowest
to Store/Count
No of iteration
Temp in Fahrenheit
Temp in Celsius
Number of days
temp was below 20
Number of days
temperature was 20
and above
Running sum of 100
days temperature
Average
temperature of
100days
Highest temperature
in 100 days
Lowest temperature
in 100 days
Count0
CountA0
CountB0
Total0
Avg0
Highest0
Lowest1000
Count0
CountA0
CountB0
TempA0
Yes
Count=100
TempB0
No
Total0
Input
TempA
Avg0
Highest0
Lowest1000
Output
TempB
Yes
TempB<20
CountA CountA + 1
Yes
TempB (TempA-32)/1.8
No
PRINT TempB
TotalTotal+TempB
CountB CountB + 1
TempB>Highest
No
ELSE
CountBCountB+1
HighestTempB
ENDIF
Yes
TempB<Lowest
No
Lowest TempB
NEXT
AvgTotal/100
Count Count + 1
Avg Total/100
Output
Avg, CountA, CountB,
Highest, Lowest
End
Module Module1
Sub Main()
Dim Count As Integer=0
Dim CountA As Integer=0
Dim CountB As Integer=0
Dim TempA As Integer=0
Dim TempB As Integer=0
Dim Total As Integer=0
Dim Avg As Integer=0
Dim Highest As Integer=0
Dim Lowest As Integer=1000
For Count= 1 To 100
TempA=Console.ReadLine()
TempB=(TempA-32)/1.8
Console.WriteLine (TempB)
Total=Total+TempB
If TempB < 20 Then
CountA=Count+1
Else
CountB=CountB+1
End If
If TempB>Highest Then Highest=TempB
If TempB<Lowest Then Lowest=TempB
Next
Avg=Total/100
Console.WriteLine(Avg & CountA & CountB &
Highest & Lowest)
End Sub
End Module
Start
Num0
Count0
CountA0
Largest00
Yes
Module Module1
Sub Main()
DIM Num As Integer=0
Count=50
No
Num0
Input
Num
Yes
Num>100
Count0
No
CountA0
Largest0
FOR Count1 TO 50
READ Num
CountA CountA + 1
Num>Largest
No
NEXT
PRINT CountA, Largest
Largest Num
Count Count + 1
End
1) Start of
Flowchart
Start
Count0
CountA0
CountB0
TempA0
TempB0
Total0
Avg0
Highest0
Lowest1000
Day
Input
Day,
TempA
12) Always
Calculate
Average outside
loop.
Yes
Day=
Friday
5) Value
calculation if
required.
No
AvgTotal/Count
TempB (TempA-32)/1.8
Output
Avg,
CountA,
CountB
Highest,
Lowest
6) Output
result only if
point 5 exists.
Output
TempB
14) End of
Flowchart
End
Count Count+1
Yes
is
TempB<20
?
CountA
CountA+1
Yes
No
CountB
CountB+1
is
TempB>Hi
ghest
?
No
Highest
TempB
Yes
is
TempB<Lo
west
?
No
Lowest
TempB
Input
Day,
TempA
Initialization
[Variable]Initial Value)
Start
Input [Variable]
Initialization Box
Input Value
Loop exit
Condition Check
Yes
(TotalTotal+[Variable])
No
Value Calculation
data to find)
Output
Result
Calculate Average
(Avg Total/Count)
Totaling
(Add Input value or result to
Output
Avg, CountA, CountB,
Highest, Lowest
Total Variable)
Count Count + 1
(to calculate average)
End
CountACountA+1)
Extreme values (if highest/lowest to
find)
(IF [Variable]>Highest THEN
Check
Highest [Variable])
(IF [Variable]<Lowest THEN
Increment the
counter variable by 1
Increment the
counter variable by 1
Lowest [Variable])
Input [Variable]
Yes
Extreme Value
Check
No
Endwhile
Calculate Average
(AvgTotal/Loop End Value)
Calculate Percentage
Yes
Extreme Value
Check
No
Values
Input Value
Start
to Store/Count
No of days
Day of week
Temp in Fahrenheit
Temp in Celsius
Number of days
temp was below 20
Number of days
temperature was 20
and above
Running sum of 100
days temperature
Average
temperature of
100days
Highest temperature
in 100 days
Lowest temperature
in 100 days
Count0
CountA0
CountB0
Total0
Avg0
Highest0
Lowest1000
Day
Input
TempA, Day
Yes
Day="Friday"
No
Avg Total/100
Output
Count Count + 1
End
Yes
TempB<20
CountACountA + 1
Yes
No
CountBCountB + 1
TempB>Highest
No
Highest TempB
Yes
TempB<Lowest
No
Count0
CountA0
CountB0
TempA0
TempB0
Total0
Avg0
Highest0
Lowest1000
Day
READ TempA, Day
WHILE DAY<>Friday
Do
TempB (TempA-32)/1.8
PRINT TempB
TotalTotal+TempB
CountCount+1
IF TempA < 20
THEN
CountACount+1
ELSE
CountBCountB+1
ENDIF
IF TempB>Highest THEN HighestTempB
IF TempB<Lowest THEN LowestTempB
READ TempA, Day
ENDWHILE
AvgTotal/Count
PRINT Avg, CountA, CountB, Highest, Lowest
Module Module1
Sub Main()
Dim Count As Integer=0
Dim CountA As Integer=0
Dim CountB As Integer=0
Dim TempA As Integer=0
Dim TempB As Integer=0
Dim Total As Integer=0
Dim Avg As Integer=0
Dim Highest As Integer=0
Dim Lowest As Integer=1000
Dim Day As String=
Day=Console.ReadLine()
TempA=Console.ReadLine()
While Day <> Friday
TempB=(TempA-32)/1.8
Console.WriteLine (TempB)
Total=Total+TempB
Count=Count+1
If TempB < 20 Then
CountA=Count+1
Else
CountB=CountB+1
End If
If TempB>Highest Then Highest=TempB
If TempB<Lowest Then Lowest=TempB
Day=Console.ReadLine()
TempA=Console.ReadLine()
End While
Avg=Total/Count
Console.WriteLine(Avg & CountA & CountB &
Highest & Lowest)
End Sub
End Module
LowestTempB
Input
Day, TempA"
Start
Num0
CountA0
Largest0
Input
Num
Num0
CountA0
Yes
Num= - 1
Largest00
No
Yes
Num>100
READ Num
No
WHILE Num<> -1
Module Module1
Sub Main()
DIM Num As Integer=0
DIM CountA As Integer=0
DIM Largest As Integer=0
Num=Console.ReadLine()
While Num<>-1
Do
CountA CountA + 1
Num=Console.ReadLine()
READ Num
Yes
Num>Largest
No
ENDWHILE
PRINT CountA, Largest
End While
Console.WriteLine (CountA & Largest)
End Sub
End Module
Largest Num
Input
Num
End
Start
Num0
Total0
Count0
Largest00
Avg0
Num0
Count0
Input
Num
Total0
Avg0
Yes
Num= - 1
Largest0
No
READ Num
WHILE Num<> -1
TotalTotal + Num
Module Module1
Sub Main()
DIM Num As Integer=0
DIM Count As Integer=0
DIM Total As Integer=0
DIM Largest As Integer=0
Num=Console.ReadLine()
While Num<>-1
Total=Total+Num
Do
Count=Count+1
TotalTotal+Num
CountCount + 1
CountCount+1
Yes
Num>Largest
No
READ Num
ENDWHILE
Largest Num
AvgTotal/Count
PRINT Avg, Largest
Input
Num
End
Num=Console.ReadLine()
End While
Avg=Total/Count
Console.WriteLine (Avg & Largest)
End Sub
End Module
Practice Questions:
(1) 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
(2) 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.
(3) A geography class decide to measure daily temperatures and hours of sunshine per day over a 12 month period (365 days)
Write an algorithm, using pseudocode, which inputs the temperatures and hours of sunshine for all 365 days, and finally outputs the average (mean) temperature for the year
and the average (mean) number of hours per day over the year.
(4) A small shop sells 280 different items. Each item is identified by a 3 digit code. All items that start with a zero (0) are cards, all items that start with a one (1) are sweets, all
items that start with a two (2) are stationery and all items that start with a three (3) are toys.
Write an algorithm, using pseudocode, which inputs the 3 digit code for all 280 items and outputs the number of cards, sweets, stationery and toys.
(5) A company are 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 werent cars, buses or lorries during the survey
(6) Speed cameras read the time a vehicle passes a point (A) on the road and then reads the time it passes a second point (B) on the same road (points A and B are 100
metres apart). The speed of the vehicle is calculated using:
The maximum allowed speed is 100 kilometres per hour. 500 vehicles were monitored using these cameras over a 1 hour period.
Write an algorithm, using pseudocode, which:
inputs the start time and end time for the 500 vehicles that were monitored
calculate the speed for each vehicle using the formula above
outputs the speed for each vehicle and also a message if the speed exceeded 100 km/hour
output the highest speed of all the 500 vehicles monitored
May/June 2006
16 (a) A formula for calculating the body mass index (BMI) is:
Calculate the BMI for a person whose weight is 80kg and height is 2 metres.
[1]
(b) 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.
[6]
20 Temperatures (C) are being collected in an experiment every hour over a 200 hour period. Write an algorithm, using pseudocode or otherwise, which inputs
each temperature and outputs
how many of the temperatures were above 20C
how many of the temperatures were below 10C
the lowest temperature that was input
[5]
May/June 2007
19 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.
[5]
Oct/Nov 2007
16 (a) Fuel economy for a car is found using the formula:
[1]
(b) The Fuel Economy for 1000 cars is to be calculated using the formula in Question 16(a).
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
the best Fuel Economy (i.e. highest value)
the worst Fuel Economy (i.e. lowest value)
[6]
May/June 2008
19 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.
[5]
Oct/Nov 2008
19 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
May/June 2009
18 A small airport handles 400 flights per day from three airlines:
FASTAIR (code FA)
SWIFTJET (code SJ)
KNIGHTAIR (code KA)
Each flight is identified by the airline code and 3 digits. For example FA 156.
Write an algorithm, using pseudocode or otherwise, which monitors the 400 flights into and out of the airport each day. The following inputs, processing and
outputs are all part of the monitoring process:
input flight identification
calculate number of flights per day for each of the three airlines
output the percentage of the total flights per day by each airline
any validation checks must be included
[5]
[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
the average final speed for all the cars.
[6]
[7]
[3]
[3]
[4]
[4]
[4]
(b) Describe, with examples, two sets of test data you would use to test your algorithm.
[2]
[2]
[6]
[6]
[4]
[4]
[4]
[6]
abcde
01234
The check digit (e) is found by:
multiplying the first and third digits (i.e. a and c) by 3
multiplying the second and fourth digits (i.e. b and d) by 2
adding these four results together to give a total
dividing this total by 10
remainder is check digit (e)
Write an algorithm, using pseudocode or flowchart only, which
inputs 100 five-digit barcodes in the form a, b, c, d, e
re-calculates the check digit for each number and checks whether the input check digit (e) is correct
outputs the number of barcodes which were entered correctly
[5]
[5]
[6]