Section 15.3 Practice Reading from Files
Now that you have seen an example of how to read numbers from a file and find their average it’s time to practice some more. Pick one of the following options:
Subsection 15.3.1 Option 1
Activity 15.3.
Modify the program we just wrote so that it also computes the standard deviation of the numbers in the file.
Hint: To do so, you’ll first have to find the average (as we did before). Then, you have to “reset” the file by closing it and re-opening it, then you can read the file again and compute the standard deviation using the following:
For each number you read from the file, subtract the average and square the result.
You need to find the average of those squared differences.
Take the square root of that average squared difference and you are done!
admin.....open in new window
When you are done, please paste your code into the code submission box below:
Subsection 15.3.2 Option 2
Activity 15.4.
Modify the program we just wrote so that, instead of finding the average of the numbers in the file, it finds the largest number in the file.
admin.....open in new window
When you are done, please paste your code into the code submission box below:
Subsection 15.3.3 Option 3
Activity 15.5.
Modify the program we just wrote so that instead of reading numbers, it reads characters from the file my_second_file.txt
. Print out the characters you read from the file one next to the other.
As before, for now we are telling you how many characters are in the file, namely 9. So that you know what to expect, here is what the contents of the file looks like:
chocolate
admin.....open in new window
When you are done, please paste your code into the code submission box below: