Algorithms
Below is a series of different programs that have pseudocode and flowcharts completed for them. Use them to try and relate your algorithm designs to your code.
Example 1 - Teddy Bears Program - Sequencing
Visual Basic
Dim hours As Decimal = txtHours.Text
Dim bears As Integer = txtBears.Text
Dim hourswage As Decimal = hours * 7
Dim bearswage As Decimal = bears * 0.45
Dim total As Decimal = hourswage + bearswage
MessageBox.Show("Your total wage is " & FormatCurrency(total))
Pseudocode
Flowchart
Example 2 - Water Temperature Program - Selection
Visual Basic
Dim fahrenheit As Decimal = txtFahrenheit.Text
Dim centigrade As Decimal = (fahrenheit - 32) * (5 / 9)
If centigrade <= 0 Then
MessageBox.Show("Water Frozen")
ElseIf centigrade >= 100 Then
MessageBox.Show("Water Boiling")
Else
MessageBox.Show("Water is neither frozen or boiling")
End If
Pseudocode
Flowchart
Example 3 - Average Calculator - Iteration (FOR)
Visual Basic
Dim total As Integer = 0
Dim numbers As Integer = InputBox("How many numbers do you want to enter?")
For x = 1 To numbers
Dim latest As Integer = InputBox("Enter a number:")
total = total + latest
Next
Dim average As Decimal = total / numbers
MessageBox.Show("The total of the numbers was " & total.ToString & vbNewLine &
"The average of the numbers was " & Decimal.Round(average, 2).ToString)
Pseudocode
Flowchart
Example 4 - Timestable Program - Iteration (FOR)
Visual Basic
Dim number As Integer = txtNumber.Text
Dim total As Integer
For i = 0 To 12 Step 1
total = number * i
lstOutput.Items.Add(txtNumber.Text + " x " + i.ToString + " = " + total.ToString)
Next
Pseudocode
Flowchart
Example 4 - Menu Selection - Iteration (WHILE)
Visual Basic
Dim valid As Boolean = False
While valid = False
Dim choice As Integer = InputBox("Enter which option you want: ")
If choice = 1 Or choice = 2 Or choice = 3 Then
valid = True
End If
End While
MessageBox.Show("Let's Go!")
Pseudocode
Flowchart
Example 5 - Login System - Reading from a File
Visual Basic
Dim username As String = txtUsername.Text
Dim password As String = txtPassword.Text
Dim file As New StreamReader("C:\Logins.csv")
Dim details As Array
Dim found As Boolean = False
While (file.EndOfStream = False)
details = file.ReadLine.Split(",")
If details(0) = username And details(1) = password Then
found = True
MessageBox.Show("Email: " & details(0) & vbNewLine &
"Name: " & details(2) & " " & details(3) & vbNewLine &
"Address:" & vbNewLine & details(4) & vbNewLine & details(5) & vbNewLine & details(6))
End If
End While
If found = False Then
MessageBox.Show("Incorrect login details entered")
End If
Pseudocode
Flowchart
Example 6 - Product Catalogue - Writing to a File
Visual Basic
Dim barcode As String = txtBarcode.Text
Dim description As String = txtDescription.Text
Dim price As String = txtPrice.Text
Dim file As New StreamWriter("C:\Products.csv", True)
file.WriteLine(barcode & "," & description & "," & price)
file.Close()
MessageBox.Show("Product Added to Catalogue")
Pseudocode
Flowchart
Example 7 - Measurement Conversion - Functions
Visual Basic
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim measurement As Decimal = txtMeasurement.Text
Dim selection As String = cmbConversion.Text
If selection = "M to KM" Then
MessageBox.Show(measurement.ToString + " miles in KM is " + mtokm(measurement).ToString)
ElseIf selection = "KM to M" Then
MessageBox.Show(measurement.ToString + " KM in miles is " + kmtom(measurement).ToString)
Else
MessageBox.Show("Please enter a valid selection")
End If
End Sub
Function mtokm(ByVal number As Decimal)
Dim answer As Decimal = number * 1.6
Return answer
End Function
Function kmtom(ByVal number As Decimal)
Dim answer As Decimal = number * 0.62
Return answer
End Function
Pseudocode
Flowchart