Calculate the frequency distribution of an array?

I need to write a program that reads data into an array of type int. Valid values ​​are from 0 to 10. Your program should determine how many values ​​have been entered. List individual entries and the number of times how many times this entry has occurred .

I am having problems with the highlighted area, this is what I still have ...

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();
}
+3
source share
3 answers

I think the key here is the valid values ​​are between 0-10 . I would use an array index to store each value. For example, if you process a value of 5, an increment values[5].

So, first you must initialize the array, for example:

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();
        }
0

Be sure to add

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

Then

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();

For a function, FindAllIndexofcreate Extension Methodin static classoutside of your Programclass

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

Output

enter image description here

0
source

All Articles