Hi guys i m new around here as you can see,
i was wondering why can t the program find
the minimum of random generated numbers?Any
help would be greatly apreciated.

``````public static void Main(string[] args)

{
int[] randomNum = new int[10];

int min = randomNum[0];
int max = randomNum[0];

Random RandomNumber = new Random();

Console.WriteLine("Printing the interval");
Console.WriteLine("---------------------------");

foreach (int i in randomNum)
{
randomNum[i] = RandomNumber.Next(1, 10);

Console.WriteLine(randomNum[i]);

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

}

Console.WriteLine("---------------------------");
Console.WriteLine("Maximal number is in interval <1,10> \t"+max);
Console.WriteLine("Minimal number is in interval <1,10> \t"+min);

}
} `
``````
2
Contributors
3
Replies
7
Views
6 Years
Discussion Span
Last Post by castajiz_2

This is some strange code to find min and max value of integer array.
Why dont you create an array of n numbers, and then use Linq to find those 2 values:

``````//an array of numbers:
int[] numbers = { 1, 2, 3, 4, 5 };
//then do:
int min = numbers.Min();
int max = numbers.Max();
``````

And one more thing, your i is always 0, becuase you never increment it (+1 on each step of the loop). And even if you would increment it (by using for loop) MIN will always be zero. And thats because

``````if (randomNum[i] < min)
``````

will never be true.
As I said, you have to 1st create an array of all 10 numbers and then find the min and max value.
There are ways of making your code work, but you will have to check on every step of the loop if any of current random number is bigger or smaller of any od the numbers in array. But this just doesnt make any sence.

This would means:

``````if (randomNum[i] > "of any of the current number is array")
{
//set max number to randomNum[i];
}
//same for min
``````

ok,i got the idea, thanks !