Reputation: 3132
I am writing a little app for statistics. A function of this app will be determining the frequency of randomly generated numbers in an Array
which holds 100 elements.
So far, I managed to generate a two-dimensional Array
which I have filled with the numbers 0-100 (these are max and min of randomly generated numbers in the other Array
).
I thought of filling this array with the frequency based on the index.
Like this:
//Array for random numbers
int[] randomNumbers = new int[10] {2, 3, 4, 5, 5, 2, 8, 9, 3, 7};
//Two-dimensional array for holding frequency of each unique number
int[,] frequency = new int[2,101];
for (int i = 0; i <= 100; i++)
{
frequency[0, i] = i;
}
The frequency array gets generated fine. But I don't know where to go from here. How could I add the frequency of each unique number to the array? I tried this:
for (int i = 0; i < randomNumbers.Length; i++)
{
frequency[1, i] = frequency[1, i] + 1;
}
But it just fills the frequency array with ones continuously.
Upvotes: 1
Views: 2594
Reputation: 768
You should be able to do this:
int[] randomNumbers = new int[10] {2, 3, 4, 5, 5, 2, 8, 9, 3, 7};
Dictionary<int, int> dictionary = new Dictionary<int,int>();
foreach(int randomNumber in randomNumbers)
{
if (!dictionary.ContainsKey(randomNumber))
dictionary.Add(randomNumber, 1);
else
dictionary[randomNumber]++;
}
If you then want to sort them, use Linq...
var sortedList = from pair in dictionary
orderby pair.Key
select pair;
Upvotes: 2
Reputation: 12811
I think you're looking for something like this:
int[] randomNumbers = ...;
int[] frequency = new int[101];
for (int i = 0, l = randomNumbers.Length; i < l; ++i)
++frequency[randomNumbers[i]];
The result of this is that the frequency
array contains the number of occurrences for each index. In other words frequency[0]
will contain the number of occurrences of the value 0
, frequency[1]
will contain the number of occurrences of the value 1
, etc...
Upvotes: 2
Reputation: 6122
To me, it looks like a Dictionary<int, int>
is what's best suited to your needs:
Dictionary<int, int> frequency = new Dictionary<int, int>();
foreach (int number in randomNumbers)
{
if (frequency.ContainsKey(number))
frequency[number]++;
else
frequency.Add(number, 1);
}
int numberOfGroups = frequency.Count;
Upvotes: 2
Reputation: 2317
I would do it that way, you have index in frequency table as value of number and actual value is frequency (just something like that not exactly):
for (int i = 0; i <= 100; i++)
{
frequency[i] = randomNumbers.SelectMany( o=>o.ArrayProperty==i ).Count() ;
}
Upvotes: 2
Reputation: 939
You may find it useful to have two arrays, and each time you generate a random number, you would set the next element of the first array to the number, and set the random numberth element of the second array to itself +1. This will give you statistics from the start (from POV of the random number generator.
int[] vals, stats;
vals = new int[100]
stats = new int[100]
for (int i = 0; i < 100; i++)
{
vals[i] = getRandom();
stats[vals[i]] = stats[vals[i]] + 1;
}
Something like that might do the trick :)
Upvotes: 2
Reputation: 203834
GroupBy
makes this pretty straightforward.
var frequencies = randomNumbers.GroupBy(n => n)
.Select(group => new
{
Number = group.Key,
Frequency = group.Count()
});
Upvotes: 12