c++ problems updated!

I think there is something wrong with my constructor. Can someone please help??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef INTEGER_SET_H
#define INTEGER_SET_H

class IntegerSet
{
private:
  int A[101];
  int validEntry(int x) const
  {
    return (x >= 0 && x <= 100);
  }
public:
  IntegerSet();
  IntegerSet(int[], int);
  IntegerSet unionOfSets(const IntegerSet&);
  IntegerSet intersectionOfSets(IntegerSet&);
  void inputSet();
  void insertElement(int);
  void deleteElement(int);
  void printSet() const;
  bool isEqualTo(IntegerSet);
}; 

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <iomanip>
#include "IntegerSet.h"
using namespace std;

IntegerSet::IntegerSet()
{
  
}

IntegerSet::IntegerSet(int array[], int size)
{
  for (int i = 0; i < size; i++)
    insertElement(array[i]);
}

void IntegerSet::inputSet()
{
  int number;
  do
  {
    cout << "Enter an element{-1 to end}: ";
    cin >> number;
    if (validEntry(number))
      A[number] = 1;
    else if (number != -1)
      cout << "Invalid Element\n";
  } 
  while (number != -1);
  cout << "Entry Complete\n";
}

void IntegerSet::printSet() const
{
  int x = 1;
  bool empty = true;
  cout << '{';
  for (int u = 0; u < 101; u++) {
    if (A[u]) {
      cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
      empty = false;
      x++;
    }
  }
  
  if (empty)
    cout << setw(4) << "---";
  cout << setw(4) << "}" << "\n";
}

IntegerSet IntegerSet::unionOfSets(const IntegerSet &r)
{
  IntegerSet temp;
  for (int i = 0; i < 101; i++)
    if (A[i] == 1 || r.A[i] == 1) 
      temp.A[i] = 1;
  return temp;
}

IntegerSet IntegerSet::intersectionOfSets(IntegerSet &r)
{
  IntegerSet temp;
  for (int i = 0; i < 101; i++)
    if (A[i] == 1 && r.A[i] == 1)
      temp.A[i] = 1;
  return temp;
}

void IntegerSet::insertElement(int k)
{
  if (validEntry(k))
    A[k] = 1;
  else
    cout << "Invalid insert attempted!\n";
}

void IntegerSet::deleteElement(int m)
{
  if (validEntry(m))
    A[m] = 0;
  else
    cout << "Invalid insert attempted!\n";
}

bool IntegerSet::isEqualTo(IntegerSet r)
{
  for (int i = 0; i < 101; i++)
    if (A[i] != r.A[i]) 
      return false;
    return true;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#include <iostream> 
#include "IntegerSet.h" // IntegerSet class definition
using namespace std;

int main()
{
   IntegerSet a;
   IntegerSet b;
   IntegerSet c;
   IntegerSet d;

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();
   c = a.unionOfSets( b );
   d = a.intersectionOfSets( b );
   cout << "\nUnion of A and B is:\n";
   c.printSet();
   cout << "Intersection of A and B is:\n";
   d.printSet();

   if ( a.isEqualTo( b ) )
      cout << "Set A is equal to set B\n";
   else
      cout << "Set A is not equal to set B\n";

   cout << "\nInserting 77 into set A...\n";
   a.insertElement( 77 );
   cout << "Set A is now:\n";
   a.printSet();

   cout << "\nDeleting 77 from set A...\n";
   a.deleteElement( 77 );
   cout << "Set A is now:\n";
   a.printSet();

   const int arraySize = 10;
   int intArray[ arraySize ] = { 25, 67, 2, 9, 99, 105, 45, -5, 100, 1 };
   IntegerSet e( intArray, arraySize );

   cout << "\nSet E is:\n";
   e.printSet();

   cout << endl;

   system("PAUSE");
   return 0;
} // end main 


My output it totally wrong... please help :(
Last edited on
please help~~~~~~~~~!
You need to set the A's elements to 0 in the constructors. Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
IntegerSet::IntegerSet()
{
  clear();
}

IntegerSet::IntegerSet(int arr[], int size)
{
  clear();
  for (int i = 0; i < size; i++)
    insertElement(arr[i]);
}

void IntegerSet::clear()
{
  for (int i = 0; i < 101; i++)
      A[i] = 0;
}

Topic archived. No new replies allowed.