//file: COOL005.cpp
//Author: AOU
//Date 09/13/2004
////////////////////////////////////////////////////////
// Assignment 02, Date Assigned 9/13/2004, Due: 9/20/2004
////////////////////////////////////////////////////////
/*
As discussed in the class, implement the following member
functions and corresponding test functions:
bool deleteByPos(int p);
int deleteByValue(int v, int c);
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);
};
////////////////////////////////////////////////////////
// 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);
////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////
void main(void)
{
srand(time(NULL));
//test_initialize();
//test_constructor();
//test_search();
//test_search2();
//test_shuffle();
//test_sortBubble1();
test_sortBubble2();
}
////////////////////////////////////////////////////////
// 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;
}
////////////////////////////////////////////////////////
// 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";
}
|