pseudocode with vb by farhan rahmat

32
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

Upload: trynos

Post on 08-Sep-2015

242 views

Category:

Documents


2 download

DESCRIPTION

c

TRANSCRIPT

DescriptionFlow Chart SymbolPseudocodeVB/VB.Net

Variable Declaration & Initialization

TempA0TempB0Weather

[Variable Name] Initial Value

Example:TempA0TempB0 Weather

DIM [Variable Name] As Data Type = Initial Value

Example:DIM TempA As Integer=0DIM TempB As Integer =0DIM Weather As String=

InputInput TempA

READ [Variable Name]Example:READ TempA,[Variable Name]=Console.ReadLine()Example:TempA=Console.ReadLine()

OutputOutput TempB

PRINT [Variable Name],Message/CommentExample:PRINT TempB, BMI, Over weightConsole.WriteLine ((Variable Name] & Message/Comment)Example:Console.WriteLine (TempB & BMI & Over weight)

ProcessTempB (TempA-32)/1.8

TempB (TempA-32)/1.8

TempB=(TempA-32)/1.8

Selection

isTempBHighest Then Highest = TempB

(Note: End If is not needed if the whole IF.THEN statement is written on a single line.)

Selection

isWeather=Foggy?

NoYes

FoggyFoggy+1

isWeather=Cloudy?CloudyCloudy+1YesNo

isWeather=Rainy?RainyRainy+1YesNo

Sunny Sunny +1

IF Weather =Foggy THEN FoggyFoggy+1IF Weather =Cloudy THEN CloudyCloudy+1IF Weather =Rainy THEN Rainy Rainy +1Else Sunny Sunny +1ENDIFCASE [Variable Name ] OF expression : statement expression : statement expression : statementOTHERWISE else statementENDCASE

Example:CASE Weather OF Foggy : FoggyFoggy+1 Cloudy: CloudyCloudy+1 Rainy : RainyRainy+1OTHERWISE SunnySunny+1ENDCASESelect [Variable Name] Case expression list Statements Case Else Else statementEnd Select

Example:Module Module1Sub Main()DIM Weather as StringDIM Foggy as Integer=0DIM Cloudy as Integer=0DIM Rainy as Integer=0DIM Sunny as Integer=0Weather=Console.ReadLine()Select Weather Case "Foggy" Foggy=Foggy+1 Case "Cloudy" Cloudy=Cloudy+1 Case "Rainy" Rainy=Rainy+1 Case Else Sunny=Sunny+1End SelectConsole.WriteLine (Foggy)Console.WriteLine (Cloudy)Console.WriteLine (Rainy)Console.WriteLine (Sunny)End SubEnd Module

(You can try this code online at http://ideone.com/EOBwlK,)

Iteration (Count Based Loop)Count 0Count=100Output"Hello World"Count Count + 1EndYesNoStartCounter could be Count, X, etc.

Things to do within loopFOR Counter Start TO End -------- -------- -------- -------- --------

Things to do outside the loopNEXT------------------------

Example:Print Hello World 100 times:

FOR Count=1 TO 100 PRINT Hello WorldNEXTCounter could be Count, X, etc.

DIM Counter As Datatype

Things to do within loopFOR Counter = Start TO End -------- -------- -------- -------- --------

Things to do outside the loopNEXT------------------------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 SubEnd Module

Iteration (Condition Based Loop)StartCount 0Input CodeCode=9999Print"Hello World"Input CodeEndYesNoREAD [Variable Name]WHILE [Variable Name condition

Things to do within loop DO -------- -------- -------- -------- -------- READ [Variable Name]

Things to do outside the loopENDWHILE------------------------

Example:Print Hello World till code 9999 was entered:

READ Code WHILE Code 9999 DO PRINT Hello World READ CodeENDWHILE

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 Code9999 Console.WriteLine(Hello World) Code=Console.ReadLine() End While End SubEnd Module

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 THEN Highest [Variable]) (IF [Variable]LargestLargest NumNoYesCount Count + 1Print CountA , LargestEndYesNoStartNum0Count0CountA0Largest00Write an algorithm, using pseudocode or a flowchart, which inputs 50 positive numbers outputs how many of the numbers were > 100 Outputs the value of the largest (highest) number input

Num0Count0CountA0Largest0FOR Count1 TO 50 READ Num IF Num>100 THEN CountACountA+1 IF Num>Largest THEN LargestNumNEXTPRINT CountA, Largest

Module Module1 Sub Main() DIM Num As Integer=0 DIM Count As Integer=0 DIM CountA As Integer=0 DIM Largest As Integer=0 For Count= 1 To 50 Num=Console.ReadLine() If Num>100 Then CountA=CountA+1 If Num>Largest Then Largest=Num Next Console.WriteLine (CountA & Largest) End SubEnd Module

9) Set the counting condition if tally is required and increment the respective counting variable with 1.EndAvgTotal/CountOutputAvg, CountA, CountB Highest, Lowest12) Always Calculate Average outside loop.13) Always Print Average, Counting Variables, and Extreme Values outside the loop.14) End of FlowchartYesNo11) Input command for next iteration before returning to loop,Highest TempB10) If extreme values such as Highest/Lowest, Tallest/Shortest, Maximum/Minimum is required. isTempB>Highest?isTempB