#include <iostream.h>

// Array Rank Programming Problem
// Input: An array size, followed by the specified number of integers
// Output: The corresponding rank array.

// The rank of an element is the number of smaller elements in the
// array plus the number of equal elements that appear to its left.
// The rank array is an array of element ranks.  For example, if the
// input array is [4, 3, 9, 3, 7], the corresponding rank array is
// [2, 0, 4, 1, 3].


// function headers:
int prompt_size();

int main()
{
  // get input from user:
  const int array_size = prompt_size();
  int the_ints[array_size];
  int ranks[array_size];

  cout << "Enter the ints, each on a separate line:" << endl;
  for( int i = 0; i < array_size; i++ )
  {
    cin >> the_ints[i];
    ranks[i] = 0;   // simultaneously set all ranks to 0 as we get input
  }                 // (saves having another for loop)

  // compare all pairs of elements, increment the proper ranks
  for( int i = 1; i < array_size; i++ )
    for( int j = 0; j < i; j++ )
      if( the_ints[j] <= the_ints[i] )
        ranks[i]++;
      else
        ranks[j]++;

  // output the rank array
  cout << "The input array was: " << endl;
  for( int i = 0; i < array_size; i++ )
    cout << the_ints[i] << " ";
  cout << endl << "The corresponding rank array is: " << endl;
  for( int i = 0; i < array_size; i++ )
    cout << ranks[i] << " ";
  cout << endl;

  return 0;
}


int prompt_size()
{
  int s = 0;
  cout << "Enter desired size of array : ";
  cin >> s;
  while( s < 1 )
  {
    cout << "Please enter a size of at least 1: ";
    cin >> s;
  }
  return s;
}
