|
//Date: 2003.02.12 //Author: AOU //File: cArray04.cpp /* Added the following member functions: CArray(char ch); void testConstructorR(void); added m_ to the data member names changed class name cArray to CArray */ /////////////////////////////////////////////////////////// // include files /////////////////////////////////////////////////////////// #include <iostream.h> #include <stdlib.h> /////////////////////////////////////////////////////////// // constants /////////////////////////////////////////////////////////// const int MAX_COUNT = 10; //maximum size of array const int MAX_VALUE = 5; /////////////////////////////////////////////////////////// // prototypes /////////////////////////////////////////////////////////// void testSearchSeq(void); void testAreDistinct(void); void testConstructorR(void); /////////////////////////////////////////////////////////// // class CArray /////////////////////////////////////////////////////////// class CArray { private: int m_a[MAX_COUNT]; int m_n; public: CArray(void); CArray(int m); void display(void) const; void displayMultiple(int c) const; void populate(void); bool areDistinct(void) const; bool searchSeq(int x) const; CArray(char ch); }; /////////////////////////////////////////////////////////// // void main(void) /////////////////////////////////////////////////////////// void main(void) { // testSearchSeq(); // testAreDistinct(); testConstructorR(); } /////////////////////////////////////////////////////////// // void testConstructorR(void) /////////////////////////////////////////////////////////// void testConstructorR(void) { cout << "testConstructorR\n"; cout << "================\n"; for (int i=1; i<=10; i++) { cout << "Case #" << i << endl; CArray array1('r'); array1.display(); } } /////////////////////////////////////////////////////////// // CArray::CArray(char ch) /////////////////////////////////////////////////////////// /* A constructor fills array with values depending on the value of parameter ch. If ch is equal to 'r' then it fills the array with random values which are also random in count. CArray array1('r'); gives: a[4,1,2], n=3 CArray array2('r'); gives: a[4,1,2,3,5,3], n=6 Algorithm: n = random integer bet 0 and MAX_COUNT do the following for i=0 to n-1 a[i] = random integer between 0 and MAX_VALUE */ CArray::CArray(char ch) { if (('r'==ch) || ('R'==ch)) { m_n = rand()%(MAX_COUNT+1); for (int i=0; i<=m_n-1; i++) m_a[i] = rand()%(MAX_VALUE+1); } else m_n = 0; } /////////////////////////////////////////////////////////// // void testAreDistinct(void) /////////////////////////////////////////////////////////// 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) const /////////////////////////////////////////////////////////// bool CArray::areDistinct(void) const { if (m_n <= 1) return true; else { for (int i=1; i<=m_n-1; i++) for (int j=0; j<=i-1; j++) if (m_a[i] == m_a[j]) return false; return true; } } /////////////////////////////////////////////////////////// // void testSearchSeq(void) /////////////////////////////////////////////////////////// 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) const /////////////////////////////////////////////////////////// bool CArray::searchSeq(int x) const { for (int i=0; i<=m_n-1; i++) if (x == m_a[i]) return true; return false; } /////////////////////////////////////////////////////////// // CArray::CArray(int m) /////////////////////////////////////////////////////////// CArray::CArray(int m) { m_n = m; for (int i=0; i<=m_n-1; i++) m_a[i] = rand()%(MAX_VALUE+1); } /////////////////////////////////////////////////////////// // void CArray::populate(void) /////////////////////////////////////////////////////////// void CArray::populate(void) { m_n = rand()%(MAX_COUNT+1); for (int i=0; i<=m_n-1; i++) m_a[i] = rand()%(MAX_VALUE+1); } /////////////////////////////////////////////////////////// // void CArray::displayMultiple(int c) /////////////////////////////////////////////////////////// void CArray::displayMultiple(int c) const { for (int i=1; i<=c; i++) display(); }; /////////////////////////////////////////////////////////// // void CArray::display(void) const /////////////////////////////////////////////////////////// void CArray::display(void) const { cout << "array[" << m_n << "]="; for (int i=0; i<=m_n-1; i++) cout << m_a[i] << ' '; cout << endl; } /////////////////////////////////////////////////////////// // CArray::CArray(void) /////////////////////////////////////////////////////////// CArray::CArray(void) { m_n=0; cout << "Default constructor for CArray called\n"; } /////////////////////////////////////////////////////////// // SAMPLE RUN /////////////////////////////////////////////////////////// /* testConstructorR ================ Case #1 array[8]=5 4 4 5 4 0 0 4 Case #2 array[0]= Case #3 array[7]=5 1 3 1 5 1 2 Case #4 array[9]=0 3 0 2 3 4 4 3 2 Case #5 array[6]=5 5 0 5 0 3 Case #6 array[2]=5 1 Case #7 array[7]=0 5 3 2 3 3 2 Case #8 array[5]=1 5 4 5 2 Case #9 array[3]=3 3 1 Case #10 array[0]= Press any key to continue */ |