Simple File I/O - Read a new line
I have a file that stores exam scores for a class of students. I am trying to write a program that successfully opens the file, reads in the exam scores, finds the average score, the highest score, and the lowest score, and prints these out. The average score should be printed with 2 digits after the decimal point.
Thi开发者_StackOverflow社区s is what I have so far:
static void Main()
{
string myData = "";
int temp = 0;
int max = 0;
int min = 0;
double average = 0;
StreamReader fileReader = new StreamReader("data.txt");
do
{
myData = fileReader.ReadLine();
if (myData != null)
{
max = int.Parse(myData);
temp = int.Parse(myData);
if (temp > max)
temp = max;
}
} while (myData != null);
fileReader.Close();
Console.ReadLine();
}//End Main()
I don't exactly know how to proceed. How do I read in a new line and assign it to temp? I don't think I'm doing it right.
Here is one way that will make your teacher sad :P
static void Main(string[] args)
{
var file = new StreamReader("scores.txt");
var split = file.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
IEnumerable<int> ints = split.Select(x => int.Parse(x));
Console.WriteLine("Total Scores:" + ints.Count());
Console.WriteLine("Max:" + ints.Max());
Console.WriteLine("Min:" + ints.Min());
Console.WriteLine("Average:" + ints.Average().ToString("0.00"));
Console.ReadLine();
}
While this is technically correct, this is counter-productive to understanding algorithms (however basic they may be), and I suggest you look at the other answers. But this demonstrates how versatile the .NET framework is.
<3 LINQ
The first error I found is that max
will always be exactly equal to temp
because you assign them the same value, so this if
will never be true:
max = int.Parse(myData);
temp = int.Parse(myData);
if (temp > max)
Not bad. Here's some pseudocode:
- File.ReadAllLines to get an array of strings (call this lines)
- Foreach line in lines
- Parse the double (? is each line guaranteed to be an int?)
- if max < parsed, max = parsed
- if min > parsed, min = parsed
- sum += parsed
- Print out min, max, (sum / lines.count).ToString("000.00")
Assumes the file looks like:
25
12
33.5
100
75
...
Since this is homework, I'll just give you some clues.
Right now, you're setting "max" each time through the loop. Try only setting temp, and see what happens. You might want to consider defaulting max (when you create it) to a very small number instead of 0.
Also, you'll need to do something similar for "min", but default it to a very large number.
int max = int.MinValue;
int min = int.MaxValue;
In order to get the average, you'll need to have a sum and a count, and keep track of those. Then, at the end, use a double to compute the average, and print. To get 2 decimal places, you can use average.ToString("N") - the "N" format does a nicely formatted number with 2 decimal places by default.
How about you separate out the processes? Do the reading and fill a list of integers with the contents of the file. Then perform the processing for min / max and average later on.
Isolating issues help you focus on them. I like to call this noise reduction. As a contractor I get to work on a lot of messy code and one of the reasons they are hard to understand is that too much is going on at the same time. If you simplify whats going on the code almost writes itself. This is also called Separation of Concerns. This is a very important programming principle.
After you've isolated the issues and got the code working you can then try to put it all together again so the process is more efficient (if you do it inline with the file reading then you will only hold one line in memory at a time).
For starters, you need to get rid of the line "max = int.Parse(myData)". Otherwise, you'll keep overwriting max with the current value.
I take it that this is the general gist of your assignment. You should be leery of copying this code.
double count = 0.0;
double min = double.MaxValue;
double max = double.MinValue;
double total = 0.0;
using(StreamReader sr = new StreamReader(@"c:\data.txt"))
{
while (!sr.EndOfStream)
{
String line = sr.ReadLine();
double value = double.Parse(line.Trim());
if (value < min) min = value;
if (value > max) max = value;
total += value;
count++;
}
}
Console.WriteLine("Min: {0}", min);
Console.WriteLine("Max: {0}", max);
Console.WriteLine("Avg: {0}", (total / count).ToString("0.00"));
Console.ReadLine();
static void Main(string[] args)
{
const string filename = @"data.txt";
bool first = true;
int min=0, max=0, total=0;
var lines = File.ReadAllLines(filename);
foreach (var line in lines)
{
var score = int.Parse(line.Trim());
if (first)
{
min = max = total = score;
first = false;
continue;
}
if (score < min)
min = score;
if (score > max)
max = score;
total += score;
}
if (first)
{
Console.WriteLine("no input");
return;
}
var average = (double)total/lines.Length;
Console.WriteLine(string.Format("Min: {0}, Max: {1}, Average: {2:F2}", min, max, average));
}
Hey thanks to everybody who helped out and offered suggestions, here is my final code implementation (without using arrays)
using System;
using System.IO;
class Program
{
static void Main()
{
string line = "";
int value = 0;
int max = 0;
int min = 100;
int total = 0;
double count = 0.0;
double average = 0;
StreamReader fileReader = new StreamReader(@"data.txt");
do
{
line = fileReader.ReadLine();
if (line != null)
{
value = int.Parse(line);
if (value > max)
max = value;
if (value < min)
min = value;
total += value;
count++;
}
} while (line != null);
average = total / count;
Console.WriteLine("Max: {0}", max);
Console.WriteLine("Min: {0}", min);
Console.WriteLine("Avg: {0:f2}", average);
Console.ReadLine();
}//End Main()
}//End class Program
精彩评论