I am writing a program that allows the user to insert numbers and then the program sorts them into ascending order. I have this accomplished, however, I also need an array to count the number of times each number the user inserts occurs. For ex: Say the user inserts 3 6 7 6 1. I need the array to keep count of the number of times these numbers occur. I also need to display the number/numbers that occur the most. I have struggled with these issues for the past few days and am not very good with arrays. I would really appreciate any help you could offer. Thank you!
for the first problem, counting the number of times each number occurs in the array, you want to creat a second array that is the same size as the first one. Initialize all cells in this new array to 0. Pass both of those arrays to your counting function. You will have two loops, one inside the other. The first loop will loop through the second array, and the second loop will loop through the first array. By comparing the value of the cell in the first array that corresponds to the cell the second array is currently on, testing each value in the first array and incrementing the value in the second array by one each time you have a match, you will end up with the second array being populated with the number of times each number occured in the first array.
Ok...I have tried out your suggestion and so far I have it working without any compilation errors. However, I get some pretty crazy numbers in my counter column in the results. Here is my new code:
But now in the result it only tells how many times the first number was displayed. The other numbers display 0 times:
Numbers Times
1 1
2 0
3 0
Why is this?
Also, I do not have anything in my code yet to display the number(s) that occur the most. I was wondering how to go about that. I am learning more with your help! Thank you!
Ok...looking through your code I don't see anything wrong with the counter function...One thing to note though, is that you don't need a return value for that counter function...arrays automatically get passed by reference...you could have just as easiler used the following function header:
void count_array(int sample_array[], int counter[], int number_used)
and not returned a value...I just compiled a small test program on my computer and it ran the code just fine...you might try using void instead and recompiling and see how it goes...
As for the other problem (which number occurs the most...once you get your second array (the one with the count of numbers) you will have your number(s) that occur the most...You just need to figure out how to use it...
Wonderful...it now works! I will continue to work on displaying the numbers that occur the most often.
I have another question though. I have used a selection sort to sort the numbers into ascending order. Out of curiosity, how could I change it to an insertion sort, so that a number is sorted into place as soon as the user enters it? It seems it would make more sense to di it this way. Thank you!
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.