This is the problem:
Write a program that reads in an array of type int. Provide facility to either read
this array from the key board or from a file, at the user's option. If the user chooses
file input, the program should request a file name. You may assume that there are fewer than
50 entries in the array. Your program determines how many entries there are. The output is
to be a two-column list. The first column is a list of the distinct array elements; the
second column is the count of the number of occurrences of each element. The list should be
sorted on entries in the first column, largest to smallest.

For the array

-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12

the output should be

N Count

4 2

3 3

2 2

1 4

-1 1

-12 4

and this is what I have so far:

#include <iostream>

void fillArray(int a[], int SIZE, int& lastElement);
void sort(int a[], int lastElement);
void swapValues(int& v1, int& v2);
int indexSmallest(const int a[], int startIndex, int lastElement);
void showFrequency (int a[], int SIZE);


int main( )
{
    using namespace std;
    const int SIZE = 50;
    int arr[SIZE] = {0};
    int lastElement; //the index

    cout << "Enter scores:\n";
    fillArray (arr, SIZE, lastElement);
    sort (arr, lastElement);
    showFrequency (arr, lastElement);
    return 0;
}



void fillArray(int a[], int SIZE, int& lastElement)
{
    using namespace std;
    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;
    }
    lastElement = index;
}
void sort(int a[], int lastElement)
{
    int index_of_next_smallest;
    for (int index = 0; index < lastElement - 1; index++)
    {
        index_of_next_smallest =
                     indexSmallest(a, index, lastElement);
        swapValues(a[index], a[index_of_next_smallest]);

    }
}


void swapValues(int& v1, int& v2)
{
    int temp;
    temp = v1;
    v1 = v2;
    v2 = temp;
}


int indexSmallest(const int a[], int startIndex, int lastElement)
{
    int min = a[startIndex],
        index_of_min = startIndex;
    for (int index = startIndex + 1; index < lastElement; index++)
        if (a[index] < min)
        {
            min = a[index];
            index_of_min = index;
            //min is the smallest of a[start_index] through a[index]
        }

    return index_of_min;
}

void showFrequency (int a[], int SIZE)
{ 
  using namespace std;
  int prev = a[0], freq = 1; 
  cout << "Number        count \n";
  cout << a[0] << "   ";
  for (int i=1; i<= SIZE; i++)
  { if (prev == a[i])
        freq++;
  else 
  { cout << freq;
    cout << a[i] << " ";
    freq = 1;
    prev = a[i];
  }
  }
  cout << freq << endl;
}

it's not coming out right... can anyone help?

Edited 3 Years Ago by Nick Evan: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.