//file : p008.cpp
//author: AOU
//date : 07/14/2004
/*
Project #6 07/14/2004
friend bool isEqualTo(const CArray &a1, const CArray &a2);
CArray(const CArray &second);
and identify contstant functions
Project #5 07/13/2004
void shuffle(void);
friend void shuffle(CArray &a1);
Project #4 07/12/2004
bool has(int x);
bool hasDistinctValues(void);
*/
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
const int ARRAY_SIZE = 10;
const int MIN_VALUE = 0;
const int MAX_VALUE = 6;
const int TEST_COUNT = 10;
void testAll(void);
void test_populate(void);
void test_sortBubble(void);
void test_isSorted(void);
void test_constructor_n(void);
void test_constructor_r(void);
void test_constructor_s(void);
void test_constructor_d(void);
void test_has(void);
void test_hasDistinctValues(void);
void test_shuffle(void);
void test_shuffle_friend(void);
void test_isEqualTo_friend(void);
void test_constructor_copy(void);
class CArray
{
private:
int a[ARRAY_SIZE];
int n;
public:
CArray(void);
void populate(void);
void display(void) const;
void sortBubble(void);
bool isSorted(void) const;
void display(int m) const;
CArray(int n);
CArray(char ch);
bool has(int x) const; // myArray.has(x)
bool hasDistinctValues(void) const;
void shuffle(void);
friend void shuffle(CArray &a1);
friend bool isEqualTo(const CArray &a1, const CArray &a2);
CArray(const CArray &second);
};
void main(void)
{
//test_populate();
//test_sortBubble();
//test_isSorted();
//test_constructor_n();
//test_constructor_r();
//test_constructor_s();
//test_constructor_d();
//test_has();
//test_hasDistinctValues();
//test_shuffle();
//test_shuffle_friend();
test_isEqualTo_friend();
//test_constructor_copy();
//testAll();
}
void testAll(void)
{
test_populate();
test_sortBubble();
test_isSorted();
test_constructor_n();
test_constructor_r();
test_constructor_s();
test_constructor_d();
test_has();
test_hasDistinctValues();
test_shuffle();
test_shuffle_friend();
test_isEqualTo_friend();
test_constructor_copy();
}
/////////////////////////////////////////////////
// CArray::CArray(const CArray &second)
/////////////////////////////////////////////////
CArray::CArray(const CArray &second)
{
cout << "Copy constructor CArray(CArray &second) was called\n";
this->n = second.n;
for (int i=0; i<=second.n-1; i++)
this->a[i] = second.a[i];
}
/////////////////////////////////////////////////
// test_constructor_copy
/////////////////////////////////////////////////
void test_constructor_copy(void)
{
cout << "+++++++++++++++++++++\n";
cout << "test_constructor_copy\n";
cout << "+++++++++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('r');
cout << "a1="; a1.display();
CArray a2(a1);
cout << "a2="; a2.display();
cout << "a1="; a1.display();
cout << "----------------\n";
}
}
/////////////////////////////////////////////////
// bool isEqualTo(const CArray &a1, const CArray &a2)
/////////////////////////////////////////////////
bool isEqualTo(const CArray &a1, const CArray &a2)
{
//a1.n =a2.n = 0; protected by const gun
if (a1.n != a2.n)
return false;
for (int i=0; i<=a1.n-1; i++)
if (a1.a[i] != a2.a[i])
return false;
return true;
}
/////////////////////////////////////////////////
// test_isEqualTo_friend
/////////////////////////////////////////////////
void test_isEqualTo_friend(void)
{
cout << "+++++++++++++++++++++\n";
cout << "test_isEqualTo_friend\n";
cout << "+++++++++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('r'), a2('r');
cout << "a1="; a1.display();
cout << "a2="; a2.display();
if (isEqualTo(a1, a2))
cout << "a1 is equal to a2\n";
else
cout << "a1 is NOT equal to a2\n";
if (isEqualTo(a1, a1))
cout << "a1 is equal to a1\n";
else
cout << "a1 is NOT equal to a1\n";
a1 = a2; //??? assigning a2 to a1
cout << "After a1 = a2;\n";
cout << "a1="; a1.display();
cout << "a2="; a2.display();
if (isEqualTo(a1, a2))
cout << "a1 is equal to a2\n";
else
cout << "a1 is NOT equal to a2\n";
cout << "----------------\n";
}
}
void shuffle(CArray &a1)
{
for (int i=1; i<=a1.n*a1.n*a1.n; i++)
{
int k47 = rand()%a1.n;
int temp = a1.a[0];
a1.a[0] = a1.a[k47];
a1.a[k47] = temp;
}
}
void test_shuffle_friend(void)
{
cout << "+++++++++++++++++++\n";
cout << "test_shuffle_friend\n";
cout << "+++++++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('s');
a1.display();
shuffle(a1);
cout << "After shuffle\n";
a1.display();
cout << "----------------\n";
}
}
/*
do the following n*n times
swap a random value with the first value
*/
void CArray::shuffle(void)
{
for (int i=1; i<=n*n*n; i++)
{
int k47 = rand()%n;
int temp = a[0];
a[0] = a[k47];
a[k47] = temp;
}
}
void test_shuffle(void)
{
cout << "++++++++++++\n";
cout << "test_shuffle\n";
cout << "++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('s');
a1.display();
a1.shuffle();
cout << "After shuffle\n";
a1.display();
cout << "----------------\n";
}
}
bool CArray::hasDistinctValues(void) const
{
if (n<2)
return true;
int left, right;
for (left = 0; left<=n-2; left++)
for (right=left+1; right<=n-1; right++)
if(a[left] == a[right])
return false;
return true;
}
void test_hasDistinctValues(void)
{
cout << "++++++++++++++++++++++\n";
cout << "test_hasDistinctValues\n";
cout << "++++++++++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('r');
a1.display();
if (a1.hasDistinctValues())
cout << "list has distinct values\n";
else
cout << "list does NOT have distinct values\n";
CArray a2('d');
a2.display();
if (a2.hasDistinctValues())
cout << "list has distinct values\n";
else
cout << "list does NOT have distinct values\n";
cout << "----------------\n";
}
}
bool CArray::has(int x) const
// myArray.has(x)
// possibilities
// n=0 and n>0
// n=0 =>false
// n>0 =>false or true
/*
Algorithm:
if n is 0 then return false
else
if x matches with any element in a[] return true
else return false
*/
{
if (0==n)
return false;
else
{
for (int j=0; j<=n-1; j++)
if (x==a[j])
return true;
return false;
}
}
void test_has(void)
{
cout << "++++++++\n";
cout << "test_has\n";
cout << "++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('r');
a1.display();
int x = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1);
cout << "Searching for " << x << endl;
if (a1.has(x))
cout << x << " is in the list\n";
else
cout << x << " is in NOT the list\n";
cout << "----------------\n";
}
}
CArray::CArray(void)
{
cout << "Constructor CArray(void) was called\n";
n = 0;
for (int i=0; i<ARRAY_SIZE; i++)
a[i] = 0;
}
void CArray::display(void) const
{
cout << "a[" << n << "] = ";
for (int i=0; i<n; i++)
cout << a[i] << ' ';
cout << endl;
}
void CArray::populate(void)
{
n = rand()%(ARRAY_SIZE+1);
for (int i=0; i<n; i++)
a[i] = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1);
}
void CArray::sortBubble(void)
{
int swapCount;
do
{
swapCount = 0;
for (int i=0; i<=n-2; i++)
{
if (a[i] > a[i+1])
{
int temp = a[i];
a[i]=a[i+1];
a[i+1] = temp;
swapCount++;
}
}
}
while (swapCount>0);
}
bool CArray::isSorted(void) const
{
for (int i=0; i<=n-2; i++)
if (a[i] > a[i+1])
return false;
return true;
}
void CArray::display(int m) const
{
while (m-- > 0)
{
//display();
//CArray::display();
//(*this).display();
this->display();
}
}
CArray::CArray(int n)
{
cout << "Constructor CArray(int n) was called\n";
this->n = n;
//CArray::n = n;
//(*this).n = n;
for (int i=0; i<n; i++)
a[i] = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1);
}
CArray::CArray(char ch)
{
cout << "Constructor CArray(char ch) was called\n";
if ('r' == ch)
this->populate();
else if ('s' == ch)
{
this->populate();
this->sortBubble();
}
else if ('d' == ch)
{
/*
do the following random number of times
x = random value
if x is not in then add it to the collection
*/
n = 0;
int times = rand()%ARRAY_SIZE*2;
for (int i=1; i<=times; i++)
{
int x = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1);
if (!this->has(x))
a[n++] = x;
}
/*
if (0 == n)
{
a[0] = x;
n = 1;
}
else
{
int j;
bool found = false;
for (j=0; j<=n-1; j++)
if (a[j] == x)
{
found = true;
break;
}
if (!found)
{
a[n] = x;
n++;
}
}
*/
}
}
void test_constructor_r(void)
{
cout << "++++++++++++++++++\n";
cout << "test_constructor_r\n";
cout << "++++++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('r');
cout << "After CArray a1('r');\n";
a1.display();
cout << "----------------\n";
}
}
void test_constructor_s(void)
{
cout << "++++++++++++++++++\n";
cout << "test_constructor_s\n";
cout << "++++++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('s');
cout << "After CArray a1('s');\n";
a1.display();
cout << "----------------\n";
}
}
void test_constructor_d(void)
{
cout << "++++++++++++++++++\n";
cout << "test_constructor_d\n";
cout << "++++++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1('d');
cout << "After CArray a1('d');\n";
a1.display();
cout << "----------------\n";
}
}
void test_populate(void)
{
cout << "+++++++++++++\n";
cout << "test_populate\n";
cout << "+++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1;
a1.display();
a1.populate();
a1.display();
cout << "----------------\n";
}
}
void test_sortBubble(void)
{
cout << "+++++++++++++++\n";
cout << "test_sortBubble\n";
cout << "+++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1;
a1.populate();
a1.display();
a1.sortBubble();
cout << "After sortBubble()\n";
a1.display();
cout << "----------------\n";
}
}
void test_isSorted(void)
{
cout << "+++++++++++++\n";
cout << "test_isSorted\n";
cout << "+++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
CArray a1;
a1.populate();
a1.display();
a1.sortBubble();
cout << "After sortBubble()\n";
a1.display();
if (a1.isSorted())
cout << "SORT successful\n";
else
cout << "SORT NOT successful\n";
cout << "----------------\n";
}
}
void test_constructor_n(void)
{
cout << "++++++++++++++++++\n";
cout << "test_constructor_n\n";
cout << "++++++++++++++++++\n";
for (int i=1; i<=TEST_COUNT; i++)
{
int n = rand()%(ARRAY_SIZE+1);
CArray a1(n);
cout << "After CArray a1(" << n << ");\n";
a1.display();
cout << "----------------\n";
}
}
|