Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)

Similar presentations


Presentation on theme: "Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)"— Presentation transcript:

1 Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)

2 Recall from last class

3 3 Using Array variables in Visual Basic 1.Use Dim statements to declare the array variables Examples: Score (1) 00…..00 Score (2) Score (3)Score (30) …… Dim Student (1 To 30) As String Dim Score (1 To 30) As Integer Default values

4 4 Using Array variables in Visual Basic 2.Assign values to Subscripted variables: – Using individual Assignment statements. Examples: Score (1) 8792…..00 Score (2) Score (3)Score (30) …… Score (1) = 87 Score (3) = 92

5 5 Using Array variables in Visual Basic 2.Assign values to Subscripted variables: – Passing by Open and Input statements Dim Student (1 To 30) As String Dim Score (1 To 30) As Integer Ind = 0 Total = 0 Open "A:\Score.txt" For Input As #1 Do While Not EOF(1) Ind = Ind + 1 Input #1, StuName, StuScore Student (Ind) = StuName Score (Ind) = StuScore Total = Total + Score (Ind) Loop : "John", 78 "Lynn", 91 "Okra", 88 "Paul", 77 "Bako", 91 "Kyli",78 "Doll",56 : "King",67 Score.txt

6 Exercises

7 7 Determine the values in the computer memory after the execution of this program Private Sub cmdButton_Click() Dim Num (1 To 15) As Integer Num (3) = 10 Num (15) = Triple (Num(3)) End Sub Private Function Triple (x As Integer) As Integer Triple = 3 * x End Function ….. Num (1) Num (2) Num (3)Num (15) ……

8 8 Determine the output displayed in the picture box when the command button is clicked Private Sub cmdButton_Click() Dim Num (1 To 15) As Integer Num (3) = 10 picOutput.Print Num (3) * Value (Num(3)) picOutput.Print Num (2) + Num(15) End Sub Private Function Value (x As Integer) As Integer Value = 3 * x End Function

9 9 Visual Basic provides two functions that can be used with arrays: LBound(arrayName) which returns the smallest available subscript of an array and UBound(arrayName) which returns the largest available subscript of an array. Determine what will be displayed on the picture box when the command button cmdDisplay is clicked. Private Sub cmdDisplay_Click() Dim Num(1 To 15) As Integer Num(3) = 10 Num(15) = Triple(Num(3)) picOutput.Print LBound(Num) picOutput.Print UBound(Num) picOutput.Print Num(LBound(Num)) picOutput.Print Num(UBound(Num)) End Sub Private Function Triple(x As Integer) As Integer Triple = 3 * x End Function

10 10 Suppose that the file Score.txt contains 3 records (see below). Determine the values of the following variables after the program is executed. Private Sub cmdButton_Click() Dim Student (1 To 30) As String Dim Score (1 To 30) As Integer Ind = 0 Total = 0 Open "A:\Score.txt" For Input As #1 Do While Not EOF(1) Ind = Ind + 1 Input #1, StuName, StuScore Student (Ind) = StuName Score (Ind) = StuScore Total = Total + Score (Ind) Loop Average = Total / Ind End Sub Student (3) = Score (2) = Ind = Average = "John", 9 "Nathalie", 10 "Greg", 10 Score.txt

11 11 Private Sub cmdButton_Click() Dim Student (1 To 30) As String Dim Score (1 To 30) As Integer Ind = 0 Total = 0 Open "A:\Score.txt" For Input As #1 Do While Not EOF(1) Ind = Ind + 1 Input #1, StuName, StuScore Student (Ind) = StuName Score (Ind) = StuScore Total = Total + Score (Ind) Loop Average = Total / Ind End Sub 1)Suppose that the statement picOutput.Print Student(30); "has"; Score(30) appears after Loop, what will be displayed in the picture box: ? 2) Suppose that the following statements appear after Loop For i = 1 To 4 picOutput.Print Student(i); Score(i) Next i What will be displayed in the picture box ? "John", 9 "Nathalie", 10 "Greg", 10 Score.txt

12 12 Identify the error(s) Private Sub cmdDisplay_Click() Dim planet (1 To 9) As String Call Assign (planet) picOutput.Print planet (1) End Sub Private Sub Assign (planet As String) planet (1) = "Venus" End Sub Answer:

13 13 Identify the error(s) n Private Sub cmdDisplay_Click() Dim p (1 To 100) As Single Dim i As Integer For i = 1 To 200 p (i) = i / 2 Next i End Sub Answer: n Private Sub cmdDisplay_Click() Dim b (2 To 8 Step 2) As Integer Dim t As Integer Open "Data.txt" For Input As #1 For t = 2 To 8 Step 2 Input #1, b (t) Next t Close #1 End Sub Answer:


Download ppt "Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)"

Similar presentations


Ads by Google