Insertion Sort and Number Occurence

Hello! I have written a program that lets the user enter numbers and they are then sorted into ascending order. The number of times each number occurs is also displayed. I previously wrote it using a selection sort but could not get the times each number occured to display correctly. Now I have tried to write it using an insertion sort. The number occurences display correctly now, however, I can't get the list of numbers to display right. I get a bunch of random numbers and letters. If someone could help me tidy up my code I'd greatly appreciate it.

Also, I would like to display the number(s) that occur most often. Say for ex: I enter 6 3 6 3 5. Both 6 and 3 would would be displayed. How would I go about doing this? Thank you! :cheesy:

A map would be one way to do it. If you don't know about maps, then using a sequence of arrays can do it, too.

After your initial array is sorted it will look like this:
A => 33566
Now you could set up an array of unique numbers in the array which could look like this:
B => 356
and a separate array of frequencies for each of those numbers which would look like this:
C => 212
Then you could sort the frequencies changing B and C at the same time so you end up with something like this:
B => 536
C => 122

If you don't want to change B and C you could always use new arrays to hold the unique values associated with any given frequency in the order of their frequency of occurrence.

I think you are getting your variables mixed up. In fill_array() you assign the value of index to number_used which is passed to fill_array as a reference to an int so that value can be passed from main() to insertion_sort(), but then you assign 1 to number_used which overwrites the number you took pains to retrieve and pass to insertion_sort() in the first place. That in turn makes it difficult for me to follow your logic in insertion_sort().

Even more basic to me, however, is that insertion_sort() implies that you sort the values as they are inserted, not after the entire array has been entered.

I have taken everything out of your program except the fill_array and insertion_sort subroutines and changed a few things cosmetically to suit my sense of style. The major change from your last post has to do with the fill_array() function.

Wonderful! I now have my code working! You have been so helpful! Is there another way I can display the numbers that occur the most without using a map or array? I haven't learned maps and I can't imagine being assigned something I haven't learned yet. Is there a function or an if-statement that coud accomplish this? We only need 2 arrays for this project, one for the numbers and one for the count.

>>Is there another way I can display the numbers that occur the most without using a map or array?

Make sample_array() an array of unique values instead of an array of all values. (Note: you will need an input counter to make sure that total number of numbers entered by user doesn't exceed a given value if they are only allowed to enter a given number of inputs. In your code you are already doing this partway with count++) That is after you find the insertion point check to see if sample_array is the same value as valueToEnter. If it is, then you increase the value of counter by one but you don't insert valueToEnter into sample_array. If it isn't, then you insert the valueToEnter as before, but you must also shift the values of the elements in counter similarly. That is each counter[x] represents the number of times sample_array[x] is entered by the user.

When user input has been completed you can search the counter for the max frequency value. Then you can search counter for all the elements with the value of maximum frequency and output the elements in sample_array and counter whose index is associated with the maximum frequenct value.

In the following code I've changed the function prototypes slightly to accomodate passing counter back and forth. I've commented where I'd add things I talked about above, but I've not actually written all of the code. WARNING: I've not tested the code.