All posts by Mr Stratton

Computing teacher and a PT at Coltness High School.

VB Program for Displaying the Average of 20 Marks

Below is the program from today’s lesson. It is not completed, I would like you to Change the program to display the average with two decimal places. Please add your solution with a comment below.

Private Sub CmdStart_Click()
'load an Array with data from a list box
'list box is hidden on the form
'List Boxes count from 0 so add a heading to the fist item
Dim IntNumber(20) As Integer
Dim StrName(20) As String
Dim intavg As Integer
Call Load_Array(IntNumber(), StrName())
Call Calc_Average(IntNumber(), intavg)
Call Display_Array(IntNumber(), StrName(), intavg)
End Sub

Private Sub Load_Array(ByRef Number() As Integer, ByRef StrName() As String)
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Number(Counter) = LstNumbers.List(Counter)
StrName(Counter) = LstNames.List(Counter)
Next
End Sub


Private Sub Calc_Average(ByRef IntNumber() As Integer, ByRef intavg As Integer)
Dim IntSum As Integer
For Counter = 1 To 20
IntSum = IntSum + IntNumber(Counter)
Next
intavg = IntSum / 20
End Sub

Private Sub Display_Array(ByRef Number() As Integer, ByRef StrName() As String, ByVal intavg)
'Standard procedure as used in previous programs
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Form1.Print StrName(Counter) & " scored " & Number(Counter) & "%"
Next
Form1.Print "Average = " & intavg & "%"
End Sub

Transferring data from a listbox to an array

The programmer would enter the information in a list box then hide this box from the user (Preferences). This allows the program to use the same test data at all times. This is the same as READ DATA in other languages. Shown below is the program from today’s lesson.

Private Sub CmdStart_Click()
'load an Array with data from a list box
'list box is hidden on the form
'List Boxes count from 0 so add a heading to the fist item
Dim Number(20) As Integer
Call Load_Array(Number())
Call Display_Array(Number())
End Sub

Private Sub Load_Array(ByRef Number() As Integer)
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Number(Counter) = LstNumbers.List(Counter)
Next
End Sub

Private Sub Display_Array(ByRef Number() As Integer)
'Standard procedure as used in previous programs
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Form1.Print Number(Counter)
Next
End Sub

2 Different ways of handling Array Parameters

Method 1 – pass the whole array ByVal so that the procedure handles the entire array

Option Explicit
Private Sub CmdStart_Click()
'Set up variables
Dim multiple As Integer
Dim answers(12) As Integer 'Array of 12 numbers
'Get Multiplier from user
Call GetValue(multiple)
'Calculate Answers
Call CalcAnswer(answers(), multiple)
'Display table
Call DisplayTable(answers(), multiple)
End Sub


Private Sub GetValue(ByRef intvalue As Integer)
intvalue = InputBox("Please enter the value")
End Sub


Private Sub DisplayTable(ByRef answers() As Integer, ByVal multiple)
Dim counter As Integer
Dim Answer As Integer
For counter = 1 To 12
lstOutput.AddItem multiple & " X " & counter & " = " & answers(counter)
Next
End Sub


Private Sub CalcAnswer(ByRef answers() As Integer, ByVal multiple)
Dim counter As Integer
For counter = 1 To 12
answers(counter) = multiple * counter
Next
End Sub

Method 2 – Pass a single item of the array so that the procedure only handles integers

Option Explicit
Private Sub CmdStart_Click()
'Set up variables
Dim Multiple As Integer
Dim counter As Integer
Dim Answers(12) As Integer 'Array of 12 numbers
'Get Multiplier from user
Call GetValue(Multiple)
For counter = 1 To 12
'Calculate Answers
Call CalcAnswer(Answers(counter), Multiple, counter)
'Display table
Call DisplayTable(Answers(counter), Multiple, counter)
Next
End Sub


Private Sub GetValue(ByRef intvalue As Integer)
intvalue = InputBox("Please enter the value")
End Sub


Private Sub DisplayTable(ByVal Answer, ByVal Multiple, ByVal counter)
lstOutput.AddItem Multiple & " X " & counter & " = " & Answer
End Sub

Private Sub CalcAnswer(ByRef Answer As Integer, ByVal Multiple, ByVal counter)
Answer = Multiple * counter
End Sub

Both methods have their uses and differing programs call for differing strategies.

Parameter Passing with Arrays

The program you are about to see is known as a top down design this means that a problem is taken and broken down into smaller parts, worked out and executed.
You will see in this program that sub programs are used, these correspond to each level of the program this means that the program will run and only pick up on mistakes when it gets to that level. Also you could use breakpoints which will stop at the point you allocate the breakpoints too.
This type of program is known as a parameter this means that the pieces of information passes in and out of the program, to make the program work smoothly you must choose either byValue or byRefrence, by val means that the program does not change where as byref means the program will change.

Display the times table of the users choice
Option Explicit
Private sub CmdStart_Click()
‘Set up variables
Dim multiple As integer
‘Get Multiplier from user
Call GetValue(multiple)
‘Display table
Call DisplayTable(multiple)
End sub
Private sub GetValue(ByRef intvalue As Integer)
intvalue = InputBox(“Please enter the value”)
End sub
Private sub DisplayTable(ByVal multiple)
Dim Counter As Integer
Dim Answer As Integer

For counter = 1 to 12
Answer = multiple * counter
lstOutput.AddItem multiple & “X “ & Counter & “=” & Answer
Next
End Sub

Given you have typed in the correct coding the program should work however you need to look out for the byval and byref parts, in your second private sub because it is in the list box this will be what the answer is and multiple of your number, byVal is the correct one to use, however watch out if you put byRef in it’s place, this would keep the value as 0 and multiply by 0 and not the user’s input, this is because byVal stays the same meaning because the computers first number is 0 it will stay as 0, whereas byRef changes it and would therefore display the user’s input choice of multiple.

Binary Numbers

Computers store all data as binary, numbers are easily stored this way.

Humans work with a base 10 numbering system (we have 10 fingers), however computers can only store data using electricity (on and off) which is base 2.

This article explains binary in a simple way

128  64  32  16  8  4  2  1 are the headings at the top of the binary numbers so 10001000 would be 128+8 which is 136

Practice here before attempting some bingo

For you exams you will be required to show the working for binary conversion, however, you can check this by using a calculator that has a binary function. The calculator below was found in a local pound shop, you can check if your calculator has binary by checking the buttons for BIN, OCT, DEC, HEX.

You can make a little Binary Calculator with the attached file. The photographs show it in action, simply fold the flaps to make the binary number and add the numbers shown. (teachers note that for 2’s complement just change +128 to -128)

Random List Generation and Array Handling

Well it looks like our Tuesday bloggers have let us down 🙁

Today’s lesson was to create a program to store 50 random number in an array and then to display that array in reverse. Once the task was given the pupils were asked to solve the problem. Most went straight to the computer and never thought about analysis or design, this led to confusion about the task and pupils were brought back and the task discussed again.

The Main steps were given to the pupil

  1. Set up variables
  2. Populate list with random numbers
  3. Create reverse list
  4. Display forward and reverse lists
  5. end program

After discussion we had the following refinements for step 2

  1. For 50 numbers
  2. Generate random whole number between 1 and 20
  3. Store number at next location in Array
  4. Next

In VB this is written as

For IntCounter = 1 to 50
IntNumber=int(rnd*20)+1
IntForward(Intcounter)=IntNumber
Next

Program Code for Times Table

Change the program code below so that the program performs the calculation in a separate procedure from the display.

Option Explicit
Private Sub CmdStart_Click()
'Set up variables
Dim multiple As Integer
'Get Multiplier from user
Call GetValue(multiple)
'Display table
Call DisplayTable(multiple)
End Sub


Private Sub GetValue(ByRef intvalue As Integer)
intvalue = InputBox("Please enter the value")
End Sub


Private Sub DisplayTable(ByVal multiple)
Dim Counter As Integer
Dim Answer As Integer
For Counter = 1 To 12
Answer = multiple * Counter
lstOutput.AddItem multiple & " X " & Counter & " = " & Answer
Next
End Sub

Software Development 2010 – where we are

You should find the powerpoint I used in class attached at the bottom of this entry.  Here are some of the example I used in class.

Squeakland – the home of etoys

Sugar on a stick – OLPC’s OS

Please see the PT ICM blog for minecraft

Software Development Catchup

The code we have created so far at the end of the period is given below, please see if you can finish it off.

Option Explicit
Private Sub CmdStart_Click()
‘Set up variables
Dim multiple As Integer
‘Get Multiplier from user
Call GetValue(multiple)
‘Display table
Call DisplayTable
End Sub
Private Sub GetValue(ByRef intvalue As Integer)
intvalue = InputBox(“Please enter the value”)
End Sub
Private Sub DisplayTable()
End Sub