Category Archives: 2. Software Development
Homework for Tue 8th Nov 2011
- Write the pseudocode for the part of the program which checks that the grade entered is a number between 1 and 7. (You should not need all the lines given!)
- Write the Visual BASIC line of code which displays a message box if an invalid grade is entered. The grade is stored in a variable called grade.
- Write the Visual BASIC line of code which displays a message box showing “Int 2” when the grade entered is either a 3 or a 4. The grade is stored in a variable called grade.
- Explain briefly what each of the following algorithms does:
- Input validation
- Finding a maximum
- Finding a minimum
- Counting occurrences
- Linear search
4b. Write the pseudocode for the algorithms above
5. An array can be used to store variables. Explain what an array is, by using an example.
- The values I am going to store in my example are:
- I am going to create my array using this line of Visual BASIC code:
- My array works like this:
Dice Roll Program
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'1 Set up Var
Dim dice(6000) As Integer
Dim results(6) As Integer
Dim target As Integer
'2 Roll 6000 dice
Call RollDice(dice)
'3 For Each Side of the dice
For Target = 1 To 6
'4 Count each side
Call count_occurrences(Target, dice, results(Target))
'5 next
Next
'6 Display
Call Display(results)
End Sub
Private Sub RollDice(ByRef dice() As Integer)
Dim counter As Integer
Randomize()
For counter = 1 To 6000
dice(counter) = Int(Rnd() * 6) + 1
Next
End Sub
Private Sub count_occurrences(ByVal target As Integer, ByRef Int_array() As Integer, ByRef counter As Integer)
'counting occurrences with an array of 10 strings
Dim pointer As Integer
counter = 0
For pointer = 1 To 6000
If Int_array(pointer) = target Then counter = counter + 1
Next
End Sub
Private Sub Display(ByRef results() As Integer)
Dim Counter
'clear listbox
lstOutput.Items.Clear()
'Display
For Counter = 1 To 6
lstOutput.Items.Add(Counter & vbTab & results(Counter))
Next
End Sub
End Class
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 SingleDim NumberOK As BooleanNumberOK = FalseDoNum = InputBox(“Enter a number between ” & Min & ” and ” & Max)If (Num >= Min) And (Num <= Max) Then NumberOK = TrueIf Not NumberOK Then MsgBox (“That was not in the valid range”)Loop Until NumberOKReturn NumEnd 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 integerFor index = 0 to 9local_array(index) = InputBox(“Enter integer for element ” & index)NextEnd 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 IntegerRandomizeFor index = 0 to 9Num = Int (Rnd * 100) + 1local_array(index) = NumNextEnd 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 integerFor index = 0 to 9lstIndex.Items.Add (index)lstElement.Items.Add (local_array(index))NextEnd Sub++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Private Sub linear_search1(ByVal target As Integer, ByRef int_array() As Integer, ByRef position As Integer)‘basic linear search on array of integersDim pointer As IntegerDim found As Booleanfound = FalseFor pointer = 0 To 9If int_array(pointer) = target ThenMsgBox (“Target found at position ” & pointer)found = Trueposition = pointerEnd IfNextIf 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 stringsDim pointer As IntegerDim found As Booleanfound = Falsepointer = 0Do While (found = False) And (pointer <= max_index)If test_array(pointer) = target ThenMsgBox(“Target found at position ” & pointer)found = Trueposition = pointerEnd Ifpointer = pointer + 1LoopIf 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 stringsDim pointer As Integercounter = 0For pointer = 0 To 9If char_array(pointer) = target Then counter = counter + 1NextEnd Sub++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Private Sub find_minimum(ByRef num_array() As Single, ByRef minimum As Single)‘finding minimum in an array of 10 real numbersDim pointer As Integerminimum = num_array(0)For pointer = 0 To 9If num_array(pointer) < minimum Then minimum = num_array(pointer)NextEnd Sub++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Private Sub find_maximum(ByRef int_array() As Integer)‘finding maximum in an array of integersDim pointer As IntegerDim maximum As Integermaximum = int_array(0)For pointer = 0 To 14If int_array(pointer) > maximum Then maximum = int_array(pointer)NextMsgBox (“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 ThenDisplay FailElseif Percent>=45 ThenDisplay DElseif Percent>=50 thenDisplay CElseif Percent>=60 thenDisplay BElseif Percent>=75thenDisplay AElseDisplay “Error”End IF
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
- Set up variables
- For 12 Tables
- For 12 sums
- Calculate the answer to the current sum
- Display the sum
- Next sum
- Next Table
- 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
- Set up variables
- Ask for and get the times table from user
- For 12 sums
- Calculate the answer to the current sum
- Display the sum
- Next sum
- 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
- Get 2 numbers
- Add the two numbers
- 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
Summer Holidays
I have been asked loads of times “Sir, what can I do to be prepared for the Higher course?”, the answer is very simple. Download MS Visual Basic Express and LTS’ notes.
Work through the notes during the holidays and you will be well prepared for next year.