user1863593
user1863593

Reputation: 199

C# - displaying the max, min, and average

I am new to C#. I have been working on this program and researching but am not getting anywhere. The goal is to have the user enter numbers (how many is up to the user). when they enter a 0, it will stop the program and display the minimum number entered, the maximum number entered, and the average of all numbers entered. I am not getting any errors and I am getting. If someone can please point me in the right direction.

The WriteLines are returning:

Lowest number is 0 Highest number is 0 Average is: 0 Count: 5

Here is my code:

int LOWEST =0;
int HIGHEST=0;
const int STOP = 0;
double average = 0;
int input;

int count = 0;
Console.WriteLine("Enter a number. You can end the program at anytime by entering 0");
input = Convert.ToInt32(Console.ReadLine());
while (input != STOP)
{
     for (int i=0; input != STOP; i++)
     {
           Console.WriteLine("Enter a number. You can end the program at anytime by entering 0");
           input = Convert.ToInt32(Console.ReadLine());
           count++;
           var Out = new int[] { input };
           LOWEST = Out.Min();
           HIGHEST = Out.Max();
           average = Out.Average();

           if ((input > LOWEST) || (input < HIGHEST))
           {
                 LOWEST = Out.Min();

           }
           if (input > HIGHEST)
           {
                 HIGHEST = Out.Max();
           }
      }
}


Console.WriteLine("Lowest number is {0}", LOWEST);
Console.WriteLine("Highest number is {0}", HIGHEST);
Console.WriteLine("Average is {0}", average);
Console.WriteLine("Count: {0}", count);
Console.ReadLine();

Upvotes: 2

Views: 15148

Answers (3)

Wouter de Kort
Wouter de Kort

Reputation: 39898

On each run you are constructing a new array of integers:

var Out = new int[] { input };

After this line, Out contains one item: the last input. Calling Min, Max and Average on it will return the last value. Which is zero if you ended the program.

instead of creating a new array each time, you want to create a List<int> at the beginning of your program and then add each input to it. You can then use the whole list of values to calculate Min, Max and Average.

Eventually you can change your code into something like this:

const int STOP = 0;
int input = -1;

List<int> Out = new List<int>();

while (input != STOP)
{
    Console.WriteLine("Enter a number. You can end the program at anytime by entering 0");
    input = Convert.ToInt32(Console.ReadLine());

    if (input == STOP) break;

    Out.Add(input);

}


Console.WriteLine("Lowest number is {0}", Out.Min());
Console.WriteLine("Highest number is {0}", Out.Max());
Console.WriteLine("Average is {0}", Out.Average());
Console.WriteLine("Count: {0}", Out.Count);
Console.ReadLine();

Upvotes: 6

ToddB
ToddB

Reputation: 1474

List<int> numbers = new List<int>();
        numbers.Add(10);
        numbers.Add(30);
        numbers.Add(20);
        numbers.Add(0);
        numbers.Max();
        numbers.Min();
        numbers.Average();

returns 30, 0 and 15.

Upvotes: 3

SAJ14SAJ
SAJ14SAJ

Reputation: 1708

Before your loop, you should probably make Out an extensible data structure analogous to an array, the List.

List<int> Out = new List<int>();

then each loop, you can

Out.Add(input);

Since this sounds like an exercise for the reader, you can then traverse your list and compute the average from all data values.


Alternately, before the loop, you could declare

int n = 0;
int total = 0;

and each loop, do

n += 1;
total += input;

From these, you should be easily able to compute the average.

Upvotes: 1

Related Questions