|
//Date: 2003.02.10 //Author: AOU //File: cArray03.cpp /* Added the following member functions: bool areDistinct(void); bool searchSeq(int x); void testSearchSeq(void); void testAreDistinct(void); */ /////////////////////////////////////////////////////////// // include files /////////////////////////////////////////////////////////// #include <iostream.h> #include <stdlib.h> /////////////////////////////////////////////////////////// // constants /////////////////////////////////////////////////////////// const int MAX_COUNT = 10; //maximum size of array const int MAX_VALUE = 5; void testSearchSeq(void); void testAreDistinct(void); /////////////////////////////////////////////////////////// // class cArray /////////////////////////////////////////////////////////// class cArray { private: int a[MAX_COUNT]; int n; public: cArray(void); cArray(int m); void display(void); void displayMultiple(int c); void populate(void); bool areDistinct(void); bool searchSeq(int x); }; /////////////////////////////////////////////////////////// // void main(void) /////////////////////////////////////////////////////////// void main(void) { testSearchSeq(); testAreDistinct(); } void testAreDistinct(void) { cout << "testAreDistinct\n"; cout << "===============\n"; for (int p=1; p <= 10; p++) { cout << "Case #" << p << endl; int n = rand()%(MAX_COUNT+1); cArray myArray(n); myArray.display(); if (myArray.areDistinct()) cout << "Array has distinct values\n"; else cout << "Array does not have distinct values\n"; cout << "--------------------------------------\n"; } } bool cArray::areDistinct(void) { if (n <= 1) return true; else { for (int i=1; i<=n-1; i++) for (int j=0; j<=i-1; j++) if (a[i] == a[j]) return false; return true; } } void testSearchSeq(void) { cout << "testSearchSeq\n"; cout << "=============\n"; for (int p=1; p <= 10; p++) { cout << "Case #" << p << endl; int n = rand()%(MAX_COUNT+1); cArray myArray(n); myArray.display(); int x; x = rand()%(MAX_VALUE+1); if (myArray.searchSeq(x)) cout << x << " is in the Array\n"; else cout << x << " is NOT in the Array\n"; cout << "--------------------------------------\n"; } } bool cArray::searchSeq(int x) { for (int i=0; i<=n-1; i++) if (x == a[i]) return true; return false; } cArray::cArray(int m) { n = m; for (int i=0; i<=n-1; i++) a[i] = rand()%(MAX_VALUE+1); } void cArray::populate(void) { n = rand()%(MAX_COUNT+1); for (int i=0; i<=n-1; i++) a[i] = rand()%(MAX_VALUE+1); } /////////////////////////////////////////////////////////// // void cArray::displayMultiple(int c) /////////////////////////////////////////////////////////// void cArray::displayMultiple(int c) { for (int i=1; i<=c; i++) display(); }; /////////////////////////////////////////////////////////// // void display(int a[], int n) /////////////////////////////////////////////////////////// void cArray::display(void) { cout << "a[" << n << "]="; for (int i=0; i<=n-1; i++) cout << a[i] << ' '; cout << endl; } /////////////////////////////////////////////////////////// // cArray::cArray(void) /////////////////////////////////////////////////////////// cArray::cArray(void) { n=0; cout << "Default constructor for cArray called\n"; } /////////////////////////////////////////////////////////// // SAMPLE RUN /////////////////////////////////////////////////////////// /* testSearchSeq ============= Case #1 a[8]=5 4 4 5 4 0 0 4 2 is NOT in the Array -------------------------------------- Case #2 a[7]=5 1 3 1 5 1 2 3 is in the Array -------------------------------------- Case #3 a[2]=3 0 2 is NOT in the Array -------------------------------------- Case #4 a[10]=4 4 3 2 2 5 5 0 5 0 3 is in the Array -------------------------------------- Case #5 a[2]=5 1 1 is in the Array -------------------------------------- Case #6 a[5]=5 3 2 3 3 2 is in the Array -------------------------------------- Case #7 a[5]=1 5 4 5 2 4 is in the Array -------------------------------------- Case #8 a[10]=3 1 5 3 1 4 4 5 2 0 0 is in the Array -------------------------------------- Case #9 a[10]=4 2 4 4 2 3 2 3 4 2 0 is NOT in the Array -------------------------------------- Case #10 a[4]=3 2 3 5 0 is NOT in the Array -------------------------------------- testAreDistinct =============== Case #1 a[0]= Array has distinct values -------------------------------------- Case #2 a[1]=2 Array has distinct values -------------------------------------- Case #3 a[5]=2 5 4 0 3 Array has distinct values -------------------------------------- Case #4 a[8]=1 5 4 2 0 3 5 3 Array does not have distinct values -------------------------------------- Case #5 a[7]=1 0 0 0 3 2 5 Array does not have distinct values -------------------------------------- Case #6 a[1]=5 Array has distinct values -------------------------------------- Case #7 a[2]=2 4 Array has distinct values -------------------------------------- Case #8 a[0]= Array has distinct values -------------------------------------- Case #9 a[10]=5 3 3 0 4 0 5 0 5 5 Array does not have distinct values -------------------------------------- Case #10 a[5]=4 5 4 2 4 Array does not have distinct values -------------------------------------- Press any key to continue */ |