//  File: array.C
//
//  implementation of array class for testing sorting algorithms

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include "array.h"


// Constructor initializes array to random values
//
Array::Array(int n /* = DSIZE */, long seed /* = 0 */) {

   if (n <= 0) {
      cerr << "Bad array size" << endl ;
      exit(1) ;
   }

   // if no seed given for random number generator, use time
   //
   if (seed == 0) seed = (long) time(NULL) ;
   srand48(seed) ;

   arr = new DATA[n] ;
   if (arr == NULL) {
      cerr << "Out of memory in array constructor" << endl ;
      exit(1) ;
   }

   size = n ;
   Array::seed = seed ;

   for (int i = 0 ; i < size ; i++) {
      arr[i] = (DATA) drand48() ;
   }
}


void Array::print() {

   for (int i = 0 ; i < size ; i++) {
      cout << setw(12) << arr[i] ;
      if (i % 5 == 4) cout <<endl ;
   }

   // need last CR?
   if (size % 5 != 0) {
      cout << endl ;
   }
}


bool Array::check() {

   for (int i = 0 ; i < size-1 ; i++) {
      if (arr[i] > arr[i+1]) return false ;
   }
   return true ;
}
