|
//file: COOL012.cpp //Author: AOU //Date 09/29/2004 //////////////////////////////////////////////////////// // Assignment 03, Date Assigned 9/27/2004, Due: 10/06/2004 //////////////////////////////////////////////////////// /* As discussed in the class, implement the following member function and corresponding test functions: COOL operator +(const COOL &s2) const; // s3=s1+s2 void test_operatorPlus(void); s3=s1+s2 s1 and s2 may have duplicate values s3 will not have any duplicate values Submit the printed form of the following: (1) Detailed Description: (2) Two Example: (3) Detailed Algorithm: (4) Code: (5) Output from 20 Test Cases: Sample output: */ //////////////////////////////////////////////////////// //////////////////////////////////////////////////////// //////////////////////////////////////////////////////// // includes //////////////////////////////////////////////////////// #include <iostream.h> #include <math.h> #include <stdlib.h> #include <time.h> //////////////////////////////////////////////////////// // constants //////////////////////////////////////////////////////// int const MAX_SIZE = 20+2; int const INFINITY = 9999; int const TEST_COUNT = 20; int const MAX_VALUE = 10; /* Description: Example: Algorithm: */ //Problem: // List of values (duplicates allowed) kept in order // with several operations: // initialize, insert, delete, modify, search, ... //Example: // {}, {50}, {25,50}, {25,36,50}, {25,36,50,76}, // {25,36,50,76} //Algorithms: //////////////////////////////////////////////////////// // class COOL //////////////////////////////////////////////////////// class COOL { private: int a[MAX_SIZE]; int n; void swap(int &v1, int &v2) { int t = v1; v1 = v2; v2 = t; }; public: COOL(void); bool insert(int x); void display(void) const; bool isSorted(void)const; COOL(int m); int search(int x) const; void search(int x, int &p, int &c) const; void shuffle(void); void sortBubble1(void); bool deleteByPos(int p); int deleteByValue(int v, int c); void sortBubble2(void); friend bool isEqualTo(const COOL &firstList, const COOL &secondList); bool isEqualTo(const COOL &secondList) const; bool operator ==(const COOL &secondList) const; bool operator !=(const COOL &secondList) const; COOL(const COOL &aList); friend COOL combine(const COOL &firstList, const COOL &secondList); bool has(int x) const { if (search(x) == -1) return false; else return true; }; int deleteDupes(void); COOL operator +(const COOL &s2) const; // s3=s1+s2 }; //////////////////////////////////////////////////////// // test function prototypes //////////////////////////////////////////////////////// void test_initialize(void); void test_constructor(void); void test_search(void); void test_search2(void); void test_shuffle(void); void test_sortBubble1(void); void test_sortBubble2(void); void test_deleteByPos(void); void test_deleteByValue(void); void test_isEqualToFriend(void); void test_isEqualTo(void); void test_isEqualToOperator(void); void test_isNotEqualToOperator(void); void test_constructorCopy(void); void test_combine(void); void test_deleteDupes(void); void test_operatorPlus(void); void test_pointers(void); void test_pointers2(void); //////////////////////////////////////////////////////// // main //////////////////////////////////////////////////////// void main(void) { //srand(time(NULL)); //test_initialize(); //test_constructor(); //test_search(); //test_search2(); //test_shuffle(); //test_sortBubble1(); //test_sortBubble2(); //test_deleteByPos(); //test_deleteByValue(); //test_isEqualToFriend(); //test_isEqualTo(); //test_isEqualToOperator(); //test_isNotEqualToOperator(); //test_constructorCopy(); //test_combine(); //test_deleteDupes(); //test_operatorPlus(); //test_pointers(); test_pointers2(); } //////////////////////////////////////////////////////// // void test_pointers2(void) //////////////////////////////////////////////////////// void test_pointers2(void) { COOL aList(5); COOL *p = &aList; p->display(); cout << "p = " << p << endl; cout << "-------------------\n"; COOL bList(7); p = &bList; p->display(); cout << "p = " << p << endl; cout << "-------------------\n"; COOL aLists[5]; for (int i=0; i<=4; i++) { COOL tList(rand()%MAX_SIZE); aLists[i] = tList; aLists[i].display(); } cout << "-------------------\n"; p = &aLists[0]; cout << "p = " << p << endl; p->display(); p++; cout << "p = " << p << endl; p->display(); } //////////////////////////////////////////////////////// // void test_pointers(void) //////////////////////////////////////////////////////// void test_pointers(void) { COOL aList(5); COOL *p1; int *p2 = NULL; cout << "sizeof(aList) = " << sizeof(aList) << endl; cout << "sizeof(COOL) = " << sizeof(COOL) << endl; cout << "sizeof(int) = " << sizeof(int) << endl; cout << "sizeof(p1) = " << sizeof(p1) << endl; cout << "sizeof(p2) = " << sizeof(p2) << endl; aList.display(); p1 = &aList; p1->display(); p1 = new COOL(7); p1->display(); delete p1; p1->display(); } //////////////////////////////////////////////////////// // COOL COOL::operator +(const COOL &s2) const //////////////////////////////////////////////////////// COOL COOL::operator +(const COOL &s2) const { COOL temp; // return temp; } //////////////////////////////////////////////////////// // void test_operatorPlus(void) //////////////////////////////////////////////////////// void test_operatorPlus(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_operatorPlus\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE/2; COOL s1(m); m = rand()%MAX_SIZE/2; COOL s2(m); m = rand()%MAX_SIZE/2; COOL s3(m); cout << "s1 = "; s1.display(); cout << "s2 = "; s2.display(); cout << "s3 = "; s3.display(); s3 = s1 + s2; cout << "After s3 = s1 + s2;\n"; cout << "s1 = "; s1.display(); cout << "s2 = "; s2.display(); cout << "s3 = "; s3.display(); cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // int COOL::deleteDupes(void) //////////////////////////////////////////////////////// /* [1, 2, 2, 3, 3, 3, 4] =[1, 2, 3, 4 ] oldN = n create a temp list tList for each value x in THE list if x is not in tList then add x to tList end for THE list = tList deleted = oldN-n return deleted */ int COOL::deleteDupes(void) { int oldN = this->n; COOL tList; for (int i=1; i<=this->n-2; i++) if (!tList.has(a[i])) tList.insert(a[i]); int deleted = oldN-tList.n; *this = tList; return deleted; } //////////////////////////////////////////////////////// // void test_deleteDupes(void) //////////////////////////////////////////////////////// void test_deleteDupes(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_deleteDupes\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); cout << "myList = "; myList.display(); m = myList.deleteDupes(); cout << "After m = myList.deleteDupes();\n"; cout << "m= " << m << endl; cout << "myList = "; myList.display(); cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // COOL combine(const COOL &aList, const COOL &bList) //////////////////////////////////////////////////////// /* create a temp list copy aList to temp list insert all the elements from bList to temp list */ COOL combine(const COOL &aList, const COOL &bList) { COOL tList(aList); for (int i=1; i<=bList.n-2; i++) tList.insert(bList.a[i]); return tList; } //////////////////////////////////////////////////////// // void test_combine(void) //////////////////////////////////////////////////////// void test_combine(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_combine\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%(MAX_SIZE/2); COOL myList(m); cout << "myList = "; myList.display(); m = rand()%(MAX_SIZE/2); COOL yourList(m); cout << "yourList = "; yourList.display(); COOL hisList; hisList = combine(myList, yourList); cout << "hisList = combine(myList, yourList);\n"; cout << "myList = "; myList.display(); cout << "yourList = "; yourList.display(); cout << "hisList = "; hisList.display(); cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // COOL::COOL(COOL &aList) //////////////////////////////////////////////////////// COOL::COOL(const COOL &aList) { cout << "Copy constructor was called \n"; this->n = aList.n; for (int i=0; i<=MAX_SIZE-1; i++) this->a[i] = aList.a[i]; } //////////////////////////////////////////////////////// // void test_constructorCopy(void) //////////////////////////////////////////////////////// void test_constructorCopy(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_constructorCopy\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); cout << "myList = "; myList.display(); COOL yourList(myList); cout << "After COOL yourList(myList);\n"; cout << "myList = "; myList.display(); cout << "yourList = "; yourList.display(); //if (isEqualTo(myList, yourList)) //if (myList.isEqualTo(yourList)) //if (myList == yourList) if (myList.operator !=(yourList)) cout << "Lists are NOT equal\n"; else cout << "Lists are equal\n"; COOL hisList; hisList = myList = yourList; cout << "After hisList = myList = yourList;\n"; cout << "myList = "; myList.display(); cout << "yourList = "; yourList.display(); cout << "hisList = "; hisList.display(); cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // bool COOL::operator !=(COOL &secondList) //////////////////////////////////////////////////////// bool COOL::operator !=(const COOL &secondList) const { return !(secondList == *this); /* if ((0==n) && (0==secondList.n)) return false; else if (n != secondList.n) return true; else for (int i=0; i<=n-1; i++) if (a[i] != secondList.a[i]) return true; return false; */ } //////////////////////////////////////////////////////// // void test_isNotEqualToOperator(void) //////////////////////////////////////////////////////// void test_isNotEqualToOperator(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_isNotEqualToOperator\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); m = rand()%MAX_SIZE; COOL yourList(m); yourList.display(); //if (isEqualTo(myList, yourList)) //if (myList.isEqualTo(yourList)) //if (myList == yourList) if (myList.operator !=(yourList)) cout << "Lists are NOT equal\n"; else cout << "Lists are equal\n"; cout << "After myList = yourList;\n"; myList = yourList; myList.display(); yourList.display(); //if (isEqualTo(myList, yourList)) //if (myList.isEqualTo(yourList)) if (myList != yourList) cout << "Lists are NOT equal\n"; else cout << "Lists are equal\n"; cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // bool COOL::operator ==(const COOL secondList) const //////////////////////////////////////////////////////// bool COOL::operator ==(const COOL &secondList) const { //if ((0==n) && (0==secondList.n)) //if ((0==(*this).n) && (0==secondList.n)) if ((0==this->n) && (0==secondList.n)) return true; else if (n != secondList.n) return false; else for (int i=0; i<=n-1; i++) if (a[i] != secondList.a[i]) return false; return true; } //////////////////////////////////////////////////////// // void test_isEqualToOperator(void) //////////////////////////////////////////////////////// void test_isEqualToOperator(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_isEqualToOperator\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); m = rand()%MAX_SIZE; COOL yourList(m); yourList.display(); //if (isEqualTo(myList, yourList)) //if (myList.isEqualTo(yourList)) //if (myList == yourList) if (myList.operator ==(yourList)) cout << "Lists are equal\n"; else cout << "Lists are NOT equal\n"; cout << "After myList = yourList;\n"; myList = yourList; myList.display(); yourList.display(); //if (isEqualTo(myList, yourList)) //if (myList.isEqualTo(yourList)) if (myList == yourList) cout << "Lists are equal\n"; else cout << "Lists are NOT equal\n"; cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // bool COOL::isEqualTo(COOL secondList) //////////////////////////////////////////////////////// bool COOL::isEqualTo(const COOL &secondList) const { if ((0==n) && (0==secondList.n)) return true; else if (n != secondList.n) return false; else for (int i=0; i<=n-1; i++) if (a[i] != secondList.a[i]) return false; return true; } //////////////////////////////////////////////////////// // void test_isEqualTo(void) //////////////////////////////////////////////////////// void test_isEqualTo(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_isEqualTo\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); m = rand()%MAX_SIZE; COOL yourList(m); yourList.display(); //if (isEqualTo(myList, yourList)) if (myList.isEqualTo(yourList)) cout << "Lists are equal\n"; else cout << "Lists are NOT equal\n"; cout << "After myList = yourList;\n"; myList = yourList; myList.display(); yourList.display(); //if (isEqualTo(myList, yourList)) //if (myList == yourList) if (myList.isEqualTo(yourList)) cout << "Lists are equal\n"; else cout << "Lists are NOT equal\n"; cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // bool isEqualTo(COOL firstList, COOL secondList) //////////////////////////////////////////////////////// /* Description: Example: Algorithm: if both lists are empty then return true else if their sizes are different then return false else for i=0 to n-1 if firstList.a[i] <> secondList.a[i] then return false end for end if return true */ bool isEqualTo(const COOL &firstList, const COOL &secondList) { if ((0==firstList.n) && (0==secondList.n)) return true; else if (firstList.n != secondList.n) return false; else for (int i=0; i<=firstList.n-1; i++) if (firstList.a[i] != secondList.a[i]) return false; return true; } //////////////////////////////////////////////////////// // void test_isEqualToFriend(void) //////////////////////////////////////////////////////// void test_isEqualToFriend(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_isEqualToFriend\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); m = rand()%MAX_SIZE; COOL yourList(m); yourList.display(); if (isEqualTo(myList, yourList)) cout << "Lists are equal\n"; else cout << "Lists are NOT equal\n"; cout << "After myList = yourList;\n"; myList = yourList; myList.display(); yourList.display(); if (isEqualTo(myList, yourList)) //if (myList == yourList) cout << "Lists are equal\n"; else cout << "Lists are NOT equal\n"; cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // bool COOL::deleteByPos(int p) //////////////////////////////////////////////////////// /* Description: Example: [-inf,20, 30, 40, +inf], 0 =>nothing [-inf,20, 30, 40, +inf], 1 =>[-inf, 30, 40, +inf] [-inf,20, 30, 40, +inf], 3 =>[-inf,20, 30, +inf] Algorithm: n=2, p<0, p=0, p=n-1 =>nothing p=1..n-2 shift values to left starting at p+1 */ bool COOL::deleteByPos(int p) { //supply the code that works return true; } //////////////////////////////////////////////////////// // void test_deleteByPos(void) //////////////////////////////////////////////////////// void test_deleteByPos(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_deleteByPos\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); int p = rand()%(MAX_SIZE+1); cout << "Trying to delete value at position " << p << endl; if (myList.deleteByPos(p)) cout << "deleteByPos was successful\n"; else cout << "deleteByPos was NOT successful\n"; myList.display(); cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // int COOL::deleteByValue(int v, int c) //////////////////////////////////////////////////////// /* Description: Example: [-inf,20,30,40,40,+inf], 20,5 =>[-inf,30,40,40,+inf], 1 [-inf,20,30,40,40,+inf], 40,5 =>[-inf,20,30,+inf], 2 [-inf,20,30,40,40,+inf], 40,1 =>[-inf,20,30,40,+inf], 1 Algorithm: */ int COOL::deleteByValue(int v, int c) { //supply the code that works return 0; } //////////////////////////////////////////////////////// // void test_deleteByValue(void) //////////////////////////////////////////////////////// void test_deleteByValue(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_deleteByValue\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); int v = rand()%(MAX_VALUE+1); int c = rand()%MAX_SIZE; cout << "Trying to delete " << c << " copies of value " << v << endl; int deleted = myList.deleteByValue(v, c); if (deleted > 0) { cout << "deleteByValue was successful\n"; cout << "Deleted " << deleted << "copies of value " << v << endl; } else cout << "deleteByValue was NOT successful\n"; myList.display(); cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // void COOL::sortBubble2(void); //////////////////////////////////////////////////////// /* do the following sorted = true check the list left to right if a bad pair found swap, set sorted to false while not sorted */ void COOL::sortBubble2(void) { bool sorted; do { sorted = true; for (int j=0; j<=n-1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); sorted = false; } } } while (!sorted); } //////////////////////////////////////////////////////// // void test_sortBubble2(void) //////////////////////////////////////////////////////// void test_sortBubble2(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_sortBubble2\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); myList.shuffle(); cout << "After shuffle\n"; myList.display(); myList.sortBubble2(); cout << "After sortBubble2\n"; myList.display(); if (myList.isSorted()) cout << "List is sorted\n"; else cout << "List is NOT sorted\n"; cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // void COOL::sortBubble1(void); //////////////////////////////////////////////////////// /* -99,1, 2, 3, 4, 5,+99 for i = 1 to n-1 for j=0 to n-2 if a[i] > a[i+1] then swap a[i], a[i+1] end for end for */ void COOL::sortBubble1(void) { for (int i = 1; i<=n-1; i++) { for (int j=0; j<=n-2; j++) { if (a[j] > a[j+1]) swap(a[j], a[j+1]); } } } //////////////////////////////////////////////////////// // void test_sortBubble1(void) //////////////////////////////////////////////////////// void test_sortBubble1(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_sortBubble1\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); myList.shuffle(); cout << "After shuffle\n"; myList.display(); myList.sortBubble1(); cout << "After sortBubble1\n"; myList.display(); if (myList.isSorted()) cout << "List is sorted\n"; else cout << "List is NOT sorted\n"; cout << "------------------------\n"; } } //////////////////////////////////////////////////////// // void COOL::shuffle(void) //////////////////////////////////////////////////////// /* Description: shuffles/scramble/unsort/re-organize/shake Example: [-99, 12, 24, 24 99]=>[-99, 24, 12, 24 99] Algorithm: randomly pick two values and swap delete a value randomly while building another sequentially split the list into two sublists and merge them randomly randomly pick a value and swap it with the first value do the following n*n p = random value between 1 and n-2 swap a[1] with a[p] */ void COOL::shuffle(void) { if (n<=3) return; for (int i=1; i<=n*n; i++) { int p = 1 + rand()%(n-2); swap(a[1],a[p]); } } //////////////////////////////////////////////////////// // void test_shuffle(void) //////////////////////////////////////////////////////// void test_shuffle(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_shuffle\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); myList.shuffle(); cout << "After shuffle\n"; myList.display(); cout << endl; } } //////////////////////////////////////////////////////// // void COOL::search(int x, int &p, int &c) //////////////////////////////////////////////////////// /* Description: searches for copies of x in the list, in p returns the position, in c returns the number of copies of x starting at p. Example: [-99, 12, 24, 24, 24, 45, 50, 60, 75, 99], 24 => 2, 3 => at positions 2, 3, 4 Algorithm: */ void COOL::search(int x, int &p, int &c) const { /* Code to be replaced p=1; c=3; */ for (int i=1; i<=n-2; i++) { if (x == a[i]) { p=i; c=1; for (int j=i+1; j<=n-2; j++) { if (x==a[j]) c++; else return; } return; } if (x < a[i]) break; } c=0; p=-1; } //////////////////////////////////////////////////////// // void test_search2(void) //////////////////////////////////////////////////////// void test_search2(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_search2\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); int x = rand()%(MAX_VALUE +1); cout << "Searching for " << x << endl; int p, c; myList.search(x, p, c); cout << "Found " << c << " copies of " << x << " starting at position " << p << endl; cout << "In other words, " << x << " was found at positions "; for (int j=p; j<=p+c-1; j++) cout << j << ' '; cout << endl << endl; } } //////////////////////////////////////////////////////// // int COOL::search(int x) //////////////////////////////////////////////////////// /* Description: searches for the first value x in the list and returns the position if found else returns -1 Example: [-99, 12, 24, 45, 50, 60, 75, 99], 24 => 2 [-99, 12, 24, 45, 50, 60, 75, 99], 25 => -1 Algorithm: for i=1 to n-2 if x = a[i] then return i if x < a[i] then exit loop end for return -1 */ int COOL::search(int x) const { for (int i=1; i<=n-2; i++) { if (x == a[i]) return i; if (x < a[i]) break; } return -1; } //////////////////////////////////////////////////////// // void test_search(void) //////////////////////////////////////////////////////// void test_search(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_search\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); myList.display(); int x = rand()%(MAX_VALUE +1); cout << "Searching for " << x << endl; cout << "Found at " << myList.search(x) << endl; } } //////////////////////////////////////////////////////// // COOL::COOL(int m) //////////////////////////////////////////////////////// //create a list with m values COOL::COOL(int m) { n = 2; a[0] = -INFINITY; a[1] = +INFINITY; for (int i=2; i<=MAX_SIZE-1; i++) a[i] = 0; for (i=1; i<=m; i++) { int x = rand()%(MAX_VALUE+1); insert(x); } } //////////////////////////////////////////////////////// // void test_constructor(void) //////////////////////////////////////////////////////// void test_constructor(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_constructor\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; COOL myList(m); cout << "After COOL myList(" << m << ");\n"; myList.display();// display(myArray, m); } } //////////////////////////////////////////////////////// // bool COOL::isSorted(void) //////////////////////////////////////////////////////// // bool isSorted(int a[], int n) /* Description: checks if the list is in order Example: Algorithm: */ bool COOL::isSorted(void) const { for (int i=0; i<=n-2; i++) if (a[i] > a[i+1]) return false; return true; } //////////////////////////////////////////////////////// // void test_initialize(void) //////////////////////////////////////////////////////// void test_initialize(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_initialize\n"; cout << "+++++++++++++++++++++++++++++++\n"; COOL myList; for (int i=1; i<=TEST_COUNT/TEST_COUNT; i++) { //myList.initialize(); //initialize(myArray, m); myList.display();// display(myArray, m); int j, x; for (j=1; j<=TEST_COUNT; j++) { x = rand()%(MAX_VALUE+1); myList.insert(x); //insert(myArray, m, x); myList.display(); //display(myArray, m); if (myList.isSorted()) //(isSorted(myArray, m)) cout << "List is in order\n"; else cout << "List is NOT in order\n"; } } } //////////////////////////////////////////////////////// // COOL::COOL(void) //////////////////////////////////////////////////////// /* Description: Initializes the list of values by setting n=0, and a[0..MAX_SIZE-1] = 0; Algorithm: n = 2 a[0] = -INFINITY for i=1 to MAX_SIZE-2 a[i] = 0; a[MAX_SIZE-1] = +INFINITY */ COOL::COOL(void) { n = 2; a[0] = -INFINITY; a[1] = +INFINITY; for (int i=2; i<=MAX_SIZE-1; i++) a[i] = 0; } //////////////////////////////////////////////////////// // bool COOL::insert(int x) //////////////////////////////////////////////////////// /* Description: inserts x into the list and maintains the order Example: {}, {50}, {25,50}, {25,36,50}, {25,36,50,76}, {25,36,50,76} Algorithm0: Case0: list is full Case1: x goes to empty list x Case3: x > all the values in the list x Case2: x < all the values in the list Case4: x goes between two values and handle duplicates Algorithm1: m = n s1: if (x >= a[m-1]) then a[m] = x n++ exit function else a[m] = a[m-1] m-- goto s1 Algorithm2: m = n while x < a[m-1]) a[m] = a[m-1] m-- wend a[m] = x n++ */ bool COOL::insert(int x) { if (n >= MAX_SIZE) return false; int m = n; while (x < a[m-1]) { a[m] = a[m-1]; m--; } a[m] = x; n++; return true; } //////////////////////////////////////////////////////// // void COOL::display(void) //////////////////////////////////////////////////////// /* Description: Algorithm: */ void COOL::display(void) const { cout << "COOL[" << n-2 << "] = ["; for (int i=0; i<=n-1; i++) cout << a[i] << ' '; cout << "]\n"; } |