Counting Occurrences of Numbers

Pages: 12
Hey guys,
I am creating a program that creates a 20-block array and input random numbers. The program requires that the array be outputted as to look like this:
0
6
3
6
4
6
2
3
etc...

Following the random number output, the program needs to basically tally how many of each number were displayed (number - tally). For example, the above should return:
0 - 1
1 - 0
2 - 1
3 - 2
4 - 1
5 - 1
6 - 3

I don't want you guys to do the program for me. I just need some pointers about how to go about the tallying. I feel like the best way to go about it is a for loop with if - else if statements, but I'm having trouble figuring it out. I've tried to put it on paper, but nothing has come of it thus far.

As always, thank you in advance for your help.
What's the range on your random numbers?
Range is 0 - 15. This is what I have done so far. The array is declared and initialized in int main () and passed to CreateArray, where the random numbers are generated and consoled in. The next step is talling how many numbers of each were displayed (see post above).


EDIT: See below for most recent code.
Last edited on
Ok, easiest way would be to have two arrays.

You'll have an array of size 20, which will contain your list of numbers.

Then a counter array of size 16. You can initialise all elements to zero and use these as counters for each number. So every time you find a 0, increment the element at 0 for this array.

Then, you can just output the values at each index of the count array at the end to give you the counts for each number.

You get me?
I think I'm on the same page. Question though, how do you use Array blocks as counter? If the array was SecondArray, could I say something like:

SecondArray[1] = SecondArray[1] +1

I'm new with arrays, so bare with me here. Thanks.
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int COUNT_SIZE = 16;
int count_array[COUNT_SIZE];

for(int i=0; i<COUNT_SIZE;i++)
{
   count_array[i] = 0;
}

const int RANDOM_SIZE = 20;
int random_array[RANDOM_SIZE];
// Code to generate an array of 20 random numbers, 0-15.
// I'll leave that bit to you.

for(int j=0; j<RANDOM_SIZE;j++)
{
   count_array[random_array[j]]++;
}


Syntax looks a little funky there, but it works pretty well. I'll explain it a bit. So we're looping through each index of the random numbers. Basically, we're saying whatever is at that index (j), add a count to the count array.

So if random_array[j] = 4, then we're adding one to the count_array[4], which is essentially counting the number of occurrences for the value 4.


EDIT: If it helps, for that last bit you can extract the values. It might make it easier on the eyes.

1
2
3
4
5
6
7
int temp;

for(int j=0; j<RANDOM_SIZE;j++)
{
   temp = random_array[j];
   count_array[temp]++;
}
Last edited on
Yea, you lost me.

I am going to add a function at the bottom to do the tallying. I'm confused why you created 2 additional arrays?

EDIT: See updated function at bottom.
Last edited on
I haven't created two different arrays; they're essentially the same as yours but they're named differently. :-)

Ok, if you want to do it in functions, that's fine. But your CreateArray function has a problem. You're passing by the array by value. So when you pass in intArray from main, it creates a copy of it and works on that. Meaning the values of your array aren't changing.

Try it out; output some of the values in your array post-function. They'll likely be junk values or some uninitialized value predetermined by the IDE. The reason it looks like it's working now is because you're outputting the random numbers within the scope of the function.

If you want that array to have those values assigned, you need to pass by reference. :-)
Last edited on
Duh. Stupid mistake. Amphersands here I come. I'll update shortly, thank you.
No worries. :-)

I'm finishing work soon. Chances are I won't get home for a couple of hours.

If you haven't sorted it by then (or nobody else has helped out) I'll be happy to take another look.
Last edited on
Ok thanks. Quick question if you get this in time. Wouldn't it be ok to pass by reference to CreateArray because the random numbers are being generated only once? I am running the program without the tallying function and it seems to be working properly (that is, randomly generating numbers from 0 - 15 in each of the 20 blocks of the array). I feel like passing by reference isn't an issue until the next function comes into play?

Am I totally off base here?

Thanks again, really appreciate your help.
You are already passing the array by reference. In function argument lists, int foo[] is the same as int* foo.
If you can use the algorithm library, the easiest way to count is using the std::count function.

1
2
3
4
5
#include <algorithm>

//..
int countForThisNumber = std::count(intArray, intArray+ ARRAY_MAX, i);  //Where i is the random number you are counting.
Last edited on
If you can use the algorithm library, the easiest way to count is using the std::count function.

Unfortunately we can only use the libraries listed. Thank you though, I appreciate you trying to help me with this.
Can someone help with what iHutch was saying? I've tried to implement something like this, but it won't run at all. I think I'm misunderstanding what he/she was trying to do.

Syntax looks a little funky there, but it works pretty well. I'll explain it a bit. So we're looping through each index of the random numbers. Basically, we're saying whatever is at that index (j), add a count to the count array.

So if random_array[j] = 4, then we're adding one to the count_array[4], which is essentially counting the number of occurrences for the value 4.


EDIT: If it helps, for that last bit you can extract the values. It might make it easier on the eyes.
iHutch's Edit in his 3rd post explains that part in a more visible sense, if you still have questions, fire at will.

1
2
3
4
5
6
7
int temp;

for(int j=0; j<RANDOM_SIZE;j++)
{
   temp = random_array[j];
   count_array[temp]++;
}


I guess I just dont understand how this can tally the numbers in the array. I see the above code as assigning the current array block to a temporary integer, then adding 1 to that array block. Wouldn't this just give you the value of a given array block plus 1?

EDIT: I just re-read everything iHutch wrote, and I think I may have something here. I will get back to you guys. Thanks again for everything, this is an invaluable community. I can safely say I have learned more from you guys than I have from my actual professors.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void CountArray (int intCountArray[], int intArray[])
{
    
    int temp = 0;

    for(int intTally=0; intTally < ARRAY_MAX; intTally++)
    {
        temp = intArray[intTally];
        intCountArray[temp]++;
    }
    
    for(int intTally = 0; intTally < COUNT_SIZE; intTally++)
       {
       intCountArray[intTally] = 0;
       cout << intCountArray[intTally] << endl;
       }
       
    cout << "__________________" << endl;
    
    for(int i=0; i < ARRAY_MAX; i++)
       {
       intCountArray[intArray[i]]++;
       cout << intCountArray[intArray[i]] << endl;
       }
   

     
}


Yea, I'm lost. I tried outputting it 2 separate ways and neither works. The first output sends out all 0's and the other output simply counts from 0 to 19.

Like I said, I'm new to arrays so I'm still trying to fully understand how to use them properly. Any input would be really helpful.
The first output sends out all 0's and the other output simply counts from 0 to 19.

That is because you set them to zero before you display them... (lol) doh!

Not sure what you are trying to accomplish with the second one.

Once you've tallied the count array, you now how the 'count' for each of your possible values in the array so from 0 .. 15, at the index of 0 you might have the value of 2, and then at the index of 1 you might have the value of 0, then at 2 you might have the value of 1, the values at the index are the counts for the numbers that were in the randomly generated array.
But the problem is the tally doesn't work - or better yet, I don't understand how to make it work.

Not sure what you are trying to accomplish with the second one.

I was trying what iHutch was suggesting (most likely did something wrong though)
Pages: 12