Chapter seven review Monther Aldwairi • • • • • • • • • • • • • • What is the output of: Private Sub cmdButton_Click() Dim i As Integer, a(1 To 4) As integer Open "DATA.TXT" For Input As #1 For i = 1 To 4 Input #1, a(i) Next i For i = 4 To 1 Step -2 picBox.Print a(i); Next i End Sub Contents of DATA.TXT: 1,3,5,7 • Output: • 73 • What is the output of: • Dim a(2 To 9) As Integer, i As Integer, _ k As Integer, sum As Integer • For i = 2 To 9 • a(i) = i • Next i • sum = 0 • For k = 4 To 6 • sum = sum + 10 ^ a(k-2) • Next k • picBox.Print sum • Output: • 11100 • What is the output of: • • • • • • • • • • Dim a(-1 to 5) As Single, x As Single Open "DATA.TXT" For Input As #1 Do While Not EOF(1) Input #1, x a(x) = a(x) + 3 Loop Close #1 picBox.Cls picBox.Print a(0); a(2) Contents of DATA.TXT: 0, 1, 5, 2, 0 • Output: • 63 • • • • • • • • • • • • • Dim a(-2 To 5), b As Integer Dim i As Integer, j As Integer Open "DATA.TXT" For Input As #1 num = 0 For i = 3 To 5 Input #1, a(i) Next i For j = -1 To 1 Input #1, k b = b + a(k) Next j picBox.Print b Contents of DATA.TXT: 2, 1, 0, -1, 3, 3 • Output: • 4 • How many comparisons will be made in the first pass of a bubble sort of n items? • Answer: n-1 • How many comparisons will be made in the second pass of a bubble sort of n items? • Answer: n-2 • How many comparisons will be made in all the passes of a bubble sort of n items? • Answer: (n-1)+(n-2)+(n-3) + … +2+1 • = n(n-1)/2 • Given the following array items: • 3, 1, 8, 9, 2, 10 • a) What is the contents of the array after the first pass of using bubble sort? • b) What is the contents of the array after the first comparison in the second pass? • c) How many comparisons will be made to find 2 using sequential search on the original array? • d) How many comparisons will be made to find 2 using sequential search if the array was sorted? • How many elements are in the array declared by • Dim myArray(1 To 5, 1 To 5) As Single • How many elements are in the array declared by • Dim myArray(0 To 3, 1 To 3) As Single 7.1 3. Stuhldreher Crowley 4. 11 5. 6 2 9 11 3 4 6. 10 12 3 14 13. river(1) river(2) river(3) river(4) river(5) Thames Ohio Amazon Volga Nile river(1) river(2) river(3) river(4) river(5) Ohio Amazon Volga Nile Thames 14. cat(1) cat(2) cat(3) Felix Garfield Morris cat(4) Socks 7.2 3. Michigan 4. The sum of the first 4 elements is 30 6. South Pacific 7. The total rainfall for the first quarter is 10 12. hue will be unknown in the Sub procedure. Also, in the Print statement in the Sub procedure, tone needs a subscript. 7.3 1. 200 100 2. Oliver Stan 3. 11 7 Numbers interchanged. 10. 15 comparisons 11. (n - 1) + (n - 2) + ... + 1 A shorter version of this formula is n*(n1)/2 7.5 4. 1. 2. Bogart 5. 4 1 6 5 8 2 13. Private Sub Exchange(a() As Single) Dim col As Integer, temp As Single 'Interchange values of 2nd and 3rd row For col = 1 To 10 temp = a(2, col) a(2, col) = a(3, col) a(3, col) = temp Next col End Sub 14. Private Sub FindGreatestValue(a() As Single) Dim row As Integer, col As Integer Dim greatest As Single 'Find greatest value in array and the locations where it occurs greatest = a(1, 1) For row = 1 To 3 For col = 1 To 45 If a(row, col) > greatest Then greatest = a(row, col) End If Next col Next row picOutput.Print "The greatest value is"; greatest picOutput.Print "It occurs at (row, col): " For row = 1 To 3 For col = 1 To 45 If a(row, col) = greatest Then picOutput.Print row, col End If Next col Next row End Sub