Category Archives: Computing Higher

Listbox into Array Program

REMEMBER! Save all First Class emails etc if you do not all the information will be lost.

Todays Task :
Create a program that stores 20 numbers in a Listbox then input them into a array before displaying them on the screen.

My Design:

Pseudocode:
main steps

1/ Set up variables
2/ Get and store 20 numbers from listbox
3/Display Numbers
4/End Program

Refinements:
2.1/For 1 to 20
2.2/store number
2.3/Next

Here is Mr Stratton’s Awesome Program From Today
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

REMEMBER! Arrays Are Always Passed ByReferance

and Remember!
Homework:
For Tuesday the Multiple Choice sheet.
The end

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

You wanted the best? You got the best. Ladies and gentlemen, the lesson from 11th of November

In computing today we started off with Local and Global variables. Local Variables exist only within a single subroutine, and  cannot be accessed from elsewhere in the code.

Global Variables are created in the main part of the program that can be seen from any part of the program.

We then moved on to Arrays where we learned that ARRAYS ARE ALWAYS PASSED BY REFERENCE!!!!!!!!!!

Which means that data is passed in to a subroutine where it can be changed and then passed back out to other parts of the program.

We then had to code up a program that took and input from the user and passed that value by reference to other subroutines.

Here is the code for that program

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

Also

The game.

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.

program testing and revision

Types of testing

Exceptional – values are not expected e.g “enter a number between 1 and 10” : -5,100,six

Extreme – allowed but at the very limits of the program
e.g “enter a number between 1 and 10” : 1,10

Normal: these are allowed inputs from the user e.g “enter a number between 1 and 10” : 3,8,5

testing table

e.g a quit button

button pushed|Expected|actual
yes | quit | quit
no |not quit|not quit

quick revison

documentation-userguide,technical guide

concatenation-joining strings

variable types:

string – numbers letters and other characters e.g 1,d,{

numeric – integer~whole number
single~decimal number
and boolean

Dim – set up variable

selection
simple – if one comparitor
complex – if more than one comparitor and a logic operator
ElseElif
Selectcase

iteration repition

In class we covered buffers sppoling and storage devices.
Buffering – a buffer is an area of the computers memory that is allocated to allow the transfer of data betweeen the computer and its peripherals. The buffer is mainly used if the fast part of a computer is comunicating with the slower devies that the computer uses, the buffer does this by storing the data temporarily untill it can be dealt with.
peripheral buffer – A printer operates at a much slower rate that what the cpmuter acts, so a buffer is used so taht the program can still run, instead of waiting for every character to be printed out, the data is sent to the printer by the operating system the usually deals with the buffers.
interface buffers – these are usually called Universal Asynchronous Reciever/Transmitter or UART, this is used to relieve the CPU of continuisly stopping what its doing.

Spooling – If large amounts of data are to be sent to one peripheral device, or if that device is shared between a network of computers tehn the preffered method is to spool, this compensates for the different speed of the proccesors and the peripheral. this allows output files to be queued from many programs and then sent to the printer one by one when the printer is ready.

Storage devices – one type of storage is magnetic storage, these can be found on hard disk drives, floppy disks, sip disks and magnetic tape.they are called magnetic storage because their surfaces are coated in a material that responds to a magnetic feild, these storage devices can be fixed or removable.
hard disks can be slit in to multiple levels with in the hard drive, but the disk its self is also split into different areas called tracks, sectors and cylinders.

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