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

后端 未结 3 1774
耶瑟儿~
耶瑟儿~ 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: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;
    }
    

提交回复
热议问题