// File: intarray.C // // Implementation of the IntArray class. #include #include #include #include #include #include "intarray.h" // Constructor // IntArray::IntArray(int n /* = 1 */) { size = n ; A = new int[n] ; if (A == NULL) { cerr << "Out of memory in IntArray constructor" << endl ; } srand48(time(NULL)) ; for (int i = 0 ; i < n ; i++) { A[i] = lrand48() ; } } // Destructor // IntArray::~IntArray() { delete [] A ; } // Print the array // void IntArray::print() { cout << "integer array [" << size << "]\n" ; for (int i = 0 ; i < size ; i++) { cout << "A[" << i << "] = " << A[i] << endl ; } cout << "\n" ; } void IntArray::swap(int i, int j) { int temp ; if (i < 0 || i >= size || j < 0 || j >= size) cout << "array index out of bounds " << i << " or " << j <<"\n" ; temp = A[i] ; A[i] = A[j]; A[j] = temp ; } int IntArray::cmp(int i, int j) { if (i < 0 || i >= size || j < 0 || j >= size) cout << "array index out of bounds " << i << " or " << j <<"\n" ; if (A[i] < A[j]) return -1 ; if (A[i] == A[j]) return 0 ; return 1 ; }