Görsel Programlama – Soru Çözümleri – Ünite 6-3
1.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
For i As Integer = 1 To 4
lstBox.Items.Add(“Pass #”& i)
Next
End Sub
Pass #1 Pass #2 Pass #3 Pass #4 |
2.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
For i As Integer = 3 To 6
lstBox.Items.Add(2 * i)
Next
End Sub
6 8 10 12 |
3.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
For j As Integer = 2 To 8 Step 2
lstBox.Items.Add(j)
Next
lstBox.Items.Add(“Who do we appreciate?”)
End Sub
2
4
6
8
Who do we appreciate? |
4.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
For countdown As Integer = 10 To 1 Step -1
lstBox.Items.Add(countdown)
Next
lstBox.Items.Add(“blastoff”)
End Sub
10 9 8 7 6 5 4 3 2 1 blastoff |
5.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim num As Integer
num = 5
For i As Integer = num To (2 * num – 3)
lstBox.Items.Add(i)
Next
End Sub
5 6 7 |
6.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
For i As Double = 3 To 5 Step .25
lstBox.Items.Add(i)
Next
lstBox.Items.Add(i)
End Sub
3 3.25 3.5 3.75 4 4.25 4.5 4.75 5 5 |
7.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘First entry in text file is number of records in file
Dim recCount As Integer
Dim name, mileTime As String
Dim sr As IO.StreamReader = IO.File.OpenText(“MILER.TXT”)
recCount = CInt(sr.Readline)
For miler As Integer = 1 To recCount
name = sr.Readline
mileTime = sr.Readline
lstBox.Items.Add(name & ” “& mileTime)
Next
sr.Close()
End Sub
(Assume that the seven lines of the file MILER.TXT contain the following entries: 3, Steve Cram, 3:46.31, Steve Scott, 3:51.6, Mary Slaney, 4:20.5.)
Steve Cram 3:46.31 Steve Scott 3:51.6 Mary Slaney 4:20.5 |
8.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘First entry in text file is number of records in file
Dim recCount, total, score As Integer
Dim sr As IO.StreamReader = IO.File.OpenText(“SCORES.TXT”)
recCount = CInt(sr.Readline)
For i As Integer = 1 To recCount
score = CInt(sr.Readline)
total += score ‘Add the value of score to the value of total
Next
sr.Close()
lstBox.Items.Add(“Average = “& total / recCount)
End Sub
(Assume the file SCORES.TXT contains the entries 4, 89, 85, 88, 98.)
Average = 90 |
9.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim row, entry As String
For i As Integer = 0 To 2
row = “”
For j As Integer = 0 To 3
entry = CStr(i + (3 * j) + 1)
row &= entry & ” ”
Next
lstBox.Items.Add(row)
Next
End Sub
1 4 7 10 2 5 8 11 3 6 9 12 |
10.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim row As String
For i As Integer = 1 To 5
row = “”
For j As Integer = 1 To i
row &= “*”
Next
lstBox.Items.Add(row)
Next
End Sub
* ** *** **** ***** |
11.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim word As String
Dim num1, num2 As Integer
word = InputBox(“Please enter a word.”)
num1 = CInt(Int((20 – word.Length) / 2))
num2 = 20 – num1 – word.Length
lstBox.Items.Add(Asterisks(num1) & word & Asterisks(num2))
End Sub
Function Asterisks(ByVal num As Integer) As String
Dim chain As String = “”
For i As Integer = 1 To num
chain &= “*”
Next
Return chain
End Function
(Assume that the response is Hooray.)
*******Hooray******* |
12.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Display an array of letters
Dim info As String, letter As String
info = “DATA”
For i As Integer = 1 To info.Length
letter = info.Substring(i – 1, 1)
lstBox.Items.Add(RepeatFive(letter))
Next
End Sub
Function RepeatFive(ByVal letter As String) As String
Dim chain As String = “”
For i As Integer = 1 To 5
chain &= letter
Next
Return chain
End Function
DDDDDAAAAATTTTTAAAAA |
In Exercises 13 through 16, identify the errors.
13.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
For j As Double = 1 To 25.5 Step -1
lstBox.Items.Add(j)
Next
End Sub
Geriye doğru sayma işleminin yapılabilmesi için ilk değerin ikinci değerden büyük olması gerekir. |
14.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
For i As Integer = 1 To 3
lstBox.Items.Add(i & ” “& 2 ^ i)
End Sub
Döngü sonuna Next yazılmamış. |
15.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Display all numbers from 0 through 20 except for 13
For i As Double = 20 To 0
If i = 13 Then
i = 12
End If
lstBox.Items.Add(i)
Next
End Sub
ilk değer küçük sonraki büyük olmalı. 0 To 20 |
16.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
For j As Integer = 1 To 4 Step 0.5
lstBox.Items.Add(j)
Next
End Sub
For j As Integer = 1 To 4 Step 0.5
lstBox.Items.Add(j)
Next
j türü integer değil, double olmalı. |
Görsel Programlama – Soru Çözümleri – Ünite 6-2
1.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim flag As Boolean, word As String
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
Do While (sr.Peek <> -1)
word = sr.ReadLine
If word.IndexOf(“A”) <> -1 Then
flag = True
End If
Loop
sr.Close()
If flag Then
txtOutput.Text = “At least one word contains the letter A.”
Else
txtOutput.Text = “No word contains the letter A.”
End If
End Sub
(Assume that the four lines of the file DATA.TXT have the following entries: AL, GORE, VIDAL, SASSOON.)
At least one word contains the letter A. |
2.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim name As String
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
Do While (sr.Peek <> -1)
name = sr.ReadLine
txtOutput.Text &= name
Loop
sr.Close()
End Sub
(Assume that the two lines of the file DATA.TXT contain the following entries: Ben, son.)
Benson. |
3.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Display list of desserts
Dim dessert As String
Dim sr As IO.StreamReader = IO.File.OpenText(“DESSERTS.TXT”)
Do While (sr.Peek <> -1)
dessert = sr.ReadLine
lstOutput.Items.Add(dessert)
Loop
sr.Close()
End Sub
(Assume that the three lines of the file DESSERTS.TXT contain the following entries: pie, cake, melon.)
pie cake melon |
4.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim city As String, pop As Double
Dim sr As IO.StreamReader = IO.File.OpenText(“CITYPOPS.TXT”)
Do While (sr.Peek <> -1)
city = sr.ReadLine
pop = CDbl(sr.ReadLine)
If pop >= 3 Then
lstOutput.Items.Add(city & ” “& pop)
End If
Loop
sr.Close()
End Sub
(Assume that the eight lines of the file CITYPOPS.TXT contain the following data, where each pair of lines gives a city and its population in millions: New York, 8.1, Los Angeles, 3.8, Chicago, 2.9, Houston, 2.0.)
New York 8.1
Los Angeles 3.8 |
5.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim firstLetter As String = “”, fruit As String = “”
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
Do While (sr.Peek <> -1)
fruit = sr.ReadLine
If fruit.Substring(0, 1) <> firstLetter Then
If firstLetter <> “” Then
lstOutput.Items.Add(“”)
End If
firstLetter = fruit.Substring(0, 1)
lstOutput.Items.Add(” “& firstLetter)
End If
lstOutput.Items.Add(fruit)
Loop
sr.Close()
End Sub
(Assume that the eight lines of the file FRUITS.TXT contain the following entries: Apple, Apricot, Avocado, Banana, Blueberry, Grape, Lemon, Lime.)
A Apple Apricor Avocado B Banana Blueberry G Grape L Lemon Lime |
6.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Display list of numbers
Dim num As Double
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
num = CDbl(sr.ReadLine)
Do While (sr.Peek <> -1)
lstOutput.Items.Add(num)
num = CDbl(sr.ReadLine)
Loop
sr.Close()
End Sub
(Assume the four lines of the file DATA.TXT contain the entries 2, 3, 8, 5.)
2 3 8 |
7.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim animal As String = “”, groupName As String = “”
InputAnimal(animal)
If animal <> “” Then
SearchList(animal, groupName)
DisplayResult(animal, groupName)
End If
End Sub
Sub DisplayResult(ByVal anml As String, ByVal gName As String)
If gName = “” Then
txtOutput.Text = “Animal not found.”
Else
txtOutput.Text = “A “& gName & ” of “& anml & “s.”
End If
End Sub
Sub InputAnimal(ByRef anml As String)
‘Request the name of an animal as input
anml = InputBox(“Please enter the name of an animal.”)
End Sub
Sub SearchList(ByVal anml As String, ByRef gName As String)
Dim creature, groupName As String
creature = “”
Dim sr As IO.StreamReader = IO.File.OpenText(“ANIMALS.TXT”)
Do While (creature <> anml) And (sr.Peek <> -1)
creature = sr.ReadLine
groupName = sr.ReadLine
Loop
If (sr.Peek = -1) Then
gName = “”
Else
gName = groupName
End If
sr.Close()
End Sub
(The six lines of the file ANIMALS.TXT contain the following entries: lion, pride, duck, brace, bee, swarm. Each pair of lines give an animal and the name of a group of those animals. Assume that the response is “duck”.)
A brace of ducks. |
8.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim excerpt As String
excerpt = “I think I can. ”
Duplicate30(excerpt)
excerpt = “We’re off to see the wizard, ” & _
“the wonderful wizard of Oz.”
Duplicate30(excerpt)
End Sub
Sub Duplicate30(ByVal sentence As String)
Dim flag As Boolean
flag = False ‘Flag tells whether loop has been executed
Do While sentence.Length < 30
flag = True
sentence &= sentence
Loop
If flag = True Then
lstOutput.Items.Add(sentence)
Else
lstOutput.Items.Add("Loop not executed.")
End If
End Sub
I think I can. I think I can. Loop not executed. |
9.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim word As String = “”, cWord As String = “”
Dim sr As IO.StreamReader = IO.File.OpenText(“WORDS.TXT”)
Do While (sr.Peek <> -1)
word = sr.ReadLine
If word.Substring(0, 1) = “c” Then
cWord = word
End If
Loop
sr.Close()
txtOutput.Text = cWord
End Sub
(Assume that the 11 lines of the file WORDS.TXT contain the following items: time, is, a, child, idly, moving, counters, in, a, game, Heraclitus.)
counters |
10.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim max, value, rowMax As Double
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
Do While (sr.Peek <> -1)
value = CDbl(sr.ReadLine)
rowMax = 0
Do While value <> -2
If value > rowMax Then
rowMax = value
End If
value = CDbl(sr.ReadLine)
Loop
lstOutput.Items.Add(rowMax)
If rowMax > max Then
max = rowMax
End If
Loop
sr.Close()
lstOutput.Items.Add(max)
End Sub
(Assume that the 12 lines of the file DATA.TXT contain the following entries: 5, 7, 3, 2, 10, 12, 6, 4, 2, 1, 9, 2.)
kodlar hatalı |
In Exercises 11 through 14, identify the errors.
11.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim num As Double
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
Do While (sr.Peek <> -1) And (num > 0)
num = CDbl(sr.ReadLine)
lstOutput.Items.Add(num)
sr.Close()
End Sub
(Assume that the five lines of the file DATA.TXT contain the following entries: 7, 6, 0, 2.)
num değişkeni oto olarak başlangıçta 0 değeri alacağından döngü hiç başlamayacaktır. |
12.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim flag As Boolean, num, sum As Double
Do While flag = False
num = CDbl(InputBox(“Enter a number”))
sum += num
If num * num < 0 Then
flag = True
End If
Loop
txtOutput.Text = CStr(num)
End Sub
aynı iki sayının çarpımı hiçbir zaman sıfırdan küçük olmayacağına göre sonsuz döngüye girer. |
13.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Display names of four U.S. Presidents
Dim president As String
Dim sr As IO.StreamReader = IO.File.OpenText(“PRES.TXT”)
president = sr.ReadLine
Do
lstOutput.Items.Add(president)
president = sr.ReadLine
Loop Until (sr.Peek = -1)
sr.Close()
End Sub
(Assume that the lines of the file PRES.TXT contain the following entries: Lincoln, Washington, Kennedy, Jefferson.)
Son başkan gözükmeyecektir. kod fazlalığı mevcut. |
14.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim num As Double
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
If (sr.Peek = -1) Then
num = 0
Else
num = CDbl(sr.ReadLine)
End If
Do While 1 < num < 5
lstOutput.Items.Add(num)
If (sr.Peek = -1) Then
num = 0
Else
num = CDbl(sr.ReadLine)
End If
Loop
sr.Close()
End Sub
(Assume that the five lines of the file DATA.TXT contain the following entries: 3, 2, 4, 7, 2.)
Görsel Programlama – Soru Çözümleri – Ünite 6-1
1.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim q As Double
q = 3
Do While q < 15
q = 2 * q - 1
Loop
txtOutput.Text = CStr(q)
End Sub
17 |
2.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim balance As Double = 1000
Dim interest As Double = 0.1
Dim n As Integer = 0 ‘Number of years
Do
lstOutput.Items.Add(n & ” ” & balance)
balance = (1 + interest) * balance
n += 1
Loop Until (balance > 1200)
lstOutput.Items.Add(n)
End Sub
0 1000 1 1100 2 |
3.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Display a message
Dim num As Double = 4
Dim message As String = “”
Do
Select Case num
Case 1
message = “grammer!”
num = -1
Case 2
message = “re a su”
num = (5 – num) * (3 – num)
Case 3
message = “per pro”
num = 4 – num
Case 4
message = “You a”
num = 2 * num – 6
End Select
txtOutput.Text &= message
Loop Until (num = -1)
End Sub
You are a super programmer! |
4.
Private Sub btnQuiz_Click(…) Handles btnQuiz.Click
Dim prompt As String, firstYear As Integer
‘Computer-assisted instruction
prompt = “In what year was the IBM PC first produced?”
lstQuiz.Items.Clear()
Do
firstYear = CInt(InputBox(prompt))
Select Case firstYear
Case 1981
lstQuiz.Items.Add(“Correct. The computer was an instant”)
lstQuiz.Items.Add(“success. By the end of 1981, there”)
lstQuiz.Items.Add(“was such a backlog of orders that buyers”)
lstQuiz.Items.Add(“had a three-month waiting period.”)
Case Is < 1981
lstQuiz.Items.Add("Later. The Apple II, which preceded")
lstQuiz.Items.Add("the IBM PC, appeared in 1977.")
Case Is > 1981
lstQuiz.Items.Add(“Earlier. The first successful IBM PC”)
lstQuiz.Items.Add(“clone, the Compaq Portable, “)
lstQuiz.Items.Add(“appeared in 1983.”)
End Select
lstQuiz.Items.Add(“”)
Loop Until firstYear = 1981
End Sub
(Assume that the first response is 1980 and the second response is 1981.)
Later. The Apple II, which preceded the IBM PC, appeared in 1977. Correct. The computer was an instant success. By the end of 1981, there was such a backlog of orders that buyers had a three-month waiting period. |
5.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Calculate the remainder in long division
txtOutput.Text = CStr(Remainder(3, 17))
End Sub
Function Remainder(ByVal divisor As Double, _
ByVal dividend As Double) As Double
Dim sum As Double = 0
Do While sum <= dividend
sum += divisor
Loop
Return (dividend - sum + divisor)
End Function
2 |
6. Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Simulate IndexOf; search for the letter t
Dim info As String = “Potato”
Dim letter As String = “t”
Dim answer As Integer
answer = IndexOf(info, letter)
txtOutput.Text = CStr(answer)
End Sub
Function IndexOf(ByVal word As String, ByVal letter As String) _
As Integer
Dim counter As Integer = 0
Dim current As String = “”
Do While counter < word.Length
current = word.Substring(counter, 1)
If letter = current Then
Return counter
End If
counter += 1 'Add 1 to the value of counter
Loop
'If not found then the answer is -1
Return -1
End Function
2 |
In Exercises 7 through 10, identify the errors.
7.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim q As Double
q = 1
Do While q > 0
q = 3 * q – 1
lstOutput.Items.Add(q)
Loop
End Sub
Q değeri sürekli arttığı için program sonsuz döngüye girer. |
8.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Display the numbers from 1 to 5
Dim num As Integer
Do While num <> 6
num = 1
lstOutput.Items.Add(num)
num += 1
Loop
End Sub
num değeri her tekrarda 1 olarak alınacağı için sonsuz
döngüye girer. |
9.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Repeat until a yes response is given
Dim answer As String = “N”
Loop
answer = InputBox(“Did you chop down the cherry tree (Y/N)?”)
Do Until (answer.ToUpper = “Y”)
End Sub
Do ve loop kelimelerinin yazılışlarının yer değiştirilmesi lazım. |
10.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Repeat as long as desired
Dim n As Integer, answer As String = “”
Do
n += 1
lstOutput.Items.Add(n)
answer = InputBox(“Do you want to continue (Y/N)?”)
Until answer.ToUpper = “N”
End Sub
Loop until şeklinde yazılması lazım, sadece until yeterli değil. |
Görsel Programlama – Ders Kitabı Soru Çözümleri – [7-5]
1.
Dim a(20, 30) As Double
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
a(3, 5) = 6
a(5, 3) = 2 * a(3, 5)
txtOutput.Text = CStr(a(5, 3))
End Sub
12 |
2.
Dim years(100, 50) As Double
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim x As Integer = 7, y As Integer = 8
years(x, y) = 1937
txtOutput.Text = CStr(years(7, 8) + 60)
End Sub
1997 |
3.
Dim w(10, 15) As String
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim d As String = “Dorothy”, n As Integer = 1
w(1, 1) = d
txtOutput.Text = w(n, n)
End Sub
Dorothy |
4.
Dim actor(5, 5) As String
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim a As Integer = 2, b As Integer = 3, temp As Integer
actor(a, b) = “Bogart”
temp = a
a = b
b = temp
lstOutput.Items.Add(“1. ” & actor(a, b))
lstOutput.Items.Add(“2. ” & actor(b, a))
End Sub
1. 2. Bogart |
5.
Dim a(,) As Integer
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim p, q, sum As Integer
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
p = CInt(sr.ReadLine)
q = CInt(sr.ReadLine)
ReDim a(p, q)
For j As Integer = 0 To p
sum = 0
For k As Integer = 0 To q
a(j, k) = CInt(sr.ReadLine)
sum += a(j, k)
Next
lstOutput.Items.Add(sum)
Next
sr.Close()
End Sub
(Assume that the eight lines of the file DATA.TXT contain the following data: 1, 2, 4, 1, 6, 5, 8, 2.)
p ve q değerlerine ilk iki değer atandı, yani 1 ve 2. ondan sonraki atama işlemleri baştan değil kaldığı yerden devam ediyor. yani 4+1+6 ve 5+8+2 11 15 |
6.
Dim a(3, 4) As Integer
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim row As String
For j As Integer = 0 To 3
row = “”
For k As Integer = 0 To 4
a(j, k) = j + k
row &= a(j, k) & ” ”
Next
lstOutput.Items.Add(row)
Next
End Sub
0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 |
7.
Dim s(2, 2) As Double
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
For j As Integer = 0 To 2
For k As Integer = 0 To 2
s(j, k) = CDbl(sr.ReadLine)
Next
Next
sr.Close()
For j As Integer = 0 To 2
lstOutput.Items.Add(s(j, j))
Next
End Sub
(Assume that the nine lines of the file DATA.TXT contain the following entries: 1, 2, 3, 4, 3, 2, 3, 4, 5.)
1 3 5 |
8.
Dim m(,) As Integer
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim x, y As Integer
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
x = CInt(sr.ReadLine)
y = CInt(sr.ReadLine)
ReDim m(x, y)
For j As Integer = 0 To x
For k As Integer = 0 To y – j
m(j, k) = CInt(sr.ReadLine)
lstOutput.Items.Add(m(j, k) – k)
Next
Next
sr.Close()
End Sub
(Assume that the 10 lines of the file DATA.TXT contain the following entries: 1, 2, 6, 3, 2, 1, 3, 4, 9, 8.)
6 2 0 1 2 |
In Exercises 9 and 10, identify the errors.
9.
Dim a(2, 3) As Integer
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Fill an array
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
For j As Integer = 0 To 3
For k As Integer = 0 To 2
a(j, k) = CInt(sr.ReadLine)
Next
Next
sr.Close()
End Sub
(Assume that the 12 lines of the file DATA.TXT contain the following entries: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2.)
Döngüde a(3,2) değeri alacak ve program hata verecektir. maksimum a(2,3) değeri alabilir. |
10.
Dim score(2, 2) As Integer
Private Sub frmScores_Load(…) Handles MyBase.Load
‘Fill array from text file
Dim student As Integer
Dim sr As IO.StreamReader = IO.File.OpenText(“SCORES.TXT”)
For j As Integer = 0 To 2
student = CInt(sr.ReadLine)
For k As Integer = 0 To 2
score(k, j) = CInt(sr.ReadLine)
Next
Next
End Sub
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Report individual scores
Dim student, exam As Integer
student = CInt(txtStudent.Text)
exam = CInt(txtExam.Text)
If (student >=0 And student<=2) And (exam>=0 And exam<=2) Then
txtOutput.Text = CStr(score(student, exam))
End If
End Sub
(Assume that the 12 lines of the file SCORES.TXT contain the following data: 0, 80, 85, 90, 1, 72, 80, 88, 2, 87, 93, 90.)
score(k,j) değil, score(j,k) olması lazım. |
Görsel Programlama – Ders Kitabı Soru Çözümleri – [7-4]
1.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim p As Integer = 100
Dim q As Integer = 200
Dim temp As Integer = p
p = q
q = temp
lstOutput.Items.Add(p)
lstOutput.Items.Add(q)
End Sub
200 100 |
2.
Dim gag() As String = {“Stan”,” Oliver”}
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim temp As String
If gag(1) < gag(0) Then
temp = gag(1)
gag(1) = gag(0)
gag(0) = temp
End If
lstOutput.Items.Add(gag(0))
lstOutput.Items.Add(gag(1))
End Sub
Oliver Stan |
3.
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim x, y, temp As Double
Dim swappedFlag As Boolean = False
x = 7
y = 11
If y > x Then
temp = x
x = y
y = temp
swappedF1ag = True
End If
lstOutput.Items.Add(x)
lstOutput.Items.Add(y)
If swappedFlag Then
lstOutput.Items.Add(“Numbers interchanged.”)
End If
End Sub
11 7 Numbers interchanged. |
4. Dim a(2) As Integer
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim temp As Integer
For j As Integer = 0 To 1
For k As Integer = 0 To 2 – j
If a(k) > a(k + 1) Then
temp = a(k)
a(k) = a(k + 1)
a(k + 1) = temp
End If
Next
Next
For j As Integer = 0 To 2
lstOutput.Items.Add(a(j))
Next
End Sub
Private Sub frmNumbers_Load(…) Handles MyBase.Load
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
For j As Integer = 0 To 2
a(j) = CInt(sr.ReadLine)
Next
sr.Close()
End Sub
(Assume that the three lines of the file DATA.TXT contain the following entries: 7, 4, 3.)
3 4 7 |
In Exercises 5 and 6, identify the errors.
5. Dim c(3), d(3) As Integer
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Swap two items
c(3) = d(3)
d(3) = c(3)
lstOutput.Items.Add(c(3))
lstOutput.Items.Add(d(3))
End Sub
Private Sub frmNumbers_Load(…) Handles MyBase.Load
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
For i As Integer = 0 To 3
c(i) = CInt(sr.ReadLine)
d(i) = CInt(sr.ReadLine)
Next
sr.Close()
End Sub
(Assume that the eight lines of the file DATA.TXT contain the following entries:1, 2, 3, 4, 5, 6, 7, 8.)
içerik takas edilmeye çalışılmış fakat ilk değer geçiçi bir değişkene atanmadığından ikiside aynı değeri alacaktır. |
6. Dim a(2), b(2) As Integer
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
‘Swap the two arrays
Dim temp(2) As Integer
a = b
b = a
End Sub
Private Sub frmNumbers_Load(…) Handles MyBase.Load
Dim sr As IO.StreamReader = IO.File.OpenText(“DATA.TXT”)
For i As Integer = 0 To 2
a(i) = CInt(sr.ReadLine)
b(i) = CInt(sr.ReadLine)
Next
sr.Close()
End Sub
(Assume that the six lines of the file DATA.TXT contain the following entries:1, 3, 5, 7, 9, 11.)
aynı şekilde temp değişkeni tanımlanmış fakat kullanılmamış. içerik değiştirme başarısız. |
Görsel Programlama – Ders Kitabı Soru Çözümleri – [7-3]
1.
Private Sub btnProcess_Click(…) Handles btnProcess.Click
Dim total As Double = 0
For itemNum As Integer = 0 To 3
total += CDbl(txtWinter(itemNum).Text)
Next
txtBox.Text = “Expenses for winter: ” & FormatCurrency(total)
End Sub
Expenses for winter: $4.271,66 |
2.
Private Sub btnProcess_Click(…) Handles btnProcess.Click
Dim total As Double = 0
total += CDbl(txtWinter(2).Text)
total += CDbl(txtSpring(2).Text)
total += CDbl(txtSummer(2).Text)
total += CDbl(txtFall(2).Text)
txtBox.Text = “Annual water bill: ” & FormatCurrency(total)
End Sub
Annual water bill: $198 |
3.
Private Sub btnProcess_Click(…) Handles btnProcess.Click
Dim diff As Double
Dim total As Double = 0
For i As Integer = 0 To 3
diff = CDbl(txtSummer(i).Text) – CDbl(txWinter(i).Text)
total += diff
Next
txtBox.Text = “Summer bills top winter bills by ” & _
FormatCurrency(total)
End Sub
Summer bills top winter bills by $67.09 |
4.
Private Sub btnProcess_Click(…) Handles btnProcess.Click
Dim total As Double = 0
For itemNum As Integer = 0 To 3
total += TotalCateg(itemNum)
Next
txtBox.Text = “Total major expenses: ” & FormatCurrency(total)
End Sub
Function TotalCateg(ByVal itemNum As Integer) As Double
Dim total As Double = 0
total += CDbl(txtWinter(itemNum).Text)
total += CDbl(txtSpring(itemNum).Text)
total += CDbl(txtSummer(itemNum).Text)
total += CDbl(txtFall(itemNum).Text)
Return total
End Function
Total major extenses: $16822,6 |
In Exercises 5 through 8, determine the output displayed when the button is clicked.
5.
Structure Appearance
Dim height As Double
Dim weight As Double
End Structure
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim person1, person2 As Appearance
person1.height = 72
person1.weight = 170
person2.height = 12 * 6
If person1.height = person2.height Then
lstOutput.Items.Add(“heights are same”)
End If
person2 = person1
lstOutput.Items.Add(person2.weight)
End Sub
heights are same 170 |
6.
Structure TestData
Dim name As String
Dim score As Double
End Structure
Dim sr As IO.StreamReader = IO.File.OpenText(“SCORES.TXT”)
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim student As TestData
For i As Integer = 1 to 3
GetScore(student)
DisplayScore(student)
Next
sr.Close()
End Sub
Sub GetScore(ByRef student As TestData)
student.name = sr.ReadLine
student.score = CDbl(sr.ReadLine)
End Sub
Sub DisplayScore(ByVal student As testData)
lstOutput.Items.Add(student.name & ” ” & student.score)
End Sub
(Assume that the six lines of the file SCORES.TXT contain the following data:Joe, 18, Moe, 20, Roe, 25.)
Joe 18 Moe 20 Roe 25 |
7.
Structure Address
Dim street As String
Dim city As String
Dim state As String
End Structure
Structure Citizen
Dim name As String
Dim residence As Address
End Structure
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim person As Citizen
person.name = “Mr. President”
person.residence.street = “1600 Pennsylvania Avenue”
person.residence.city = “Washington”
person.residence.state = “DC”
txtOutput.Text = person.name & ” lives in ” & _
person.residence.city & “, ” & person.residence.state
End Sub
Mr. President lives in Washington, DC |
8.
Structure TaxData
Dim socSecNum As String
Dim numExem As Integer ‘Number of exemptions
Dim maritalStat As String
Dim hourlyWage As Double
End Structure
Structure Employee
Dim name As String
Dim hrsWorked As Double
Dim taxInfo as TaxData
End Structure
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim worker as Employee
worker.name = “Hannah Jones”
worker.hrsWorked = 40
worker.taxInfo.hourlyWage = 20
txtOutput.Text = worker.name ” & earned ” & _
FormatCurrency(worker.hrsWorked * worker.taxInfo.hourlyWage)
End Sub
Hannah Jones earned $800 |
In Exercises 9 through 11, determine the errors.
9.
Structure Nobel
Dim peace as String
Dim year as Integer
End Structure
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim prize As Nobel
peace = “International Atomic Energy Agency”
year = 2005
txtBox.Text = peace & ” won the ” & year & ” Nobel Peace Prize.”
End Sub
Programın çalışması için değişkenlerin tam olarak tanımlanması gerekiyor.
peace ve year kendi başlarına çağrılamaz. prize.peace ve prize.year şeklinde
olması gerekiyor. |
10.
Structure Vitamins
Dim a As Double
Dim b As Double
End Structure
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim minimum As Vitamins
minimum.b = 200
minimum.a = 500
lstOutput.Items.Add(minimum)
End Sub
minimum dizisinde veriler indisler içinde tutulu olmasına rağmen indis belirtilmeden veril çağırılmaya çalışılmış. |
11.
Structure BallGame
Dim hits As Double
Dim runs As Double
End Structure
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click
Dim game1, game2 As BallGame
game1.hits = 15
game1.runs = 8
game2.hits = 17
game2.runs = 10
If game1 > game2 Then
txtOutput.Text = “The first game was better.”
Else
txtOutput.Text = “The second game was at least as good.”
End If
End Sub
game1>game2 sorgusu değer alamayacağından (çünkü değerler indislerde saklı) program çalışmayacaktır. |



