Tag Archives: Standard Algorithims

Today’s little problem

So this got the heart racing today for some of you 🙂

Another subprogram in the building management software is used to find the range of temperatures in a building in one day. The temperature is recorded every 15 minutes within a 24 hour period and stored in a list.
Use pseudocode to design one algorithm to find both the highest and lowest temperatures in this list. (5Marks)

It was from the 2010 past paper, I asked you to code out the program. This program looks complicated but was a walk in the park for some people in the class, the rest of you need to go over the standard algorithms.

I gave you this snippet of code to create the data.

For counter = 1 To 96

temps(counter) = Int((Rnd() * 30) + 1)

Next Continue reading Today’s little problem

Module Library

Function getValidNumber(byVal Min As Single, byVal Max As Single) As Single
‘ Call this function with parameters for Min and Max
‘ Example
‘ Number2 = getValidNumber(-10, 10)
Dim Num As Single
Dim NumberOK As Boolean
NumberOK = False
Do
Num = InputBox(“Enter a number between ” & Min & ” and ” & Max)
If (Num >= Min) And (Num <= Max) Then NumberOK = True
If Not NumberOK Then MsgBox (“That was not in the valid range”)
Loop Until NumberOK
Return Num
End Function
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub fill_int_array (ByRef local_array() As Integer)
‘ fill a 10 element 1-D array with integers
‘ Example
‘ Call Fill_int_Arry(newNumber())
Dim index As integer
For index = 0 to 9
local_array(index) = InputBox(“Enter integer for element ” & index)
Next
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub fill_random_int_array (ByRef local_array() As Integer)
‘ fill a 10 element 1-D array with random integers
‘ Example
‘ CAll fill_random_int_arry(RndNumbers())
Dim index As integer, Num As Integer
Randomize
For index = 0 to 9
Num = Int (Rnd * 100) + 1
local_array(index) = Num
Next
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub display_int_array (ByRef local_array() As Integer)
‘display a 1-D array in 2 list boxes
‘ Exaple
‘ Call Display_Int_Array(RndNumbers())
Dim index As integer
For index = 0 to 9
lstIndex.Items.Add (index)
lstElement.Items.Add (local_array(index))
Next
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub linear_search1(ByVal target As Integer, ByRef int_array() As Integer, ByRef position As Integer)
‘basic linear search on array of integers
Dim pointer As Integer
Dim found As Boolean
found = False
For pointer = 0 To 9
If int_array(pointer) = target Then
MsgBox (“Target found at position ” & pointer)
found = True
position = pointer
End If
Next
If Not found Then MsgBox (“Target not found in array”)
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub linear_search2(ByVal target As String, ByVal max_index As Integer, ByRef test_array() As String, ByRef position As Integer)
‘more efficient linear search on array of strings
Dim pointer As Integer
Dim found As Boolean
found = False
pointer = 0
Do While (found = False) And (pointer <= max_index)
If test_array(pointer) = target Then
MsgBox(“Target found at position ” & pointer)
found = True
position = pointer
End If
pointer = pointer + 1
Loop
If Not found Then MsgBox(“Target not found in array”)
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub count_occurrences(ByVal target As String, ByRef char_array() As String, ByRef counter As Integer)
‘counting occurrences with an array of 10 strings
Dim pointer As Integer
counter = 0
For pointer = 0 To 9
If char_array(pointer) = target Then counter = counter + 1
Next
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub find_minimum(ByRef num_array() As Single, ByRef minimum As Single)
‘finding minimum in an array of 10 real numbers
Dim pointer As Integer
minimum = num_array(0)
For pointer = 0 To 9
If num_array(pointer) < minimum Then minimum = num_array(pointer)
Next
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub find_maximum(ByRef int_array() As Integer)
‘finding maximum in an array of integers
Dim pointer As Integer
Dim maximum As Integer
maximum = int_array(0)
For pointer = 0 To 14
If int_array(pointer) > maximum Then maximum = int_array(pointer)
Next
MsgBox (“The largest number in that list was ” & maximum)
End Sub