Reputation: 55
I had done a previous exercise whereby I had to write a program where a user would input 30 different numbers (hours) and the program would spit out the array, average, min, and max. That was fine. Now, however, I am tasked with reading the numbers from a text file (and outputting the same as before - array, avg, min, max).
I am hitting an error based on the original exercise, where I am told that a name does not exist in the program (I understand this is because the text doc now holds that previous name). I don't know what I need to do to change this.
I've watched some videos and found similar problems listed on this site but am stuck :(. Can anyone help?
Program code is:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace IntsArray
{
class Program
{
static void Main(string[] args)
{
StreamReader myReader = new StreamReader("Values.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
if (line != null)
Console.WriteLine(line);
}
myReader.Close();
Console.ReadLine();
for (int index = 0; index < hoursArray.Length; index++)
{
Console.Write("Enter your hours: ");
hoursArray[index] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Numbers in the list: " + hoursArray.Length);
for (int index = 0; index < hoursArray.Length; index++)
{
Console.WriteLine(hoursArray[index]);
}
Console.ReadKey();
int total = 0;
double average = 0;
for (int index = 0; index < hoursArray.Length; index++)
{
total = total + hoursArray[index];
}
average = (double)total / hoursArray.Length;
Console.WriteLine("Average = " + average.ToString("N2"));
int high = hoursArray[0];
for (int index = 1; index < hoursArray.Length; index++)
{
if (hoursArray[index] > high)
{
high = hoursArray[index];
}
}
Console.WriteLine("Highest number = " + high);
int low = hoursArray[0];
for (int index = 0; index < hoursArray.Length; index++)
{
if (hoursArray[index] < low)
{
low = hoursArray[index];
}
}
Console.WriteLine("Lowest number = " + low);
Console.ReadKey();
}
}
}
The numbers in the Values.txt looks like this:
8
24
9
7
6
12
10
11
23
1
2
9
8
8
9
7
9
15
6
1
7
6
12
10
11
23
1
2
9
8
I understand the problem, I just do not know how to fix it!
Upvotes: 0
Views: 1550
Reputation: 519
You need to define the array
int[] hoursArray = new int[WHATEVER LENGTH IT SHOULD BE];
then your StreamReader read line thing has to look something like this:
string line = "";
int position = 0; //needed to set the array position
while (line != null)
{
line = myReader.ReadLine();
if (line != null)
{
Console.WriteLine(line);
hoursArray[position] = int.Parse(line); //convert line to int and sotres it in the array
position++;
}
}
if you want to make it more flexible i would use a list instead of an array
Edit: min max avg calulation:
int max = 0;
int min = 9999999;
double avg = 0;
for (int i = 0; i < hoursArray.Length; i++)
{
if (hoursArray[i] > max) //if there is a higher value then the current max
max = hoursArray[i];
if (hoursArray[i] < min) //if there is a lower value then the current min
min = hoursArray[i];
avg = avg + hoursArray[i];
}
avg = avg / hoursArray.Length;
Console.WriteLine("Max: " + max);
Console.WriteLine("Min: " + min);
Console.WriteLine("Avg: " + avg);
Console.ReadLine();
You just need to put them together in you main and there you have it
Upvotes: 0
Reputation: 2978
First thing you will do is to identify the number of lines in the textfile.
var file = new StreamReader("file.txt").ReadToEnd();
var lines = file.Split(new char[] {'\n'});
var count = lines.Count;
Based on this count initialize your array
int[] hoursArray = new int[count];
After that populate your array.
int index = 0;
while ((line = sr.ReadLine()) != null)
{
hoursArray[index++] = Convert.ToInt32(line);
}
Then last step, you can now apply linq to get max min and avg same as what arvin said.
var max = hoursArray.Max();
var min = hoursArray.Min();
var avg = hoursArray.Avg();
Upvotes: 0
Reputation: 14995
If you want to read numbers from a file, why do you have this code ?
for (int index = 0; index < hoursArray.Length; index++)
{
Console.Write("Enter your hours: ");
hoursArray[index] = int.Parse(Console.ReadLine());
}
what you can do is read the numbers from a file, store it in Array
or List
and then use Linq
to get Max
, Min
and Average
.
List<int> Numbers = System.IO.File.ReadAllLines("Your File Path")
.Select(N=> Convert.ToInt32(N)).ToList();
Console.WriteLine(Numbers.Average());
Console.WriteLine(Numbers.Max());
Console.WriteLine(Numbers.Min());
Upvotes: 3