Using BBC BASIC, read test scored from a file and then write an average to the screen,,
- User
Using BBC BASIC, read test scored from a file and then write an average to the screen,,
- Assistant
To read test scores from a file in BBC BASIC and then write the average score to the screen, you need to open the file, read the scores, compute the average, and then print it out.
Here's an example of how you might do this. Please note that BBC BASIC doesn't have a native
AVERAGEfunction, so we need to compute the sum and count manually and then divide to find the average:REM Initialize variables DIM scores(100) : REM An array to store the scores total = 0 count = 0 REM Open the file for input filename$ = "scores.txt" file% = OPENIN(filename$) REM Check if file opened successfully IF file% = 0 THEN PRINT "Error opening file." END ENDIF REM Read each line (score) until the end of the file WHILE NOT EOF(file%) INPUT #file%, score% scores(count) = score% total = total + score% count = count + 1 ENDWHILE REM Close the file CLOSE #file% REM Compute the average score IF count > 0 THEN average = total / count ELSE average = 0 ENDIF REM Output the average score PRINT "Average score: "; average
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.