Date: Tue, 01 Oct 2002 12:51 p.m. From: Mark Jacobson To: 810-030-01@uni.edu Subject: golfers.txt, average and golfer counts logic... Hi VB students, The page 201 Programming Project #10 for doing golfer names and golfer scores from a file named golfer.txt created in Notepad or some text processor (You cannot use WordPad or Microsoft Word documents for storing the data!), is due on Friday. How do you compute the average of all scores and the total number of golfers so you can report the average score and the number of golfers printed on the Form or in a Label or a TextBox? (Textbook says to display number of golfers and the average score in text boxes. Why, I do not know! Feel free to use Labels instead of TextBoxes, if you wish. Or to just use Print and Print it on the Form is okay too). The logic of it is show below from last week's handouts, but I have made it explicit with A., B. and C. labels for: A. Initialize and prepare counters and sum variables by setting them to ZERO, before the loop that reads the entire golfer.txt input file. B. Update the count by counting (adding 1 to the counter variable) each golfer you read from the input file, and updating the sum variable with that golfer's golf score. This happens INSIDE the Do Until EOF( fileNumber ) LOOP! C. Calculating the AVERAGE from the Sum and the Count, and reporting the count (How many golfers were there) and the average score for the set of golfers. This happens AFTER the Do Until EOF() loop is all done. It is as easy as A, B, C. Here is highlight outline of above logic. A. BEFORE the loop starts, initialize the count and the sum each to 0. B. DURING the loop, process each golfer by counting them and summing their golf score. ----- --- C. AFTER the loop is done, calculate and report (OUTPUT) the very esciting results. --------- ------ See the handouts for the ListBox and file reading examples. Here are some excerpts from the code handed out last Wednesday. Private Sub cmdReadFile_Click() Open "Z:\web\UNIplayers.txt" For Input As #10 A. ' Initialize BEFORE beginning to read the ' input data file. Count variables and Sum ' variables are set to 0. A. numberOfPlayers = 0 ' Initialize the count to ZERO A. totalInches = 0 ' Initialize the sum of all heights to ZERO Do Until EOF(10) ' Read a record (line of data) from the file - golfer or player Input #10, playerNumber, playerName, playerHeight B. ' Process the player involves updating the COUNT and ' updating the SUM of heights B. numberOfPlayers = numberOfPlayers + 1 B. totalInches = totalInches + heightToInches(playerHeight) ' Could .AddItem to ListBox here, if you needed to. -------- Loop C. ' Reporting the COUNT and calculating the AVERAGE HEIGHT from ' the total count and the total number of inches or total height ' of all the players. C. Print vbCrLf; "There are "; numberOfPlayers & _ " UNI VB players." C. averageHeight = totalInches / numberOfPlayers ' Calculate average ----------------- feet = averageHeight \ 12 inches = averageHeight Mod 12 C. Print vbCrLf & _ "Average height: " & _ feet & "-" & _ Format(inches, "##") & _ " or " & Format(averageHeight, "##.#") & _ ' Report average " inches." -------------- Close #10 End Sub ***** Reminder: Quiz #2 on Friday, October 4th. ***** Mark