Mart Nta

Mart Nta

  • NA
  • 9
  • 918

how can i change console code to win Forms

Jul 24 2019 3:42 PM
  1. public static decimal GetMedian(this IEnumerable < int > source) {  
  2.  // Create a copy of the input, and sort the copy   
  3.  int[] temp = source.ToArray();  
  4.  Array.Sort(temp);  
  5.  int count = temp.Length;  
  6.  if (count == 0) {  
  7.   throw new InvalidOperationException("Empty collection");  
  8.  } else if (count % 2 == 0) {  
  9.   // count is even, average two middle elements   
  10.   int a = temp[count / 2 - 1];  
  11.   int b = temp[count / 2];  
  12.   return (a + b) / 2 m;  
  13.  } else {  
  14.   // count is odd, return the middle element   
  15.   return temp[count / 2];  
  16.  }  
  17. }  
  18. public static int GetMode(this IEnumerable < int > list) {  
  19.   // Initialize the return value   
  20.   int mode =  
  21.    default (int);  
  22.   // Test for a null reference and an empty list   
  23.   if (list != null && list.Count() > 0) {  
  24.    // Store the number of occurences for each element   
  25.    Dictionary < intint > counts = new Dictionary < intint > ();  
  26.    // Add one to the count for the occurence of a character   
  27.    foreach(int element in list) {  
  28.     if (counts.ContainsKey(element))  
  29.      counts[element]++;  
  30.     else  
  31.      counts.Add(element, 1);  
  32.    }  
  33.    // Loop through the counts of each element and find the    
  34.    // element that occurred most often   
  35.    int max = 0;  
  36.    foreach(KeyValuePair < intint > count in counts) {  
  37.     if (count.Value > max) {  
  38.      // Update the mode  
  39.      mode = count.Key;  
  40.      max = count.Value;  
  41.     }  
  42.    }  
  43.   }  
  44.   return mode;  

Answers (3)