Counting Occurrences of Numbers

Pages: 12
I just need help with how to tally the numbers. That is it. The rest of the program is completed. I'm not asking you to write my code, I just need an example of how this would work. I find it nearly impossible to learn code by reading paragraphs, so an example of how this work would be really appreciated. Again, the only thing I need help with is the tallying.

Thank you in advance.
@Duoas - Duh@me. Of course, can't believe I missed that one!

@crumbhead

Ok, here's how my version of the tally works.

You have an array of size 16, with indexes 0-15. These indexes represents the number of occurrences of the number. So, if this array is called count_array, as it was in my example, count_array[0] will hold the number of times 0 occurs in your random numbers. count_array[3] will hold the number of times 3 occurs in your random numbers and so on, up to 15.

So we initially set these 16 indexes to hold 0. So there are no occurrences of any number. Then, we loop through the array of 20 random numbers. At each index of the random number, we store whatever is held into the temp variable. So, if your array is called random_array, for example, random_array[0] could hold 2 (or any value from 0-15). We take the value at random[0], and use that as the index of the count array. Remember that the index position of the count array represent each of the 16 digits you're keeping tally of.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for(int i=0; i<MAX_RANDOM;i++)
{
   temp = random_array[i]; // Assign whatever random number is at 0 to temp.
   count_array[temp]++; // Use that temp value as an index for count. Increment it.
}

// Let's say we're on the first iteration of the loop.
// For example, random_array[0] contains 6.
// count_array has been initialised to 0

for(int i=0; i<MAX_RANDOM;i++)
{
   // In this iteration, i=0
   temp = random_array[i]; // Temp = whatever is at random_array[0]. In this example it's 6 as described above.
   count_array[temp]++;    // Increment the value at index 6 (which is what temp is, by 1).

   // Therefore, in this iteration, we have found a 6 and incremented the count for that value.
}


Does that help any?
Last edited on
Topic archived. No new replies allowed.
Pages: 12