# Do while loop, file input/output



## emoandy (Nov 21, 2008)

The program I am working on is supposed to read scores from a data file and then:

1) Find their sum 
2) Calculate their average 
3) Find the largest and smallest scores 
4) Count the scores

I have successfully found the sum, average and count, but I cannot figure out how to find the largest and smallest score. Here is my code, the data file, and my debugged form. Any advice or nudge in the right direction would be greatly appreciated.

OptionStrictOn
OptionExplicitOn
PublicClass Form1

PrivateSub btnReadfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnReadfile.Click

Dim sr As IO.StreamReader = IO.File.OpenText("scores.txt")
Dim count, sum, largest, smallest, newvalue AsInteger
Dim average AsDouble

DoWhile sr.Peek <> -1
count += 1
newvalue = CInt(sr.ReadLine)
sum += newvalue
Loop
sr.Close()
average = sum / count

lstIO.Items.Add("There were " & count & " numbers read in the file")
lstIO.Items.Add("Sum of the numbers is " & sum)
lstIO.Items.Add("Average of the items is " & FormatNumber(average))
lstIO.Items.Add("Largest number was " & largest)
lstIO.Items.Add("Smallest number was " & smallest)
EndSub
EndClass


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

Set largest and smallest equal to the first newvalue
Then compare them to each subsequent newvalue, and set them to that newvalue if larger/smaller

HTH

Jerry


----------



## Squashman (Apr 4, 2003)

Bubble Sort.


----------



## emoandy (Nov 21, 2008)

I got the largest score to work but i'm having trouble with the smallest. Since the smallest score is zero, how am I supposed to know if the code is working. I can put no code for smallest score and it will automatically be zero. I can put smallest = cint(newvalue / (newvalue) -1), and that will give me zero. <=== I know i'm not supposed to do it this way.

I just want to make sure when i'm getting zero it is because the code worked, not because I used math.


----------



## emoandy (Nov 21, 2008)

Option Strict On
Option Explicit On
Public Class Form1

Private Sub btnReadfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnReadfile.Click
 
Dim sr As IO.StreamReader = IO.File.OpenText("scores.txt")
Dim count, sum, largest, smallest, newvalue As Integer
Dim average As Double
 
lstIO.Items.Clear()
*smallest = newvalue*
Do While sr.Peek <> -1
count += 1
newvalue = CInt(sr.ReadLine)
sum += newvalue
Loop
sr.Close()
average = sum / count
*largest = newvalue*
 
lstIO.Items.Add("There were " & count & " numbers read in the file")
lstIO.Items.Add("Sum of the numbers is " & sum)
lstIO.Items.Add("Average of the items is " & FormatNumber(average))
lstIO.Items.Add("Largest number was " & largest)
lstIO.Items.Add("Smallest number was " & smallest)
End Sub
End Class


I added the two bold lines and they work but I still think smallest score isn't working the way it is supposed to. The newvalue is zero above the loop because nothing has happened yet.


----------



## TheOutcaste (Aug 8, 2007)

The first value is 12, so when count is 1, both largest and smallest are set to 12. This is the initial value you'll be comparing to.
If smallest is zero when it's finished, the code is working.

The values need to be set _*inside*_ the loop *after* newline is read the first time. I'd set them before doing the sum.

Jerry


----------



## TheOutcaste (Aug 8, 2007)

With that code, largest is the largest simply because the last value happens to be the largest.
You have to check each newvalue against the current value for smallest and largest, and update them if you find a smaller or larger value. As Squashman says, it's a Bubble Sort. A partial one as you aren't sorting all the values, just the largest and smallest.

I'll bet a full Bubble Sort will be in an upcoming assignment...


----------



## Squashman (Apr 4, 2003)

Sorry I can't help much with the code. I only know Fortran and Pascal from programming years ago. I don't know too many of the current programming languages because I now do most of my work on a mainframe.


----------



## BobFoster (Dec 5, 2008)

As others have said, you need to actually do some work inside the loop (because this is where each value from the list is processed) to determine whether each value is either larger or smaller than the current largest or smallest value:

Do While <condition>
...
If newvalue > largest Then largest = newvalue
If newvalue < smallest Then smallest = newvalue
...
Loop

I'll let you think about exactly where in the loop you need to put these lines, but think about the flow of control: in a simple program like this, a computer starts at the top and works down, executing each instruction in order unless it is told to alter that flow. It alters the values of variables (such as "largest" or "smallest") as it goes. I'm sure you know this, so if you consider what the computer needs to do in order to find the largest value (what would you do, faced with such a list?) and particularly the order in which it needs to do things (can it process a value before it's read one in, for example?), you should see how this needs to work.

One other point you made that no one else so far has talked about - you ask how you can tell if something is working. Testing is a huge part of computer programming - in many ways more important than the programming itself.

In this case you are having difficulty because the data set you have happens to have zero as the lowest value and the largest value is at the end (so you can't tell if your largest value is being picked up because it really is the biggest or because it is just at the end of the list of values you have).

The trick in both cases is to change the _input_. This technique is very common. If you were to create a new input file with no zeros and make sure that neither the first nor last values were the lowest or highest, you could be more sure that the algorithms were working as you intend. You don't need any more than four values, and you can then work out the count, average, etc. manually and you will be able to see whether the program is working correctly for these values too.

Finally, think about what happens if you don't initialise a variable (set it to a value before you start using it). Your lowest value is reported as zero even if you don't do any processing on it. What will happen if you read in a 3 and ask whether or not it's the lowest value you've read in if you haven't already set the "smallest" variable to a value?

Hope this all makes sense!

Regards,
Bob


----------

