计算数组的频率分布?

3

我需要编写一个程序,将数据读入 int 类型的数组中。有效值的范围是 0 到 10。你的程序应该确定输入了多少个值。输出不同条目的列表以及该条目出现的次数计数

我对加粗部分有些困惑,这是我目前的进展...

static void Main(string[] args)
{
    Console.WriteLine("How many scores will you enter?");

    int total = 0;
    string inValue;
    string Size = Console.ReadLine();
    int arraySize = Convert.ToInt32(Size);
    int [] ScoreArray = new int [arraySize];

    for (int i = 0; i < ScoreArray.Length; i++)
    {

        Console.Write("Enter a Number between 0 and 10, #{0}: ", i + 1);
        inValue = Console.ReadLine();
        ScoreArray[i] = Convert.ToInt32(inValue);

        total += ScoreArray[i];
    }

    Console.WriteLine("Number of scores entered: " + arraySize);
    Console.Read();
}

1
你尝试过什么?无论如何,请考虑这个:ScoreArray[theNumber] += 1 而不是 ScoreArray[i] = ...;这会有什么区别? - user166390
@pst 你能理解“这是我目前为止的进展”吗? - L.B
@L.B. 我不明白的是,那个处理了粗体部分的方式是什么... - user166390
这个问题可能会被关闭,因为你实际上没有一个真正的问题。具体你遇到了什么问题?你不理解什么?你尝试过什么?目前你的问题只是一个无限制的要求“给我提供我不理解部分的代码”。 - David Anderson
各位,非常感谢大家及时提供的有用提示、信息和支持。这里是一个伟大的论坛社区! - Joe Fredy
显示剩余4条评论
3个回答

0

我认为这里的关键是有效值在0-10之间。我会使用数组索引来存储每个值本身。例如,如果您处理值5,则增加values[5]

因此,首先您需要初始化一个类似于以下的数组:

int[] values = new int[11]; //Values 0-10

然后,循环直到用户输入空白为止:

while(true)
{
   string inValue = Console.ReadLine();
   if(String.IsNullOrEmpty(inValue)) break;

   values[Convert.ToInt32(inValue)]++; //Bounds checking would be nice here
}

然后,您可以通过循环一次数组并打印出任何值大于0的索引来显示已排序的不同列表:

for(int i = 0; i < values.length; i++)
{
   if(values[i] > 0)
   {
      Console.WriteLine("Value {0} was entered {1} time(s)..", i, values[i]);
   }
}

这很可能是你的教授想要的。我没有测试上面的代码,那就是你的工作 :)


0

这段代码可以满足你的所有需求:

        static void Main(string[] args)
        { 
            //input data
            int[] inputArray = new int[5];
            int enteredCount = 0;
            string enteredLine = string.Empty;

            Console.WriteLine("Enter numbers from 0 to 10. If you want to end please enter nothing");
            //while user input something not blank
            while ((enteredLine = Console.ReadLine()) != string.Empty)
            {
                //inputArray has no more elements, resize it
                if (enteredCount == inputArray.Length)
                {
                    Array.Resize<int>(ref inputArray, inputArray.Length + 5);
                }

                //new entered value to array
                inputArray[enteredCount] = int.Parse(enteredLine);
                enteredCount++;
            }

            //now we need count all uniq elements
            //special array. Index is a number from 0 to 10. Value is a count of that value
            int[] counts = new int[11];
            for (int i = 0; i < enteredCount; i++)
            {
                int enteredNumber = inputArray[i];
                counts[enteredNumber]++;
            }

            Console.WriteLine("Totally were entered {0} numbers", enteredCount);

            //now we now count of each number from 0 to 11. Let' print them
            for (int i = 0; i < 11; i++)
            {
                //Print only numbers, that we entered at least once
                if (counts[i] != 0)
                    Console.WriteLine("Number {0} were entered {1} times", i, counts[i]);
            }

            Console.ReadLine();
        }

感谢每行的解释!这真的帮助我看到了每个部分的目的和存在的原因。如果只给出答案而没有解释或信息,那就像是白给一样。非常感谢这些信息。 - Joe Fredy
@JoeFredy:再看看pst的提示和Mike的帮助! - Ulf Åkerstedt
我做了,它们都正是我想要的。 它们帮助解决了“怎么做”和“如何做”的问题。 而@igofed则帮助解决了“为什么做”的问题。 我对igofed的代码进行了一些修改,以适应自己的喜好。 - Joe Fredy

0

确保添加

using System.Collections.Generic;
using System.Linq;

然后

Console.WriteLine("Distinct values entered & No. of times values entered:");

int[] distinctvalues = ScoreArray.Distinct().OrderBy(x => x).ToArray(); 
//Finds distinct values and orders them in ascending order.

int[] distinctcount = ScoreArray.FindAllIndexof(distinctvalues).ToArray();
//Finds how many times distinct values are entered.

for (int i = 0; i < distinctvalues.Length; i++)
   Console.WriteLine(distinctvalues[i].ToString() + " --------- Entered " + 
   distinctcount[i].ToString() + " times");

Console.Read();

FindAllIndexof 函数在一个 静态类 中创建一个 扩展方法,放在你的 程序类 之外。

public static class EM
{
    public static int[] FindAllIndexof<T>(this IEnumerable<T> values, T[] val)
    {
        List<int> index = new List<int>();
        for (int j = 0; j < val.Length; j++)
            index.Add(values.Count(x => object.Equals(x, val[j])));
        return index.ToArray();
    }
}

输出

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接