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.
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).
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.
constint COUNT_SIZE = 16;
int count_array[COUNT_SIZE];
for(int i=0; i<COUNT_SIZE;i++)
{
count_array[i] = 0;
}
constint 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.
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. :-)
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?
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.
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.
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.