// File: inclass12.cpp
// This program reads in up to 15 numbers into an array and prints
// them out in reverse order.
//
// Input:  Number values
// Output: Values in reverse order
// ----------------------------------------------------------------------
// Class: CS 210                     Instructors: Drs. Hwang & Roberts
// Assignment: In-class exercise for 2/20/07 & 2/21/07
// Programmer(s): <fill in your name (s)>

#include <iostream>                       // >>, <<, cin, cout, endl
using namespace std;

// Function prototypes
void ReadNumbers (double anArray[], int & count);
void PrintReverse (const double anArray[], int numElements);

const int CAPACITY = 15;   // capacity of array

int main()
{
   double numbers[CAPACITY];  // array of integers
   int numElements;           // number of elements stored in array

   cout << "\nThis program reads in up to " << CAPACITY 
        << " integers into an array\n"
        << "and prints them out in reverse order.\n";
   ReadNumbers (numbers, numElements);
   PrintReverse (numbers, numElements);

   return 0;
}  // end main

// Function: ReadNumbers
// Reads values from keyboard until a negative # or CAPACITY and 
// stores tham in an array
void ReadNumbers (double anArray[],     // PASSED BACK: array to be filled
                  int & count)          // PASSED BACK: number of values read
{
   double value;

   // Initialize count
   count = 0;

   // Read in first value
   cout << "\nPlease enter a value (negative to quit): ";
   cin >> value;

   // Repeat until sentinel value or array is full
   while ((value >= 0) && (count < CAPACITY))
   {
      // Store value in array
      anArray[count] = value;
      count++;

      // Read next value
      cout << "Please enter a value (negative to quit): ";
      cin >> value;
   }  // end while
}  // end ReadNumbers

// Function: PrintReverse
// Displays elements of an array in reverse order
void PrintReverse (const double numbers[], // RECEIVED: array of numbers
                   int numElements)        // RECEIVED: number of values stored
{
   cout << "\nThere are " << numElements << " values in the array\n";
   cout << "The values in reverse order are:\n";

   // Index array backwards
   for (int count = numElements-1; count >= 0; count--)
      cout << numbers[count] << endl;
}  // end PrintReverse
