Displaying average and largest number from text file

cooljoebay

New member
Joined
Feb 13, 2009
Messages
3
Programming Experience
Beginner
Hi

Didnt know where to post this.

Am using VB 2005 and need to take an existing txt file which basically has 15 sets of data, each set containing a year (ie Year 1990) and a number below the year.

When a user clicks the calculate button on a form, I need it to first list the average of all those numbers(not year) in the txt. And below that I need the largest number in the file to be displayed.

I am basically familiar with using objReader and StreamReader. But, I can only verify and open the text. Beyond that no idea.

Thanks a lot.
 
Let me guess, a school project?

Ok, so you can read in the data, that's a good start. I would read in all the data from the file and put it into a variable structure. If it's 15 sets of two values, then you could dimension 30 variables to hold it. I think I would actually use a 2 dimension array.

So you've read in your values from the file and put them in a data struture. Somewhere along the way you will need to verify the number charactors are really numbers. I think I would do that as I'm reading them from the file. That way the values in your array could be numbers. (Note: Are we talking integers or real numbers? I'll assume integers.)

Now you need a loop to step though your array of data and process it for output. You'll have one process that does the averaging and a second step that keeps track of the largest value. You can do both in one loop through the data.

Does this help any? You need to learn to take the objective of the program and start breaking it down into smaller steps. Looking at the entire task is way to complicated. Focus on how to break it down into simpiler steps.

Bernie
 
Hi Bernie

Thanks for that helpful information. Yes, it is for school and your response is much clearer than the complicated textbook I am using. I seem to be stunted on the array process. What throws me the most is once I establish an array, I cannot use that array to get my result. THinking about "elements" doesn't sink in well enough I dont think.

Joe
 
Think about arrays as just a series of variables stored in linked cubbie holes. Lets say you have a one dimensional array of integers and I want to find the average of them all. The array lets me step through my variables in an organized way.

VB.NET:
dim myArray[] as Integer
dim i as integer
dim myTotal as integer
dim myAverage as double

' do something to fill myArray with values ...

For i = 0 to (myArray.Length - 1)
    myTotal += myArray[i]
Next

myAverage = myTotal / (i + 1)

' do something with myAverage ...

So once my array is loaded, then I can use individual values, myArray[3], or step through values, myArray and increment i.

Does this help?

Bernie
PS: Please forgive any syntax errors or caps, I'm not thinking well in VB this morning.
 
Much better. I do better with the visual examples. Its hard to believe I stay on the Dean's list considering how slow I am at the beginning.
 
Back
Top