p014
Home ] Up ]

 

//file  : p014.cpp
//author: AOU
//date  : 07/22/2004

/*
Project #11 07/22/2004
    CArray operator -(const CArray &a2) const;
    friend bool isEmpty(const CArray ta);

Project #10 07/21/2004
    bool operator <= (const CArray &a2) const;
    CArray operator ||(const CArray &a2) const;
    CArray operator &&(const CArray &a2) const;

Project #9 07/20/2004
    CArray operator +(const CArray &a2) const;
    friend ostream & operator << (ostream &bob , const CArray &a)

Project #8 07/19/2004
    bool deleteAtPos(int index);
    int purgeDupes(void);

Project #7 07/15/2004
    bool isEqualTo(const CArray &a2) const;
    bool operator ==(const CArray &a2) const;
    bool operator !=(const CArray &a2) const;
  
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 = 20;


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);
void test_isEqualTo_member(void);
void test_isEqualTo_operator(void);
void test_isNotEqualTo_operator(void);
void test_deleteAtPos(void);
void test_purgeDupes(void);
void test_operator_plus(void);

void test_operator_subset(void);
void test_operator_intersection(void);
void test_operator_union(void);

void test_operator_minus(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);
    bool isEqualTo(const CArray &a2) const;
    bool operator ==(const CArray &a2) const;
    bool operator !=(const CArray &a2) const;
    bool deleteAtPos(int index);
    int purgeDupes(void);
    CArray operator +(const CArray &a2) const;
    friend ostream & operator << (ostream &bob , const CArray &a);
    bool operator <= (const CArray &a2) const;
    CArray operator ||(const CArray &a2) const;
    CArray operator &&(const CArray &a2) const;
  
    friend bool isEmpty(const CArray ta);
    CArray operator -(const CArray &a2) const;
  };


void main(void)
  {
  //srand(time(NULL));
  //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();
  //test_isEqualTo_member();
  //test_isEqualTo_operator();
  //test_isNotEqualTo_operator();
  //test_deleteAtPos();
  //test_purgeDupes();
  //test_operator_plus();

  //test_operator_subset();
  //test_operator_intersection();
  //test_operator_union();

  test_operator_minus();
  //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();
  test_isEqualTo_member();
  test_isEqualTo_operator();
  test_isNotEqualTo_operator();
  test_deleteAtPos();
  test_purgeDupes();
  test_operator_plus();
  test_operator_subset();
  test_operator_intersection();
  test_operator_union();

  test_operator_minus();
  }


/////////////////////////////////////////////////
// CArray CArray::operator -(const CArray &a2) const
/////////////////////////////////////////////////
/*
  for each x in *this
    if (x is not in a2) and (x is not in ta)
      add x to ta
*/
CArray CArray::operator -(const CArray &a2) const
  {
  CArray ta;
  for (int i=0; i<=this->n-1; i++)
    {
    int x = this->a[i];
    if (!a2.has(x) && !ta.has(x))
      ta.a[ta.n++] = x;
    }
  return ta;
  }


/////////////////////////////////////////////////
// void test_operator_minus(void)
/////////////////////////////////////////////////
void test_operator_minus(void)
  {
  /*
  create a1(M/2), a2(M/2), a3(M/2)
  display a1, a2, a3
  a1 = a2 - a3;
  display some message
  display a1, a2, a3
  check if a1 has distinct values
  check if a1 is a subset of a2
  check if a1&&a3 is empty

  */
  cout << "+++++++++++++++++++\n";
  cout << "test_operator_minus\n";
  cout << "+++++++++++++++++++\n";

  for (int i=1; i<=TEST_COUNT; i++)
    {
    CArray a1('r'), a2('r'), a3('r');

    cout << "a1 = " << a1;
    cout << "a2 = " << a2;
    cout << "a3 = " << a3;

    a1 = a2 - a3;
    
    cout << "After a1 = a2 - a3;\n";
    
    cout << "a1 = " << a1;
    cout << "a2 = " << a2;
    cout << "a3 = " << a3;

    if (a1.hasDistinctValues())
      cout << "a1 has distinct values\n";
    else
      cout << "a1 NHAS distinct values\n";

    if (a1 <= a2)
      cout << "a1 is a subset of a2\n";
    else
      cout << "a1 is NOT a subset of a2\n";

    if (isEmpty(a1 && a3))
      cout << "a1 && a3 is empty\n";
    else
      cout << "a1 && a3 is NOT empty\n";

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// bool isEmpty(const CArray ta)
/////////////////////////////////////////////////
bool isEmpty(const CArray ta)
  {
  return (0==ta.n);
  }


/////////////////////////////////////////////////
// CArray CArray::operator ||(const CArray &a2) const
/////////////////////////////////////////////////
/*
Algorithm:
create empty ta 
for each x in *this do the following
  if ta does not have x then add it to ta

for each x in a2 do the following
  if ta does not have x then add it to ta

*/
CArray CArray::operator ||(const CArray &a2) const
  {
  CArray ta;
  int x, i;

  for (i=0; i<=this->n-1; i++)
    {
    x = this->a[i];
    if (!ta.has(x))
      ta.a[ta.n++] = x;
    }
 
  for (i=0; i<=a2.n-1; i++)
    {
    x = a2.a[i];
    if (!ta.has(x))
      ta.a[ta.n++] = x;
    }

  return ta;
  }


/////////////////////////////////////////////////
// void test_operator_union(void)
/////////////////////////////////////////////////
void test_operator_union(void)
  {
  /*
  create a1(M/2), a2(M/2), a3(M/2)
  display a1, a2, a3
  a1 = a2 || a3;
  display some message
  display a1, a2, a3
  check if a1 has distinct values
  */
  cout << "+++++++++++++++++++\n";
  cout << "test_operator_union\n";
  cout << "+++++++++++++++++++\n";

  for (int i=1; i<=TEST_COUNT; i++)
    {
    CArray a1('r'), a2('r'), a3('r');

    cout << "a1 = " << a1;
    cout << "a2 = " << a2;
    cout << "a3 = " << a3;

    a1 = a2 || a3;
    
    cout << "After a1 = a2 || a3;\n";
    
    cout << "a1 = " << a1;
    cout << "a2 = " << a2;
    cout << "a3 = " << a3;

    if (a1.hasDistinctValues())
      cout << "a1 has distinct values\n";
    else
      cout << "a1 NHAS distinct values\n";

    if (a2 <= a1)
      cout << "a2 is a subset of a1\n";
    else
      cout << "a2 is NOT a subset of a1\n";

    if (a3 <= a1)
      cout << "a3 is a subset of a1\n";
    else
      cout << "a3 is NOT a subset of a1\n";

    cout << "----------------\n";
    }
  }



/////////////////////////////////////////////////
// CArray CArray::operator &&(const CArray &a2) const
/////////////////////////////////////////////////
/*
Algorithm:
create empty ta
for each x in *this do the following
  if a2 has x and ta nas x then add x to ta
*/
CArray CArray::operator &&(const CArray &a2) const
  {
  CArray ta;
  int x;
  
  for (int i=0; i<=this->n-1; i++)
    {
    x = this->a[i];
    if (a2.has(x) && !ta.has(x))
      ta.a[ta.n++] = x;
    }

  return ta;
  }


/////////////////////////////////////////////////
// void test_operator_intersection(void)
/////////////////////////////////////////////////
  /*
  create a1(r), a2(r), a3(r)
  display a1, a2, a3
  a1 = a2 && a3;
  display some message
  display a1, a2, a3
  check if a1 has distinct values
  visually check if a1<=a2
  visually check if a1<=a3
  */
void test_operator_intersection(void)
  {
  cout << "++++++++++++++++++++++++++\n";
  cout << "test_operator_intersection\n";
  cout << "++++++++++++++++++++++++++\n";

  for (int i=1; i<=TEST_COUNT; i++)
    {
    CArray a1('r'), a2('r'), a3('r');

    cout << "a1 = " << a1;
    cout << "a2 = " << a2;
    cout << "a3 = " << a3;

    a1 = a2 && a3;
    
    cout << "After a1 = a2 && a3;\n";
    
    cout << "a1 = " << a1;
    cout << "a2 = " << a2;
    cout << "a3 = " << a3;

    if (a1.hasDistinctValues())
      cout << "a1 has distinct values\n";
    else
      cout << "a1 NHAS distinct values\n";

    if (a1 <= a2)
      cout << "a1 is a subset of a2\n";
    else
      cout << "a1 is NOT a subset of a2\n";

    if (a1 <= a3)
      cout << "a1 is a subset of a3\n";
    else
      cout << "a1 is NOT a subset of a3\n";

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// bool CArray::operator <= (const CArray &a2) const
/////////////////////////////////////////////////
/*
check if all the values in *this are in a2
for each x in *this do the following
  if a2 nhas x then return false

return true
*/
bool CArray::operator <= (const CArray &a2) const
  {
  for (int i=0; i<=this->n-1; i++)
    if (!a2.has(this->a[i]))
      return false;

  return true;
  }


/////////////////////////////////////////////////
// void test_operator_subset(void)
/////////////////////////////////////////////////
void test_operator_subset(void)
  {
  cout << "++++++++++++++++++++\n";
  cout << "test_operator_subset\n";
  cout << "++++++++++++++++++++\n";

  for (int i=1; i<=TEST_COUNT; i++)
    {
    CArray a1('r'), a2('r');

    cout << "a1 = " << a1;
    cout << "a2 = " << a2;

    if (a1 <= a2)
      cout << "a1 is a subset of a2\n";
    else
      cout << "a1 is NOT a subset of a2\n";

    if (a2 <= a1)
      cout << "a2 is a subset of a1\n";
    else
      cout << "a2 is NOT a subset of a1\n";

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// ostream & operator << (ostream &bob , const CArray &a)
/////////////////////////////////////////////////
ostream & operator << (ostream &bob , const CArray &a)
  {
  bob << "a[" << a.n << "] = ";
  for (int i=0; i<=a.n-1; i++)
    bob << a.a[i] << ' ';

  bob << endl;

  return bob;
  }


/////////////////////////////////////////////////
// CArray CArray::operator +(const CArray &a2) const
/////////////////////////////////////////////////
CArray CArray::operator +(const CArray &a2) const
  {
  //copy create *this elements into ta
  CArray ta(*this);

  //add elements of a2 to ta

  for (int i=0; i<=a2.n-1; i++)
    {
    ta.a[ta.n] = a2.a[i];
    ta.n++;
    }

  return ta;
  }


/////////////////////////////////////////////////
// void test_operator_plus(void)
/////////////////////////////////////////////////
void test_operator_plus(void)
  {
  cout << "++++++++++++++++++\n";
  cout << "test_operator_plus\n";
  cout << "++++++++++++++++++\n";

  for (int i=1; i<=TEST_COUNT; i++)
    {
    CArray a1('r'), a2(ARRAY_SIZE/2), a3(ARRAY_SIZE/2);

    cout << "a1 = " << a1;
    cout << "a2 = " << a2;
    cout << "a3 = " << a3;

    a1 = a2 + a3;

    cout << "After a1 = a2 + a3;\n";

    cout << "a1 = " << a1;
    cout << "a2 = " << a2;
    cout << "a3 = " << a3;

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// int CArray::purgeDupes(void)
/////////////////////////////////////////////////
/*
if n <2 then return 0
left=0
while (left <=n-2)
  {
  right = left+1
  while(right<=n-1)
    {
    if a[left]=a[right] then
      shoot a[right] or deleteAtPos(right)
    right++
    }

  left++
  }
*/
int CArray::purgeDupes(void)
  {
  if (n < 2) return 0;
  int deleted=0, left=0, right;

  while (left <=n-2)
    {
    right = left+1;
    while(right<=n-1)
      if (a[left]==a[right])
        deleteAtPos(right), deleted++;
      else
        right++;

    left++;
    }
  return deleted;
  }


/////////////////////////////////////////////////
// void test_purgeDupes(void)
/////////////////////////////////////////////////
void test_purgeDupes(void)
  {
  cout << "+++++++++++++++\n";
  cout << "test_purgeDupes\n";
  cout << "+++++++++++++++\n";

  for (int i=1; i<=TEST_COUNT; i++)
    {
    CArray a1('r');
    a1.display();
    a1.purgeDupes();
    cout << "After a1.purgeDupes();\n";
    a1.display();

    if(a1.hasDistinctValues())
      cout << "List has distinct values\n";
    else
      cout << "List does NOT have distinct values\n";

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// bool CArray::deleteAtPos(int index)
/////////////////////////////////////////////////
/*
Examples index=3, list=[n=6,{6,3,6,2,9,8}]
  =>list=[n=5,{6,3,6,9,8}]

Algorithm:
if index <0 or >=n return false
  else if n=1 
    n=0, return true {index = 0 at this point}
  else
    for i=index to n-2
      a[i] = a{i+1]
    n--
    return true
*/
bool CArray::deleteAtPos(int index)
  {
  if (index <0 || index>=n) 
      return false;
    else if (1==n) 
      {
      n=0;
      return true;// {index = 0 at this point}
      }
    else
      {
      for (int i=index; i<=n-2; i++)
        a[i] = a[i+1];
      n--;
      return true;
      }
  }


/////////////////////////////////////////////////
// void test_deleteAtPos(void)
/////////////////////////////////////////////////
void test_deleteAtPos(void)
  {
  cout << "++++++++++++++++\n";
  cout << "test_deleteAtPos\n";
  cout << "++++++++++++++++\n";

  for (int i=1; i<=TEST_COUNT; i++)
    {
    CArray a1('r');
    a1.display();

    int index = rand()%ARRAY_SIZE - 2;
    cout << "Deleting value at " << index << endl;

    if(a1.deleteAtPos(index))
      cout << "Deletion was a success\n";
    else
      cout << "Deletion was NOT a success\n";
    a1.display();

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// bool CArray::operator !=(const CArray &a2) const
/////////////////////////////////////////////////
bool CArray::operator !=(const CArray &a2) const
  {
  return !(*this == a2);
  //return !(::isEqualTo(*this, a2));
  //return !(this->isEqualTo(a2));
  /*
  if (*this == a2)
    return false;
  else
    return true;
  */
  /*
  if (this->n != a2.n)
    return true;

  for (int i=0; i<=this->n-1; i++)
    if (this->a[i] != a2.a[i])
      return true;

  return false;
  */
  }


/////////////////////////////////////////////////
// test_isNotEqualTo_operator
/////////////////////////////////////////////////
void test_isNotEqualTo_operator(void)
  {
  cout << "++++++++++++++++++++++++++\n";
  cout << "test_isNotEqualTo_operator\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 (a1 != a2)
      cout << "a1 is NOT equal to a2\n";
    else
      cout << "a1 is equal to a2\n";

    if (a1 != a1)
      cout << "a1 is NOT equal to a1\n";
    else
      cout << "a1 is equal to a1\n";

    a1 = a2; //??? assigning a2 to a1
    cout << "After a1 = a2;\n";
    cout << "a1="; a1.display();
    cout << "a2="; a2.display();

    if (a1 != a2)
      cout << "a1 is NOT equal to a2\n";
    else
      cout << "a1 is equal to a2\n";

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// bool CArray::operator ==(const CArray &a2) const
/////////////////////////////////////////////////
bool CArray::operator ==(const CArray &a2) const
  {
  if (this->n != a2.n)
    return false;

  for (int i=0; i<=this->n-1; i++)
    if (this->a[i] != a2.a[i])
      return false;

  return true;
  }


/////////////////////////////////////////////////
// test_isEqualTo_operator
/////////////////////////////////////////////////
void test_isEqualTo_operator(void)
  {
  cout << "+++++++++++++++++++++++\n";
  cout << "test_isEqualTo_operator\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 (a1 == a2)
    //if (a1.operator ==(a2))
      cout << "a1 is equal to a2\n";
    else
      cout << "a1 is NOT equal to a2\n";

    if (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 (a1 == a2)
      cout << "a1 is equal to a2\n";
    else
      cout << "a1 is NOT equal to a2\n";

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// bool CArray::isEqualTo(const CArray &a2) const
/////////////////////////////////////////////////
bool CArray::isEqualTo(const CArray &a2) const
  {
  if (this->n != a2.n)
    return false;

  for (int i=0; i<=this->n-1; i++)
    if (this->a[i] != a2.a[i])
      return false;

  return true;
  }


/////////////////////////////////////////////////
// test_isEqualTo_member
/////////////////////////////////////////////////
void test_isEqualTo_member(void)
  {
  cout << "+++++++++++++++++++++\n";
  cout << "test_isEqualTo_member\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 (a1.isEqualTo(a2))
      cout << "a1 is equal to a2\n";
    else
      cout << "a1 is NOT equal to a2\n";

    if (a1.isEqualTo(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 (a1.isEqualTo(a2))
      cout << "a1 is equal to a2\n";
    else
      cout << "a1 is NOT equal to a2\n";

    cout << "----------------\n";
    }
  }


/////////////////////////////////////////////////
// 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)
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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";
    }
  }


/////////////////////////////////////////////////
// void CArray::shuffle(void)
/////////////////////////////////////////////////
/*
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)
/////////////////////////////////////////////////
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
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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
/////////////////////////////////////////////////
void CArray::display(void) const
  {
  cout << "a[" << n << "] = ";

  for (int i=0; i<=n-1; i++)
    cout << a[i] << ' ';

  cout << endl;
  }


/////////////////////////////////////////////////
// void CArray::populate(void)
/////////////////////////////////////////////////
void CArray::populate(void)
  {
  n = rand()%(ARRAY_SIZE+1);

  for (int i=0; i<=n-1; i++)
    a[i] = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1);
  }


/////////////////////////////////////////////////
// void CArray::sortBubble(void)
/////////////////////////////////////////////////
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
/////////////////////////////////////////////////
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
/////////////////////////////////////////////////
void CArray::display(int m) const
  {
  while (m-- > 0)
    {
    //display();
    //CArray::display();
    //(*this).display();
    this->display();
    }
  }


/////////////////////////////////////////////////
// CArray::CArray(int n)
/////////////////////////////////////////////////
CArray::CArray(int n)
  {
  //cout << "Constructor CArray(int n) was called\n";
  if (n > ARRAY_SIZE)
    n = ARRAY_SIZE;

  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)
/////////////////////////////////////////////////
CArray::CArray(char ch)
  {
  //cout << "Constructor CArray(char ch) was called\n";
  if ('r' == ch || 'R' == ch)
    this->populate();
  else if ('s' == ch || 'S' == ch)
    {
    this->populate();
    this->sortBubble();
    }
  else if ('d' == ch || 'D' == ch)
    {
    n = 0;
    int times = rand()%(ARRAY_SIZE+1);
    for (int i=1; i<=times; i++)
      {
      int x = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1);
      if (!this->has(x))
        a[n++] = x;
      }
    }
  else
    n = 0;
  }


/////////////////////////////////////////////////
// void test_constructor_r(void)
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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)
/////////////////////////////////////////////////
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*2);
    CArray a1(n);
    cout << "After CArray a1(" << n << ");\n";
    a1.display();
    cout << "----------------\n";
    }
  }