All posts by Mr Stratton

Computing teacher and a PT at Coltness High School.

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

Logical & Syntax Errors

Here is the pseudo code and VBE code we used today.

If Percent<45 Then
Display Fail
Elseif  Percent>=45  Then
Display D
Elseif  Percent>=50 then
Display C
Elseif  Percent>=60 then
Display B
Elseif Percent>=75then
Display A
Else
Display “Error”
End IF
VB Code

Dim Percent As Integer

Percent = InputBox(“Please enter the percentage”)

If Percent >= 75 Then

MsgBox(“A”)

ElseIf Percent >= 60 Then

MsgBox(“B”)

ElseIf Percent >= 50 Then

MsgBox(“C”)

ElseIf Percent >= 45 Then

MsgBox(“D”)

ElseIf Percent < 45 Then

MsgBox(“Fail”)

Else

MsgBox(“Error”)

End If

Remember the last Else is a error trap and if your program works correctly you should never see it.

Times Table V2 – Solution

  1. Set up variables
  2. For 12 Tables
  3. For 12 sums
  4. Calculate the answer to the current sum
  5. Display the sum
  6. Next sum
  7. Next Table
  8. end program

Refine Step 4

4.1 multiply the user’s number by the current position in the times table and store in answer

Refine Step 5

5.1 Display the user’s number multiplied by position equals to the answer

This is an example of a nested loop, a nested loop is a loop within a loop.

Times Table V1 Design

  1. Set up variables
  2. Ask for and get the times table from user
  3. For 12 sums
  4.         Calculate the answer to the current sum
  5.         Display the sum
  6. Next sum
  7. end program

 

Refine Step 4

4.1 multiply the user’s number by the current position in the times table and store in answer

Refine Step 5

5.1 Display the user’s number multiplied by position equals to the answer

Step wise refinement.

We created a very simple design for a program that allowed the user to add two numbers together. We discussed at length the need for clear and concise steps. These allow the programmer and other to plan the flow of the program.

Add 2 numbers             Mr. Stratton

  1. Get 2 numbers
  2. Add the two numbers
  3. Display the answer

Refine Step 1

1.1     Ask for first number

1.2     Store First Number

1.3     Ask for second number

1.4     Store Second Number

Refine Step 2.

2.1 Set total = First Number + Second Number

Refine Step 3.

3.1     Display the sum

3.2     Display total

Glow problems

I have been having problems all tonight with Glow errors. If you have a question then please leave it in the discussion area and I will get back to you.

You could also mail it to me via Glow mail and I will copy and paste it on the discussion area with an answer.

Or post it on the blog!

Exam Study

A quick reminder that supported study is available from 6 to 7 each night before the exam. Unfortunately I need to pop out for an hour on Thursday, so supported study will be on from about 8:30. REMEMBER I am in school if you want to ask questions.
Good luck on Friday.

2007 SQA Past Paper Marking Scheme

2007 Computing SQA Exam Intro
2007 Computing SQA Exam Section 1
2007 Computing SQA Exam Section 2 Intro
2007 Computing SQA Exam Q 17
2007 Computing SQA Exam Q 18
2007 Computing SQA Exam Q 19