求平均值.int数组的最大、最小数和模式

本文关键字:小数 模式 int 数组 平均值 | 更新日期: 2023-09-27 18:01:04

嗨,我正在尝试制作一个程序,用户最多可以输入25个数字,然后它"告诉"用户平均数、最小数和最大数以及输入数字的模式,但我不确定如何做到这一点。

如有任何指导,将不胜感激

这是我到目前为止的

static void Main(string[] args)
    {
        Console.WriteLine("enter the amount of numbers you would like to use of: ");
        int arraylength = Int32.Parse(Console.ReadLine());
        int[] AverageArray = new int[25];
        //filling the array with user input
        for (int i = 0; i < AverageArray.Length; i++)
        {
            Console.Write("enter the numbers you wish to find the average for: ");
            AverageArray[i] = Int32.Parse(Console.ReadLine());
        }
        //printing out the array 
        Console.WriteLine("here is the average: ");
        for (int i = 0; i < AverageArray.Length; i++)
        {
            Console.WriteLine(AverageArray[i]);
        }
        Console.WriteLine(FindAverage(AverageArray));
    }

    public static double FindAverage(int[] averageNumbers)
    {
        int arraySum = 0;
        for (int i = 0; i < averageNumbers.Length; i++)
            arraySum += averageNumbers[i];
        return arraySum / averageNumbers.Length;
    }
    public static double LargestNumber(int[] Nums, int Count)
    {
    }
    public static double SmallestNumber(int[] Nums, int Count)
    {
    }
    public static double Mode(int[] Nums, int Count) 
    {
    }
    }

求平均值.int数组的最大、最小数和模式

var smallest = AverageArray.Min();
var largest = AverageArray.Max();
var mode = AverageArray.GroupBy(v => v)
            .OrderByDescending(g => g.Count())
            .First()
            .Key;

如果您不知道如何使用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"));
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;
}