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!
#include <iostream>
using namespace std;
int fill_array(int a[], int size, int& number_used);
void sort(int a[], int number_used);
void swap_values(int& v1, int& v2);
int index_of_smallest(const int a[], int start_index, int number_used);
int count = 0;
int main()
{
cout << "This program sorts numbers from lowest to highest.\n";
int sample_array[100], number_used;
fill_array(sample_array, 100, number_used);
sort(sample_array, number_used);
cout << "\n";
cout << "\n";
cout << "Numbers\t";
cout << "Times" << endl;
for (int index = 0; index < number_used; index++)
{
cout << sample_array[index] << "\t";
cout << "Count will go here." << endl;
}
cout << endl;
cout << "\n";
cout << "You entered " << count << " numbers." << endl;
return 0;
}
int fill_array(int a[], int size, int& number_used)
{
cout << "Enter up to " << size << " nonnegative whole numbers.\n"
<< "Mark the end of the list with a negative number.\n";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
count++;
}
number_used = index;
return count;
}
void sort(int a[], int number_used)
{
int index_of_next_smallest;
for (int index = 0; index < number_used - 1; index++)
{
index_of_next_smallest = index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
}
}
void swap_values(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int index_of_smallest(const int a[], int start_index, int number_used)
{
int min = a[start_index],
index_of_min = start_index;
for (int index = start_index + 1; index < number_used; index++)
if (a[index] < min)
{
min = a[index];
index_of_min = index;
}
return index_of_min;
}