• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

VB.net homework help.

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

EclipseJP

Member
Joined
Nov 2, 2001
Location
In front of my Computer
I have all the reading of a file down pat. But I have to seperate the file into a series. Each series is seperated with a 0. Now I can get it to read all of the numbers but, I just can't figure out how to stop it at the 0 and then continue after it. Heres an example.

1
2
3
4
5
6
7
0
10
20
and so on.

I have to add all the numbers from a series then get the average. Then get an average from all of the series.

Here is what I figured. I would put 5 labels because there is 5 series. I would put all the averages in each of those boxes. Then I would make another box with the average of all of those. How would I make each box read its own series of numbers?
Thanks in advance.
 
Not being familiar with VB.Net, here is a little c-ish psuedo code:
Code:
while(not inputfile.eof)
{
  while(input != 0)
  {
    sum += input;
  }
}
 
Unfortunatly that doesn't help me much. I tried

do until objStreamwriter.peek = 0

do while objStreamwriter.peek <> 0

but both just froze up the program when I clicked the button. It compiled fine.
 
Basically, what you want to do is have an outer loop that goes until you reach the end of the file, and an inner loop that adds numbers until you get to a zero. That's the easiest way to do it.
Code:
while(! inputstream.eof)
  {
  do
    {
    number = inputstream.get; //or whatever it is
    sum = sum + number;
    } while(number != 0 && ! inputstream.eof);
  }
 
As a quick and dirty way, why not read the file into a string and then use the split Function.

Something like (very psuedo):
Dim myString As String
myString = contents of file
Dim myArray() As String = Split(myString, 0)

From here, you can loop and enumerate each element of the array.

Somethign along the lines of:

Code:
Dim i As Integer = 0

For each String in myArray()
  response.write(myarray(i) + vbcrlf)
  i += 1
Next

this is just a quick sloppy example, but hopefully it gets you in the right direction.
 
I didn't read your original post very well, and nemisys has provided a very plausible alternative, but here is another reworking of how you could do it:
Code:
while(! inputstream.eof)
  {
  count = 0;
  do
    {
    number = inputstream.get; //or whatever it is
    cout << number << endl;
    sum = sum + number;
    count = count + 1;
    } while(number != 0 && ! inputstream.eof);
  
  cout << "The average for this series was " << sum / count;
  
  NumSeries = NumSeries + 1;
  total = total + sum;
 }

cout << "The total was " << total;
cout << "The average was  "<< total / NumSeries;

Of course, you would replace the couts with something like textbox.text = total.
 
i missed the second half where you need it all totaled and so on, so here is how I might go about it:

Code:
Public Function average() As String
  Dim myString As String
  myString = contents of file
  Dim myArray() As String = Split(myString, 0)

  Dim i As Integer = 0
  Dim intTotal As Integer = 0

  For each String in myArray()
    response.write(myarray(i) + vbcrlf)
    i += 1
    'convert the string number to an int and add to total
    intTotal += CInt(myArray(i)) 
  Next

  'divide by number of array elements. Since Array is 0 based, add 1.
  Dim strAverage As String = CStr((intTotal / (i + 1)))
  return strAverage
End Function

'display string of average in a label.
mylabel.Text = average()

Now this version is very dirty. If there are multiple files you need to read to get the 5 averages, then you might change the function to receive the file path as a string and then pass that to your file reader.

In addition to that, you might load the files into an array by enumerating the directory they are in or something like that. Then for each of your labels, pass the array element you want to use to populate the label.

Then the final label average would use something like:
MyLastLabel = CStr(CInt(label1.text + label2.text + label3.text + label4.text + label5.text) / 5)

Thats a little sloppy, but you get the point. You might not have to use the CStr function if the label control will properly display Integers. Been a while since i bothered with it so not sure off hand.

Letm e know if this makes sense to you.
 
Back