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}");
}
}
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.
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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.