Negligence in finding the Maximum/minimum values of a sequence

Source: Internet
Author: User
To do this, we want to find the maximum or minimum value of list <double>, So we write a method.
/// <Summary>
/// Minimum value
/// </Summary>
/// <Param name = "_ series"> </param>
/// <Returns> </returns>
Public static double min (list <double> _ Series)
{
Double result = 0;

Foreach (double item in _ Series)
{
If (result> item)
{
Result = item;
}
}

Return result;
}
It seems that there is no problem, but during the test, it is impossible to show 0 results in the sequence. How can the minimum value be 0? Later, I carefully read this method. The logic is indeed faulty. The initial value of result is 0, which is certainly smaller than any number in the sequence. So we made the following changes:
/// <Summary>
/// Minimum value
/// </Summary>
/// <Param name = "_ series"> </param>
/// <Returns> </returns>
Public static double min (list <double> _ Series)
{
Double result = _ series [0];

Foreach (double item in _ Series)
{
If (result> item)
{
Result = item;
}
}

Return result;
}
Another interesting thing is that Division means that the branch office can be 0. Of course, the result is weird.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.