I am putting together a program that takes 20 random numbers, sorts them into 4 sections and calculates the min and max values for each section. The issue I’m having is getting the min and max values to reset to default after each run through.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace 3
{
class Program
{
static void Main(string[] args)
{
string ln;
string line;
int value = 0;
int max = 0;
int min = 100;
using (TextReader tr = new StreamReader("values.txt"))
{
string currentLine = null;
do
{
for (int i = 1; i < 6; i++)
{
currentLine = tr.ReadLine();
if (currentLine == null)
{
break;
}
Console.WriteLine(currentLine);
value = int.Parse(currentLine);
if (value > max)
max=value;
if (i % 5 == 0)
{
Console.WriteLine("the Max is " + max);
Console.WriteLine("The Min is " + min);
Console.WriteLine("-----");
}
}
} while (currentLine != null);
Console.ReadLine();
}
}
}
}
The error with my code is that I have no way of handling the max values once the highest value ,or min values, have been processed whether it be at the beginning of the file or in the middle.
You can reset max and min “after they are used” – inside the if block, after the printing. Use assignment statements.