You are on page 1of 12

5/27/2021 Algorithms

Algorithms

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

computing.outwood.com/NEA/vb/algorithms-vb.html 1/12
5/27/2021 Algorithms

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

computing.outwood.com/NEA/vb/algorithms-vb.html 2/12
5/27/2021 Algorithms

Flowchart

Example 3 - Average Calculator - Iteration (FOR)

Visual Basic

computing.outwood.com/NEA/vb/algorithms-vb.html 3/12
5/27/2021 Algorithms

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

computing.outwood.com/NEA/vb/algorithms-vb.html 4/12
5/27/2021 Algorithms

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

computing.outwood.com/NEA/vb/algorithms-vb.html 5/12
5/27/2021 Algorithms

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

computing.outwood.com/NEA/vb/algorithms-vb.html 6/12
5/27/2021 Algorithms

Flowchart

Example 5 - Login System - Reading from a File

Visual Basic

computing.outwood.com/NEA/vb/algorithms-vb.html 7/12
5/27/2021 Algorithms

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) & vbNewLin
e & details(6))
End If
End While
If found = False Then
MessageBox.Show("Incorrect login details entered")
End If

Pseudocode

Flowchart

computing.outwood.com/NEA/vb/algorithms-vb.html 8/12
5/27/2021 Algorithms

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

computing.outwood.com/NEA/vb/algorithms-vb.html 9/12
5/27/2021 Algorithms

Flowchart

Example 7 - Measurement Conversion - Functions

Visual Basic

computing.outwood.com/NEA/vb/algorithms-vb.html 10/12
5/27/2021 Algorithms

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Hand


les 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

computing.outwood.com/NEA/vb/algorithms-vb.html 11/12
5/27/2021 Algorithms

OGAT Computer Science 2017. v1.2

computing.outwood.com/NEA/vb/algorithms-vb.html 12/12

You might also like