找到平均值。 int数组的最大,最小数量和模式

时间:2015-07-06 00:09:29

标签: c#

您好我正在尝试创建一个程序,用户可以输入最多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) 
    {

    }
    }

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:-4)

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;
}