Docsity

Lab Report 21 - Understanding Array Problems | IST 256, Lab Reports of Information Technology

Toggle sidebar
bg1
IST 256
Lab 20 – April 2, 2008
1. Understanding Array Program
a. (repeat question from Lab 19). In yesterday’s temperatures program, suppose that the
input file had the following temperatures.
5
10
20
10
After the user clicks the button, “Read Temperatures from File”, here are the values of
these variables. Note that with 4 numbers, the array subscripts from 0 to 3 are being used
for numbers. The rest of the array is not used.
Variable
Value
temperatures(0) 5
temperatures(1) 10
temperatures(2) 20
temperatures(3) 10
temperatures(4) ---
. . . ---
numtemps 4
b. Without running the program, think about the values of the variables during the
execution of the button procedure for the “Highest Temperature” button. Here is that part
of the program:
Dim high As Single
Dim index As Integer
' initialize the high so far to be the first element
high = temperatures(0)
' loop over the remaining array and find the highest
For index = 1 To numtemps - 1
' if the temp is higher than the high so far, make it the new high
If (temperatures(index) > high) Then
high = temperatures(index)
End If
Next
Write here the values of the variables index and high before and during the loop. What is
the last index number that is used? Are there too many spaces here?
Variable Before
loop
Loop
iteration 1
Loop
iteration 2
Loop
iteration 3
Loop
iteration 4
Loop
iteration 5 ..
index ------
high
pf3
pf4

Partial preview of the text

Download Lab Report 21 - Understanding Array Problems | IST 256 and more Lab Reports Information Technology in PDF only on Docsity!

IST 256

Lab 20 – April 2, 2008

1. Understanding Array Program

a. (repeat question from Lab 19). In yesterday’s temperatures program, suppose that the input file had the following temperatures. 5 10 20 10

After the user clicks the button, “Read Temperatures from File”, here are the values of these variables. Note that with 4 numbers, the array subscripts from 0 to 3 are being used for numbers. The rest of the array is not used. Variable Value temperatures(0) 5 temperatures(1) 10 temperatures(2) 20 temperatures(3) 10 temperatures(4) ---

... --- numtemps 4

b. Without running the program, think about the values of the variables during the execution of the button procedure for the “Highest Temperature” button. Here is that part of the program: Dim high As Single Dim index As Integer ' initialize the high so far to be the first element high = temperatures(0) ' loop over the remaining array and find the highest For index = 1 To numtemps - 1 ' if the temp is higher than the high so far, make it the new high If (temperatures(index) > high) Then high = temperatures(index) End If Next

Write here the values of the variables index and high before and during the loop. What is the last index number that is used? Are there too many spaces here?

Variable Before loop

Loop iteration 1

Loop iteration 2

Loop iteration 3

Loop iteration 4

Loop iteration 5 .. index ------ high

2. Understanding How to Write a Procedure Call

Suppose that we have a form with a general procedure defined at the top of the form:

Public Class Form ‘ This procedure takes the number of units, computes the total price, ‘ and displays the result in a textbox Private Sub DisplayPrice ( ByVal units as Integer, ByRef tb as TextBox) Dim price as Single = 2. Dim total as Single

total = units * price tb.Text = “Total = “ & Format(total, “currency”) End Sub …. End Class

Suppose that the form has a textbox named TextBox1 and in another procedure, we have a variable declared and given a value: Dim number as Integer number = 10

a. Write a valid procedure call to DisplayPrice that could occur in the procedure with the variable “number”:

b. With the procedure call that you wrote in part a, write what output will be given in the textbox:

When you are done with this part, show me your work.

4. Extending the Program to find the Average of the Survey Answers

Now we will extend the Survey program to have another label that shows the average of the answers for the survey question.

a. Add another label to your form called “Average Answer” and put it above the “Results” label.

b. Add code to the “Show Survey Results” procedure to compute the average. i. Declare variables called average and total as a Single. Declare another variable called number.

ii. After the histogram label is computed, add another for loop to loop over the histogram array. Before the loop, initialize number and total to 0. The loop should increase the number and total for each element of the histogram: For i = 0 to 9 number = number + histogram(i) total = total + histogram(i) * i Next

iii. After this loop, compute the average as total/number and display the average in the label that you added.

c. test your program

***************** At the end of lab ********************

Go to the C: drive where you put your folder with your name. Copy and paste this entire folder to the H: drive under your IST256 projects.

*************** Don’t forget to save your work! ****************