Private Sub Command1_Click()
‘Set up variables
Dim NumA As Single
Dim NumB As Single
Call Get2Numbers(NumA, NumB)
Call DisplaySum(NumA, NumB)
Sub Get2Numbers(ByRef NumA, ByRef NumB)
NumA = InputBox(“What is the first number”)
NumB = InputBox(“What is the second number”)
Sub DisplaySum(ByVal NumA, ByVal NumB)
MsgBox (NumA + NumB)
We were given the following program specification.
Write a program that generates 50 random whole numbers between 1 and 50. display them in a list box in reverse order.
First we need to create the main steps. You should ALWAYS do this for any program specification’s that your given. It makes the task much easier.
1. Set up variables
2. populate list
3. reverse list
4. display list
5. End program
We have already donesteps 2 and 4 last week. So we did not need the refinements for step 4, but were given the refinements for steps 2 and 3
Ref Step 2.
2.1 For 50 numbers
2.2 create a random number between 1 and 20
2.3 Store this number in the array
2.4 Next Number
The VB code for these steps are:
For Intcounter = 1 to 50
Intnumber = Int(Rnd*20)+1
Intarray(Intcounter) = Intnumber
This code creates 50 random numbers.
Ref Step 3:
3.1 For all 50 numbers.
3.2 Copy item at next position to new array.
The VB code for these refinements are.
For Intcounter = 1 to 50
Intbackwards(Intcounter) = IntArray(51-Intcounter)
This VB coding takes the random numbers generated and displays them in reverse order. The part of the code thats in Italics, is the peice of code that does this. If Intcounter = 1, then the position of IntArray thats being displayed first would be (51-1) and would display the 50th position. If the counter was 2, the 49th position would be displayed etc, if it was 3 then position 48 etc.
We used a list box to display these numbers. The reason we used a list box rather than just print it onto the form is that it is easier to read due to the scroll option that the list box provides.
We were given the new notes on the topic and started off by discussing the various devices that can be used on computers today thanks to the standardization of the interfaces the various manufactures. We then discussed the various aspects of keyboards and learned how we could make an educated guess of which would be best to suit our needs. On the subject of keyboards we agreed on the layout, hotkeys, number of keys, interface, durability/robustness, physical size, accuracy and price were all important contributing factors that you would need to consider when buying a keyboard. We also reviewed the blog from last week and then discussed the fastest method of typing/texting. We then learned that the keys used most often are positioned on the left hand side of the keyboard in the layout design called ‘QWERTY’ which was created to work with typewriters as only one arm of the old machine could be operational at the one time so a way was made to slow typists down so they would not jam the machine. This layout is not the only one available but it is the one we are used to although not the fastest. For notes on keyboards go to page 77 and 78 of the new notes. Keyboards work by a matrix of wires (a criss-cross pattern) which work by using coordinates to define which key is what. We discussed what type of keyboards are available to buy like; ultra small, infrared, LED buttons, left or right handed or hand typing and virtual keyboards. You can also get modified keyboards. How a keyboard works on page 78. Additionally we started on scanners and discussed how they work. Notes on this part are on page 80.
Today we started a new topic called peripherals.
We started off the lesson by discussing the point of interfaces. We learned that standardized interfaces were introduced so that people could mix and match peripharels from different companies. When apple came out the only peripharels that worked with their computers were also made by apple. This upset the customers and had to be changed. They later introduced usb ports so other add ons would work. However this meant that all their previous peripharels were uncompatible.
We learned about comparing and contrasting different computer products so that an educated purchase could be made for example looking at shape,size and price. To compare actual products we used the internet to try and find good computers to replace the ones that are already in the classroom.
We did this for the rest of the lesson.
Today we furthered yesterday’s lesson of arrays by discussing simpler ways to create programs by using arrays and providing examples.
Programmers prefer to count from 0 to 9, but we will count 1 to 10 as it is simpler.
Counting from 1 to 10 in play –
Dim IntAge(10)As Integer
Dim IntCounter As Integer
For IntCounter = 1 to 10
IntAge(counter) = InputBox(“Please enter number”)
In the line “Dim IntAge(10) As Integer” the area where the 10 is – inside brackets – is where the programmer would enter the number of numbers they wish for the array to create.
When creating programs like this, only the programmer should be allowed to set number of items in the array to be created as if a user were to enter a high number such as one million the program would use a lot of space and eventually have the computer face problems with all it’s RAM gone.
“No_of_items = 100
Dim Name(No_of_items) as String
DimAge(No_of_items) As Integer”
The above code would create a simpler program because if any changes were needed all you would have to do is change one line of code.
Sometimes program code has to be written out for people who can not read a language such as Visual Basic. This is known as pseudocode. An example of how to do this is here –
Refining Step 3
3.1 For 100 Numbers (this would be seen as For Counter = 100 in VB_
3.2 Add current number to total
3.3 Next Number
3.4 Average = Sum total/100
Sometimes, these steps may not be clear enough and require a further explanation such as point 3.2, here we further it – Refining step 3.2
3.2.1 total = total + number
Refining Step 4
4.1 Display the sum of the number
4.2 Display the average
4.3 Display the values
Refining Step 4.3 –
4.3.1 For 100 numbers
4.3.2 Display Current Number
4.3.3 Next Number
Today’s lesson introduced 1-D arrays. The class started off by creating 3 programs.
1. Create a program to take in 3 numbers and display the sum and average.
2. modify the program to work with 5 numbers.
3. Modify the program to display the numbers after it displays the sum and average.
The class were given the main steps and asked to refine it, before starting the task. Each task was to be saved in separate folders.
The class was then asked to modify the program for 100 numbers. This led to discussion about the need for a new data type that could store a list of values, this data type is called Array.
Scratch was used to show how arrays can be used. The class then looked at the DIM command’s help page to discover how to set up an Array.
DIM IntNumber(50) as Integer
Where (50) is the number of items in the array.
The program was modified to allow entry of 100 values before the period finished.
Write the refinements for the main program given for 100 numbers.
- Set up Variables
- Get 100 values from user
- Calculate Average and Sum of values
- Display the Average, Sum and values
- End program