Flowchart Basics by Farhan Rahmat

22
Algorithm Flow Chart Non- Iterative Formula/Value Calculation Pseudoco de Iterative Count based Loop Formula/ Value Calculatio n Counting Condition based Loop Totaling Extreme Values

description

computers

Transcript of Flowchart Basics by Farhan Rahmat

Non-Iterative Algorithm(Simple Input-Process_Output

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 calculation might be conditional, some time your output would be depending on some criteria but in any case there wont 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

StartTempA0TempB0InputTempATempB (TempA-32)/1.8OutputTempBEndStart of FlowchartHere we decide the variable to be used and initialized them. In this particular question we have taken TempA for Temperature (in Fahrenheit) and TempB for Temperature (in Celsius)Input CommandProcess BoxOutput CommandEnd of Flowchartand output temperature in Celsius.

Dry running of flowcharts

Initialzation

TempATempB

00

Input TempATempBOutput TempB

10037=(100-32)/1.837

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 madeWrite 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 AmountIf Amount>Current Balance then Print Withdrawal RefusedIf Amount>Daily Limit then Print Withdrawal RefusedCurrent Balance= Current Balance AmountIf Current Balance 100 Count=50Input NumNum>100CountA CountA + 1NoYesNum>LargestLargest NumNoYesCount Count + 1Print CountA , LargestEndYesNoStartNum0Count0CountA0Largest00outputs the value of the largest (highest) number input

Num0CountA0Largest0Input NumNum= - 1Num>100CountA CountA + 1NoYesNum>LargestLargest NumNoYesInput NumPrint CountA , LargestEndYesNoStartWrite an algorithm, using pseudocode or a flowchart, which inputs a set of positive numbers (which end with -1) outputs how many of the numbers were > 100 outputs the value of the largest (highest) number input

Write an algorithm, using pseudocode or a flowchart, which inputs 50 positive numbers outputs the average (mean) value of the input numbers outputs the value of the largest (highest) number input

Count0Total0Largest00Num0Avg0Count=50InputNumTotal Total + NumNum>LargestLargest NumNoYesCount Count + 1Avg Total / 50Print Avg , LargestEndYesNoStart

Write an algorithm, using pseudocode or a flowchart, which inputs a set of positive numbers (which end with -1) Input NumNum= - 1TotalTotal + NumCountCount + 1Num>LargestLargest NumNoYesInputNumAvg Total / CountPrint Avg , LargestEndYesNoStartNum0Total0Count0Largest00Avg0outputs the average (mean) value of the input numbers outputs the value of the largest (highest) number input

Highest TempBEndTotal Total +TempB7) Add the input value or result of value calculated if average has been asked to calculate8) Set the counting condition if tally is required and increment the respective counting variable with 1.CountB CountB+19) If extreme values such as Highest/Lowest, Tallest/Shortest, Maximum/Minimum is required. Count Count+110) Increment the loop counter by 1 for the next iteration and go back to loop exit condition.isTempBHighest?isTempBHighest?isTempB200000Tax (HValue * 2) / 100NoYesHValue>100000 and HValue50000 and HValue0?InputNum

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 = CD2 = DVD3 = video4 = bookFor 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 werent 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.

Avg(Total/100)EndOutputCountA, CountB,AvgCountACountA+1CountBCountB+1YesNoisTemp>=20?Total=Total+TempCountCount+1StartCount0Temp0CountA0CountB0Total0Avg0NoYesisCount=100?InputTempWrite an algorithm in the form of a flowchart which takes temperatures input over a 100 day period (once per day) and outputs the number of days when the temperature was below 20C and the number of days when the temperature was 20C and above, also print average temperature of 100 days.

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.

NoYesStartCount0High0Low1000isCount=100?OutputHigh,LowEndHighTempNoYesCountCount+1LowTempNoInputTempisTemp>HighYesisTemp