/* Filename:  qsorttst.cpp

   Revised by:  Br. David Carlson

   Date:  September 23, 1998

   Last Revised:  November 28, 2001

   This program asks the user for the number of items to place into
   an array, fills the array with that many random ints, prints the
   array, sorts it via quicksort, and then prints the sorted array.

   Tested with:
      Microsoft Visual C++ 6.0
      Microsoft Visual C++ .NET
      g++ under Linux
*/

#include <iostream>
#include <iomanip>    // needed for setw
#include <cstdlib>    // needed for srand, rand functions
#include <ctime>      // needed for time
#include "qsort.h"
using namespace std;


// Function prototypes:

void PrintArray(IntArray NumArray, int Count);

void FillArray(IntArray NumArray, int Count);


int main(void)
   {
   int Count;
   IntArray NumArray;

   cout << "Enter the number of items to place in the array (1 to "
      << MaxArray << "): ";
   cin >> Count;
   while ((Count < 1) || (Count > MaxArray))
      {
      cout << "Reenter.  Use a number from 1 to " << MaxArray
         << "." << endl;
      cin >> Count;
      }

   FillArray(NumArray, Count);
   cout << "The original array of random numbers:" << endl;
   PrintArray(NumArray, Count);
   QuickSort(NumArray, 0, Count - 1);

   cout << endl << "The sorted array:" << endl;
   PrintArray(NumArray, Count);

   return 0;
   }


/* Given:  NumArray  Array of ints, from index 0 to Count - 1
           Count     Number of ints in array NumArray.
   Task:   To print all the ints in NumArray on the screen, 10 per line.
   Return: Nothing.
*/
void PrintArray(IntArray NumArray, int Count)
   {
   int k;

   for (k = 0; k < Count; k++)
      {
      cout << setw(5) << NumArray[k];
      if ((k % 10 == 9) || (k == Count - 1))
         cout << endl;
      }
   }


/* Given:  Count     Number of ints to place into NumArray.
   Task:   To place into NumArray, at indices 0 to Count - 1, random
           ints between 0 and 500.
   Return: NumArray  Array filled with Count data items.
*/
void FillArray(IntArray NumArray, int Count)
   {
   int k;

   srand(time(NULL));  // seed the random number generator

   // Fill NumArray with random numbers between 0 and 500
   for (k = 0; k < Count; k++)
      NumArray[k] = (500.0 * rand()) / RAND_MAX;
   }


