I am trying to create a code that receives up to 50 integers from the keyboard, sorts the array from largest to smallest, then counts the instances of each number and displays the number inputted and the count of the number of instances. For example, if the number 11 was put in twice, the output would be:
N Count
11 2
If 11 were input 3 times and 55 twice, 1 once, then the output would be:
N Count
55 2
11 3
1 1
I believe that using a new two-dimensional array is the way to go, but can't sort out how to use a one-dimensional array to create a two dimensional array based on calculations. I have written the code to accept the input, fill the array, sort the input by swapping values, and a test output to show that that everything is functioning, but cannot determine how to create the output I desire. I believe the pseudo-code should be something like this:
function name: countArray
a[i] = b[i], c [j] where i is the index, and j is a counter both initialized to zero
for (i = 0, i <= numberUsed, i++)
{
if a[i+1] == a[i]
c[j]++;
else a[i+1] = b[i+1]
}
I have tried many variations of code to make this work and gotten so frustrated that I deleted each of my attempts. I am now posting "clean" code and on line 38 I have the comment "// SECOND ARRAY FILL GOES HERE"
can someone give me some idea of how the function definition, function call, and the guts of the function should look? I'd appreciate any insight.
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX_ENTRIES = 50;
void fillArray(int a[], int size, int& numberUsed);
//Precondition: size is the declared size of the array a.
//Postcondition: numberUsed is the number of values stored in a.
//a[0] through apnumberused-1] have been filled with
//nonnegative integers read from the keyboard.
void sortArray(int a[], int numberUsed);
//Precondition; numberUsed <= declared size of the array a.
//The array elementsa[p0] through a[numberUsed -1] have values.
//Postcondition: The values of a[0] through a[numberUsed -1] have
//been rearranged so that a[0] <= a[1].......<= a [numberUsed -1].
void swapValues(int& v1, int& v2);
//Interchanges the values of v1 and v2.
int indexOfLargest(const int a[], int startIndex, int numberUsed);
//Precondition: 0 <= startIndex < numberUsed. Reference array elements
//have values. Returns the index i such that a[i] is the largest of the
//values a[startIndex], a[startIndex + 1], ..., a[numberUsed -1].
int main()
{
int entry[MAX_ENTRIES], numberUsed; // variables used
cout << "This program reads entries from the keyboard,\n"
<< "fills an array, sorts the entered array, and\n"
<< "outputs a two column list showing the number\n"
<< "of times each value appears in the array.\n";
fillArray(entry, MAX_ENTRIES, numberUsed); // fill array with input from user
sortArray(entry, numberUsed); // sort array
// SECOND ARRAY FILL GOES HERE
//test output for sorted array
for (int i = 0; i < numberUsed; i++)
{
cout << setw(12) << entry[i];
cout << endl;
}//end of test output for sorted array
return 0;
}
void fillArray(int a[], int size, int &numberUsed) // recieves data from the keyboard and stores it in the array
{
cout << "Enter up to " << size << "numbers. When finished\n"
<< "input 0 'zero' to mark your entries complete.\n";
int next, index = 0;
cin >> next;
while ((next != 0) && (index < size)) // if input is '0', exits the input and returns to main
{
a[index] = next;
index++;
cin >> next;
}
numberUsed = index;
}
void sortArray(int a[], int numberUsed) // sorts the array so values are arranged from largeest to largest
{
int indexOfNextLargest;
for (int index = 0; index < numberUsed -1; index++)
{
indexOfNextLargest = indexOfLargest(a, index, numberUsed); // calls function indexOfLargest to find the next largeest number
swapValues(a[index], a[indexOfNextLargest]); // swaps the next largest value found by indexOfLargest, with the current value
}
}
void swapValues(int&v1, int &v2) // simple swaping function to switch 2 values
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int indexOfLargest(const int a[], int startIndex, int numberUsed) // searches the array to find the next largest value and returns the
// index of the value found to the sortArray function
{
int min = a[startIndex], indexOfMin = startIndex;
for (int index = startIndex +1; index < numberUsed; index++)
if (a[index] > min)
{
min = a[index];
indexOfMin = index;
}
return indexOfMin;
}