C # Gets the maximum, minimum, and average values in an array

Source: Internet
Author: User

C # Gets the maximum, minimum, and average values in an array of 1. Give an array
1             int New int 1,2,4,3,0,-1,545,2 ;
2. Array of arrays self-band method

itself is directly able to call Min (), Max (), Average () method to find the minimum, maximum, and average values

1             Console.WriteLine ("--------------Array own method-----------------"); 2             Console.WriteLine ("min:{0}", array. Min ()); 3             Console.WriteLine ("max:{0}", array. Max ()); 4             Console.WriteLine ("average:{0}", array. Average ());
Output Result:
1 --------------array Self method-----------------2 min:-13 Max:5454 Average:62.4
3. Encoding for minimum value
1         /// <summary>2         ///Minimum Value3         /// </summary>4         /// <param name= "Array" ></param>5         /// <returns></returns>6          Public Static intMin (int[] array)7         {8             if(Array = =NULL)Throw NewException ("array Null exception");9             intValue =0;Ten             BOOLHasValue =false; One             foreach(intXinchArray) A             { -                 if(HasValue) -                 { the                     if(x < value) value =x; -                 } -                 Else -                 { +Value =x; -HasValue =true; +                 } A             } at             if(HasValue)returnvalue; -             Throw NewException ("didn't find"); -}
Maximum Value
1         /// <summary>2         ///Maximum Value3         /// </summary>4         /// <param name= "Array" ></param>5         /// <returns></returns>6          Public Static intMax (int[] array)7         {8             if(Array = =NULL)Throw NewException ("array Null exception");9             intValue =0;Ten             BOOLHasValue =false; One             foreach(intXinchArray) A             { -                 if(HasValue) -                 { the                     if(X >value) -Value =x; -                 } -                 Else +                 { -Value =x; +HasValue =true; A                 } at             } -             if(HasValue)returnvalue; -             Throw NewException ("didn't find"); -}
Average
1         /// <summary>2         ///Average3         /// </summary>4         /// <param name= "source" ></param>5         /// <returns></returns>6          Public Static Double? Average (int[] array)7         {8             if(Array = =NULL)Throw NewException ("array Null exception");9             Longsum =0;Ten             LongCount =0; One             checked A             { -                 foreach(int? VinchArray) -                 { the                     if(V! =NULL) -                     { -                         intA =V.getvalueordefault (); -Sum + =V.getvalueordefault (); +count++; -                     } +                 } A             } at             if(Count >0)return(Double) Sum/count; -             return NULL; -}
4. Test output Test Code
1         Static voidMain (string[] args)2         {3             int[] Array =New int[] {1,2,4,3,0,-1, the,545,2, the};4 5Console.WriteLine ("--------------Array's own method-----------------");6Console.WriteLine ("min:{0}", Array. Min ());7Console.WriteLine ("max:{0}", Array. Max ());8Console.WriteLine ("average:{0}", Array. Average ());9 TenConsole.WriteLine ("---------------Internal Implementation method------------------"); One             intMin =program.min (array); A             intMax =Program.max (array); -             Double? Average =program.average (array); -Console.WriteLine ("Min:"+min); theConsole.WriteLine ("Max:"+max); -Console.WriteLine ("Average:"+average); - Console.read (); -}
Output results

The above code is also extracted from the. NET Framework, in fact, the array's own maximum, minimum, average value of the algorithm is done, in the. NET Framework source code can be seen

C # Gets the maximum, minimum, and average values in an array

Related Article

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.