Category Archives: 6. External Sites

Homework for Tue 8th Nov 2011

A program is required which will take in a student’s Standard Grade ComputingStudies grade and decide which course, Intermediate 1, Intermediate 2 or Higheris the most appropriate. Standard Grade is issued on a scale of 1 to 7.
  1. 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!)
  2. 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.
  3. 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.
  4. 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

Homework for 2/11/11

Sorry folks, I need to remember to check that automatic tasks launch, when they are meant to.

  1. What is meant when software is said to be modular?
  2. What is the disadvantage in using GLOBAL variables?
  3. What is the advantage in using LOCAL variables?
  4. Describe what a local variable is used for.
  5. Why should care be taken when using global variables?
  6. Give an extract of code which illustrates how a formal and actual parameters are used.
  7. Illustrate the difference in syntax between procedures/subroutines and functions.
  8. What is the advantage in using subprograms?
  9. Describe parameter passing by value.
  10. Describe parameter passing by reference.

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