Showing posts with label Repetion. Show all posts
Showing posts with label Repetion. Show all posts

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.

Tuesday, April 8, 2008

Student Grades Example

Today we talked about dealing with student grades using arrays. We would have an array that would hold the students' identifying information (such as name or ID number) and then arrays for all of the students' grades. Remember, these are called Parallel Arrays.

Example:

Public Class Form1
'Global Variables:
Dim students(5) As String
Dim hw1(5) As Double
Dim hw2(5) As Double
Dim hw3(5) As Double
...
...


We have declared these arrays as Global so that we will be able to use them in all subroutines, and also so that we will not loose the information stored in them for the duration of the program. Here are what the resulting arrays look like:

students()
0 1 2 3 4
+----+----+----+----+----+
| | | | | |
+----+----+----+----+----+

hw1()
0 1 2 3 4
+----+----+----+----+----+
| | | | | |
+----+----+----+----+----+

hw2()
0 1 2 3 4
+----+----+----+----+----+
| | | | | |
+----+----+----+----+----+

hw3()
0 1 2 3 4
+----+----+----+----+----+
| | | | | |
+----+----+----+----+----+


As we can see, Student1's information will reside in index 1 of all the arrays.

Code

We wrote two subroutines, one to get the information from the user and one to find and print the information for a particular student.

Here is the code for getting the student information:

Sub form_load()
For i As Integer=0 To students.GetUpperBound(0)
students(i) = InputBox("Enter student " & _
CStr(i) & "'s name:")
hw1(i) = CDbl(InputBox("Enter homework 1:"))
hw2(i) = CDbl(InputBox("Enter homework 2:"))
hw3(i) = CDbl(InputBox("Enter homework 3:"))
Next
End Sub


Here is the code for a linear search of a student:

Sub btnSearch_Click()
Dim sname As String
Dim found As Boolean = False
sname = InputBox("Enter student's name:")
For i As Integer = 0 To students.GetUpperBound(0)
If sname = students(i) Then
listbox1.Items.Add(hw1(i))
listbox1.Items.Add(hw2(i))
listbox1.Items.Add(hw3(i))
found = True
Exit For

End If
Next


If Not found Then
MsgBox("Student not found",,"")
End If
End Sub


2-Dimensional Conversion

We can convert the 1-dimensional grade parallel arrays into a two dimensional array. The names of the students must remain a separate 1-dimensional array, however since its type is different. Here are the declarations:


Public Class Form1
'Global Variables:
Dim students(5) As String

Declare hw() with 3 rows and 5 columns:
Dim hw(3,5) As Double
...
...

Resulting in the following arrays:

students()
0 1 2 3 4
+----+----+----+----+----+
| | | | | |
+----+----+----+----+----+

hw()
0 1 2 3 4
+----+----+----+----+----+
0 | | | | | |
+----+----+----+----+----+
1 | | | | | |
+----+----+----+----+----+
2 | | | | | |
+----+----+----+----+----+


Rewritten Code

In the hw() array, each row represents a HW grade and each column represents the grades for each student. Now we will rewrite our subroutines:

Sub Form_Load()
For i As Integer=0 To students.GetUpperBound(0)
students(i) = InputBox("Enter student " & _
CStr(i) & "'s name:")
hw(0,i) = CDbl(InputBox("Enter homework 1:"))
hw(1,i) = CDbl(InputBox("Enter homework 2:"))
hw(2,i) = CDbl(InputBox("Enter homework 3:"))
Next
End Sub


Here is the code for a linear search of a student:

Sub btnSearch_Click()
Dim sname As String
Dim found As Boolean = False
sname = InputBox("Enter student's name:")
For i As Integer = 0 To students.GetUpperBound(0)
If sname = students(i) Then
listbox1.Items.Add(hw(0,i))
listbox1.Items.Add(hw(1,i))
listbox1.Items.Add(hw(2,i))
found = True
Exit For

End If
Next


If Not found Then
MsgBox("Student not found",,"")
End If
End Sub


If we wanted to reduce the three lines dealing with the HW grades into one line, we could add another For loop to loop through the grades:

Sub Form_Load()
For i As Integer=0 To students.GetUpperBound(0)
students(i) = InputBox("Enter student " & _
CStr(i) & "'s name:")
For j As Integer=0 To hw.GetUpperBound(0)
hw(j,i) = CDbl(InputBox("Enter homework " & CStr(j)))
Next
Next
End Sub


Here is the code for a linear search of a student:

Sub btnSearch_Click()
Dim sname As String
Dim found As Boolean = False
sname = InputBox("Enter student's name:")
For i As Integer = 0 To students.GetUpperBound(0)
If sname = students(i) Then
For j As Integer=0 To hw.GetUpperBound(0)
listbox1.Items.Add(hw(j,i))
Next
found = True
Exit For

End If
Next


If Not found Then
MsgBox("Student not found",,"")
End If
End Sub


.GetUpperBound

Remember for 1-dimensional arrays the .GetUpperBound() function returns the number of the last possible index so:
   students.GetUpperBound(0)

returns 4.

For 2-dimensional arrays we have two indeces in which we are interested: one that indicates the highest index of rows, and one that indicates the highest index of columns. Therefore there are two possible GetUpperBound() calls:

'Get the last index of rows
hw.GetUpperBound(0)
'Get the last index of columns
hw.GetUpperBound(1)

We have used this in our rewritten subroutines above.

Tuesday, April 1, 2008

ATM Project - Part 1

Now that we have learned about File I/O, loops and decisions we can start to work on a real project. We will create an ATM simulation and we will continue adding things to it throughout the rest of this course.

First we have to decide how an ATM works. We know that it will require the following operations:

1) Validation (pin # and account number)
2) Deposit Money
3) Withdraw Money
4) Account Status (balance etc)

We will have a text file that contains the account numbers and pins in it:
acctinfo.txt

123456789
1234
234567890
9876


This is kept in the debug folder. When the program begins,

Tuesday, March 25, 2008

Boxes of Stars (Loops continued)

We learned how we could use loops to print out stars in various patterns like a box of stars [code]:



Or even an empty box of stars :



We have to use a Nested For Loop in order to print out lines of stars row by row. The outer loop represents each row, and the inner loop represents each column. Each row was constructed by using a String, since we can only print to a listbox one line at a time.

In the empty stars example we have to use If/Else statements in order to tell if we were printing out one of the borders. If we were at a border (outer or inner control variable is equal to 1 or num, where num is the total number of rows and columns) then we would print out a star, Else we will only print out a space.

Your HW is to print out the empty box of stars. You must ask the user if what number they like and then check to make sure that the number is in a range of 3 to 20.

Tuesday, March 18, 2008

Loops

We finished up Chapter 6.1 in class today. We learned that we could use loops in order to accomplish tasks that should be done several times using Loops. We learned about three types of loop structures:

Do Until
Do While
For Next


We can write a loop using any of these three structures and any loop written in one of these structures can be rewritten using one of the other two. The basic syntax for each of these loops are:


For variable = (starting point) To (ending point)
'Do statements until we reach the ending point
Next 'Update condition variable

'**************************************

'Set condition before entering loop
Do While condition
'Do statements until condition is false
'Update the condition
Loop

'**************************************

'Set condition before entering loop
Do
'Do statements until condition is true
'Update the condition
Loop Until condition


In all three loop structures we have three parts that are the same:
1) A starting point for the condition we are checking
2) An endpoint for the condition
3) An update for the condition

We must have all three things for the loop to execute and end execution. Without (3) from above for example, we would have an infinite loop which is definitely not what was intended.

Also notice that the Do While and the Do Until loops are opposites. Do While loops until the condition is False whereas the Do Until loop will loop until the condition is True. Also the Do Until is always guaranteed to loop at least once. We have no such guarantee with the Do While loop.

Do While Examples

Dim i As Integer = 1
Do While i <= 10
'List out the numbers from 1 to 10
lstOut.Items.Add( i )
i += 1
Loop


Dim i As Integer = 1
Dim num As Integer
num = CInt(InputBox("Enter a #:",""))
Do While i <= num
'List out the numbers from 1 to num
lstOut.Items.Add( i )
i += 1
Loop

Do Until Examples

Dim i As Integer = 1
Do
'List out the numbers from 1 to 10
lstOut.Items.Add( i )
i += 1
Loop Until i > 10


Dim i As Integer = 1
Dim num As Integer
num = CInt(InputBox("Enter a #:",""))
Do
'List out the numbers from 1 to num
lstOut.Items.Add( i )
i += 1
Loop Until i > num

For Next Examples

For( i As Integer = 1 To 10)
'List out the numbers from 1 to 10
lstOut.Items.Add( i )
Next


Dim num As Integer
num = CInt(InputBox("Enter a #:",""))
For( i As Integer = 1 To num)
'List out the numbers from 1 to num
lstOut.Items.Add( i )
Next