[英]In randomly generated array minimum number is always 0
I would like to know why the minimum number is always "0".我想知道为什么最小数字总是“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}");
}
}
because you initialize the min
variable with array[0]
before you populate the array.因为您在填充数组之前使用array[0]
初始化了min
变量。
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.初始化数组会将其所有项目初始化为其默认值(有关更多详细信息,请阅读Why doesn't a struct in an array have to be initialized? ),并且int
的默认值为0
,并且您的数组仅包含数字大于0
,你的min
永远不会改变。
Initialize it to number2
or int.MaxValue
and you'll get the actual minimum value.将其初始化为number2
或int.MaxValue
,您将获得实际的最小值。
Put a breakpoint on the min assignment line: min is always 0 because you don't pass on it...在 min 赋值行上放一个断点: min 总是 0 因为你没有传递它......
Because array[i] < min
is always false since min
starts from 0
and array[i]
is always higher.因为array[i] < min
总是假的,因为min
从0
开始并且array[i]
总是更高。
To solve the problem without rewriting the algorithm you can set min as:要在不重写算法的情况下解决问题,您可以将 min 设置为:
min = int.MaxValue;
Since max
starts from 0
, it works for it but you should write:由于max
从0
开始,它适用于它,但你应该写:
max = 0;
Even if array[0]
is initialized to 0
by default.即使array[0]
默认初始化为0
。
Your min
value will always be zero beacuse of this:由于以下原因,您的min
将始终为零:
else if (array[i] < min)
{
min = array[i];
}
You are initalizing min
with zero.您正在将min
初始化为零。 When you take a number from Random.next
it will give [1,100) so min will always smaller than randomNumber
.当您从Random.next
中获取一个数字时,它将给出 [1,100) 因此 min 将始终小于randomNumber
。 Because of that, your min value never changes.因此,您的最小值永远不会改变。
Instead you can do:相反,您可以这样做:
min = int.MaxValue;
before the for loop.在 for 循环之前。
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. JiT 编译器非常擅长削减未充分使用的临时变量或添加临时变量以避免过多的索引器访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.