0

I would like to know why the minimum number is always "0".

I generate array with random number and then I did some manipulations. Everything works fine except minimum number.

here is my code:

class Program
{
    static void Main(string[] args)
    {
        var array = new int[10];
        var number1 = 1;
        var number2 = 100;
        var min = array[0];
        var max = array[0];
        var sum = 0;
        var average = 0;

        Random randomNum = new Random();
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = randomNum.Next(number1, number2);

            Console.WriteLine(array[i].ToString());

            if(array[i] > max)
            {
                max = array[i];
            }
            else if (array[i] < min)
            {
                min = array[i];
            }

            sum += array[i];
            average = sum / array.Length;

        }
        Console.WriteLine($"\nmin = {min}, max = {max}, sum = {sum} and average = {average}");

    }
}
  • Put a breakpoint on the min assignment line: min is always 0 when you don't pass on it... because `array[i] < min` is always false since min starts from 0 and array[i] is always higher. –  Oct 27 '19 at 11:24
  • I would just use 2 seperate if - one for each. Connecting them like this is at best going to throw issues into it. – Christopher Oct 27 '19 at 11:24
  • 2
    The problem seems to be that you initialize `min` with the first element of an array of zeroes, i.e. zero. – the default. Oct 27 '19 at 11:25
  • 2
    The condition `array[i] < min` will always be false because `min` is initially 0. – Klaus Gütter Oct 27 '19 at 11:25
  • On top of what the others asid, you should propably rename "number1" and "number" with "lowerBound" and "upperBound". Also I can only repeat do not use a if-else. A number can be bigger then min and larger then max at the same time. – Christopher Oct 27 '19 at 11:29

4 Answers4

3

because you initialize the min variable with array[0] before you populate the array.

Initializing an array will initialize all it's items to their default values (for more details, read Why doesn't a struct in an array have to be initialized?), and as the default value of int is 0, and your array only contains numbers that are larger than 0, your min will never change.

Initialize it to number2 or int.MaxValue and you'll get the actual minimum value.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
2

Put a breakpoint on the min assignment line: min is always 0 because you don't pass on it...

Because array[i] < min is always false since min starts from 0 and array[i] is always higher.

To solve the problem without rewriting the algorithm you can set min as:

min = int.MaxValue;

Since max starts from 0, it works for it but you should write:

max = 0;

Even if array[0] is initialized to 0 by default.

2

Your min value will always be zero beacuse of this:

else if (array[i] < min)
{
     min = array[i];
}

You are initalizing min with zero. When you take a number from Random.next it will give [1,100) so min will always smaller than randomNumber. Because of that, your min value never changes.

Instead you can do:

min = int.MaxValue;

before the for loop.

Mert Köklü
  • 2,183
  • 2
  • 16
  • 20
2

I did a bit more complete rework then just those hints.

var array = new int[10];
//telling names are very importan
var lowerBound = 1;
var upperBound = 100;
//This is a reliable initialisation
var min = upperBound;
var max = lowerBound;
var sum = 0;

Random randomNum = new Random();
for (int i = 0; i < array.Length; i++)
{
    //adding a temporary variable makes it cleaner and requires less accessor use
    int temp = randomNum.Next(number1, number2);

    Console.WriteLine(temp.ToString());

    //No else-if. The first number has a 98% chance to check both of those.
    if(temp > max)
    {
        max = temp;
    }
    if (temp < min)
    {
        min = temp;
    }

    sum += temp;
    arraiy[i] = temp;

}

//No point calculating that before the output.
int average = sum / array.Length;
Console.WriteLine($"\nmin = {min}, max = {max}, sum = {sum} and average = {average}");

Note that I did most of this work for readability. Performance wise there should be no difference. The JiT Compiler is very good at cutting underused temporary variables or adding temporary variables to avoid excessive indexer access.

Christopher
  • 9,634
  • 2
  • 17
  • 31