I need to be able to read a text file into an array instead of inputting all the values myself. The text file reads as:
8.7
9.3
7.9
6.4
9.6
8.0
8.8
9.1
7.7
9.9
5.8
6.9
The main purpose of the program is to read scores from a data file, store them in an array, and calculate the highest, lowest, total and average of the 12 scores.
The text file is stored in the Debug folder of the project.
This is what I've done so far:
Console.WriteLine("Numbers in the list: " + scores.Length);
//highest number
double high = scores[0];
for (int index = 1; index < scores.Length; index++)
{
if (scores[index] > high)
{
high = scores[index];
}
}
Console.WriteLine("Highest number = " + high);
//lowest number
double low = scores[0];
for (int index = 1; index < scores.Length; index++)
{
if (scores[index] < low)
{
low = scores[index];
}
}
Console.WriteLine("lowest number = " + low);
//average of the scores
double total = 0;
double average = 0;
for (int index = 0; index < scores.Length; index++)
{
total = total + scores[index];
}
average = (double)total / scores.Length;
Console.WriteLine("Total = " + total);
Console.WriteLine("Average = " + average.ToString("N2"));
Console.ReadKey();
}
}
}
var lines = File.ReadAllLines(@"C:\scores.txt");string[]lines=File.ReadAllLines(path);double[] values=Array.ConvertAll(lines, double.Parse);File.ReadLines()is better thanFile.ReadAllLines()- it won't buffer the entire file in memory.