Thursday, May 8, 2008

Subroutines

Subroutines and functions allow us to package similar instructions together. We've seen built-in subroutines before like:

Math.Sqrt()
Int()
Math.Round()
FormatNumber()
FormatCurrency()
FormatPercent()
str.IndexOf()
str.Length()
str.Substring()
...etc...


Each one of these subroutines/functions does something useful. We can use them to do a certain job, whether it is changing a number value in some way, formatting output or figuring out certain characteristics of strings.

We also learned that we can make our own functions and subroutines to do things that we decide. So let's say that we want to write a print subroutine that will just print out our report, and a calculate() function that will take in three of the expenses and add them together and return the answer. Take a look at the code:
Click for larger image.



You just have to make sure to write the function and subroutine code outside of the button1_click() subroutine and inside the Class Form1 block.

Tuesday, May 6, 2008

2-Dimensional Arrays

We can declare 2-Dimensional arrays by using the following statement:

Dim arrayName(r,c) As Type

Where arrayName is the name of the array you have created and Type is the array's type. The subscripts r and c represent the Row and Column of the array.

So we could declare an array as type double to hold students' grades:

Dim grades(3,3) As Double
'Four students with four grades


This creates a 4x4 matrix with rows and columns starting at 0 and ending at 3:

0 1 2 3
+---+---+---+---+
0 | | | | |
+---+---+---+---+
1 | | | | |
+---+---+---+---+
2 | | | | |
+---+---+---+---+
3 | | | | |
+---+---+---+---+

We can set any cell in the array by issuing a call such as:

grades(1,1) = 90
grades(3,0) = 99
grades(1,0) = 100

With the above code our resulting matrix will look like:

0 1 2 3
+---+---+---+---+
0 | | | | |
+---+---+---+---+
1 |100| 90| | |
+---+---+---+---+
2 | | | | |
+---+---+---+---+
3 |99 | | | |
+---+---+---+---+

If we wanted to set all the cells to a particular value, such as 0 for example, we would have to use a nested loop:

For i As Integer=0 To 3
For j As Integer=0 To 3
grades(i,j) = 0
Next
Next


We can also declare the 2-dimensional array without a size as we can with regular arrays:

Dim arrayName(,) As Type
Dim array2(,) As Type = {{ROW0},{ROW1},{ROW2},...,{ROWm}}

ReDim arrayName(r,s)
ReDim Preserve arrayName(r,s)

Real Examples:

Dim grades(,) = {{99,50,80},{100,80,70},{90,88,90}}
ReDim Preserve grades(3,4)

GetUpperBound

There are two different GetUpperBound calls for 2-dimensional arrays:

arrayName.GetUpperBound(0)
arrayName.GetUpperBound(1)

The first call gives you the upperbound on the number of rows, while the second one gives you the upperbound on the number of columns.

Thursday, May 1, 2008

Some Exam Problems

Problems from the first sample test are as follows (Hopefully without typos):

1) D
2) A
3) C
4) C
5) C
6) C
7) C
8) D
9) B
10) B

Discussion:
Code from #10:
Dim place, i As Integer
Dim character, sentence, phrase(9) As String
sentence = "Every path hath a puddle."
place = 0
i = 0
character = sentence.Substring(place, 1)
Do
If character = " " Then
phrase(i) = sentence.Substring(0, place)
i += 1
End If
place += 1
character = sentence.Substring(place, 1)
Loop Until character = "."


The answer is ``Every path".

We have the following variables:
i 'The current index of the array
place 'The current location in the sentence string
character 'The current character at which we are looking
sentence 'String: "Every path hath a puddle."
phrase() 'Array of size 10


What the code does, loosely, is going through the sentence string one character at a time and every time it hits a space, it saves the all of the i+1 words in the current array index i into the array phrase().

If we look at the part of the code that actually changes the array (highlighted above) then we can see that every time the array is updated it starts at the very beginning and goes up to place characters. Therefore the final array should look like:

0 1 2 ...
+-------+------------+-----------------+----
| Every | Every path | Every path hath | ...
+-------+------------+-----------------+----

Where every cell has one extra word in it from the beginning. The program stops when character = "." which is at the end of the sentence string.

Programming Problem:
a)

Dim arr(1000) As Integer
For i As Integer = 1 To 1000
arr(i) = CInt(InputBox("Enter number: " ,""))
Next

For j As Integer = 1000 To 1, Step -1
lstBoxOut.Items.Add( arr(j) )
Next

b)
Dim arr(1000) As Integer
For i As Integer = 1 To 1000
arr(i) = CInt(InputBox("Enter number: " ,""))
Next

For j As Integer = 1000 To 1, Step -1
If arr(j) Mod 2 = 0 Then
lstBoxOut.Items.Add( arr(j) )
End If
Next

Second Practice Exam

You can find a past exam 2 here. I made a mistake on the first question when I gave you the answers. Here are the correct answers:

1) D
2) C
3) B
4) B
5) D
6) D
7) C
8) B
9) C
10) A

Part II
a) 70^2 = 4900
b) 24^2 = 576

Part III
Answer: 3

Discussion:
The first two loops simply save all of the numbers from the files into arrays a() and b()

sr = IO.File.OpenText("DATA1.TXT")
For k As Integer= 0 To 19
a(k) = CDbl(sr.ReadLine)
Next
sr.Close()
sr = IO.File.OpenText("DATA2.TXT")
For k As Integer = 0 To 19
b(k) = CDbl(sr.ReadLine)
Next


After this, a() contains:
3, 2, 5, 1, 7, 8, 3, 5, 6, 2, 3, 6, 1, 6, 5, 5, 7, 2, 5, 3

and b() contains:
5, 3, 3, 4, 8, 2, 3, 5, 9, 5, 3, 7, 3, 7, 6, 3, 2, 1, 3, 4

This is in order so a(0) equals 3 and a(1) equals 2; b(0) equals 5 and b(1) equals 3 etc...

The last loop is what changes variable c before it is output:

For k As Integer = 0 To 19
If a(k) = b(k) Then
c += 1
End If

Next
lstBox.Items.Add(c)


What the loop is doing is simply traversing all of the indeces (cells) of the arrays one at a time. Variable c only changes if the numbers in the cells are equal. Therefore we only need to count when the numbers are equal to each other:

a() contains:
3, 2, 5, 1, 7, 8, 3, 5, 6, 2, 3, 6, 1, 6, 5, 5, 7, 2, 5, 3

b() contains:
5, 3, 3, 4, 8, 2, 3, 5, 9, 5, 3, 7, 3, 7, 6, 3, 2, 1, 3, 4

This occurs three times, so the answer is 3.