Find the average. largest, smallest number and mode of a int array

后端 未结 3 1773
耶瑟儿~
耶瑟儿~ 2021-01-29 17:01

Hi I am trying to make a program where the user can enter up to 25 numbers and then it tells the user the average, smallest and largest number and the mode of the numbers entere

相关标签:
3条回答
  • 2021-01-29 17:17
    var smallest = AverageArray.Min();
    var largest = AverageArray.Max();
    var mode = AverageArray.GroupBy(v => v)
                .OrderByDescending(g => g.Count())
                .First()
                .Key;
    
    0 讨论(0)
  • 2021-01-29 17:24
    public static double LargestNumber(int[] Nums, int Count)
    {
        double max = Nums[0];
    
        for (int i = 1; i < Count; i++)
            if (Nums[i] > max) max = Nums[i];
    
        return min;
    }
    public static double SmallestNumber(int[] Nums, int Count)
    {
        double min = Nums[0];
    
        for (int i = 1; i < Count; i++)
            if (Nums[i] < min) min = Nums[i];
    
        return min;
    }
    public static double Mode(int[] Nums, int Count) 
    {
        double mode = Nums[0];
        int maxCount = 1;
    
        for (int i = 0; i < Count; i++) 
        {
           int val = Nums[i];
           int count = 1;
           for (int j = i+1; j < Count; j++)
           {
              if (Nums[j] == val) 
                 count++;
           }
           if (count > maxCount) 
           {
                mode = val;
                maxCount = count;
           }
        }
        return mode;
    }
    
    0 讨论(0)
  • 2021-01-29 17:29

    If you don't know how to use linq

       /*Assign Initial Value*/
            int highestNum = numberArray[0];
            int lowestNum = numberArray[0];
            int sum = 0;
    
            foreach (int item in numberArray)
            {
                /*Get The Highest Number*/
                if (item > highestNum)
                {
                    highestNum = item;
                }
                /*Get The Lowest Number*/
                if (item < highestNum)
                {
                    lowestNum = item;
                }
                //get the sum
                sum = item + sum;
            }            
    
            //display the Highest Num
            Console.WriteLine(highestNum);
            //display the Lowest Num
            Console.WriteLine(lowestNum);
            //Display The Average up to 2 decimal Places
            Console.WriteLine((sum/numberArray.Length).ToString("0.00"));
    
    0 讨论(0)
提交回复
热议问题