[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

NEED HELP!

Need help with the missing function definitions.  Please advise.  Del

#ifndef INT_H
#define INT_H

class IntegerSet {

public:

   IntegerSet(int);
   IntegerSet(const IntegerSet &);

   IntegerSet unionOfIntegerSets(const IntegerSet &);
   IntegerSet intersectionOfIntegerSets(const IntegerSet &);

   void emptySet();
   void inputSet();
   void insertElement(int);
   void deleteElement(int);
   void setPrint() const;
   bool isEqualTo(const IntegerSet &) const;

private:

   int *set;
   int size;

   bool validEntry(int x) const
   {
     return x >= 0 && x < size;

   }

};

#endif

// int.cpp

#include <iostream>
#include <iomanip>

using namespace std;

#include "int.h"

IntegerSet::IntegerSet(int s)
{
   size = s;
   set = new int[size];
   /*Need help in writing call to emptySet */ HELP!

}

IntegerSet::IntegerSet(const IntegerSet &init)
{
   size = init.size;
   /*Need help in writing statement to allocate sufficient memory*/ HELP!

   emptySet();

   for(int i = 0; i < size; i++)
      /*Need help in writing statement to copy elements of init*/ HELP!

}

void IntegerSet::emptySet()
{
   /*Need help in writing function definition for emptySet*/ HELP!

}

void IntegerSet::inputSet()
{
   int number;

   do {
     cout << "Enter an element (-1 to end): ";
     cin >> number;

     if(validEntry(number))
       set[number] = 1;

       else if(number != -1)
       cout << "Invalid Element\n";

   } while(number != -1);
     cout << "Entry complete\n";

}

void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;

   cout << '{';

   for(int u = 0; u < size; ++u)
      if(set[u]) {
     cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
     empty = false;
     ++x;

      }

   if(empty)
     cout << setw(4) << "---";
     cout << setw(4) << "}" << '\n';

}

IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r)
{
   IntegerSet temp(size > r.size ? size : r.size);
   temp.emptySet();

   int iterations = (size < r.size ? size : r.size);

   for(int i = 0; i < iterations; i++)
      if(set[i] == 1 || r.set[i] == 1)
     temp.set[i] = 1;

   return temp;

}

IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
{
   /*Need help in writing definition for intersectionOfIntegerSets*/ HELP!

}

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

}

void IntegerSet::deleteElement()
{
   /*NEED DEFINITION WRITTEN FOR deleteElement()*/ HELP!

}

void IntegerSet::isEqualTo()
{
   /*NEED DEFINITION WRITTEN FOR isEqualTo()*/ HELP!

}

// int_driver.cpp

#include <iostream>

using namespace std;

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

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   /*NEED WRITTEN CALL TO unionOfIntegerSets FOR OBJECT a PASSING IT b;
     ASSIGN THE RESULT TO d*/ HELP!!

   /*NEED WRITTEN CALL TO intersectionOfIntegerSets FOR OBJECT a PASSING
     IT b; ASSIGN THE RESULT TO d*/ HELP!!

   cout << "\nUnion Of A and B is:\n";
   c.setPrint();
   cout << "Intersection of A and B is:\n";
   d.setPrint();

   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.setPrint();

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

   cout << endl;

   return 0;

}
0
edelossantos
Asked:
edelossantos
  • 14
  • 13
  • 9
6 Solutions
 
Sys_ProgCommented:
1) What is emptyset() supposed to do???

2)
IntegerSet::IntegerSet(const IntegerSet &init)
{
   size = init.size;
   set = new int [size] ;

   emptySet();

   for(int i = 0; i < size; i++)
        set[i] = init.set [i] ;

}



 
0
 
Sys_ProgCommented:
edelossantos,

I really don't understand your set class,
Where r u storing the set elements
U r storing every element as -1
What do u intend to do

Amit
0
 
edelossantosAuthor Commented:
emptySet() needs to copy the elements of init.  It's the for loop after emptySet() rite after IntegerSet::IntegerSet  Incidentally you've got this one.  Del
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
edelossantosAuthor Commented:
IntegerSet::IntegerSet(const IntegerSet &init)
{
   size = init.size;
   /*Need help in writing statement to allocate sufficient memory*/ HELP!

   emptySet();

   for(int i = 0; i < size; i++)
      *** Write statement to copy elements of init***

}
0
 
Sys_ProgCommented:
edelossantos,

Could u elaborate the purpose of ur class

Amit
0
 
edelossantosAuthor Commented:
Create a class IntegerSet.  Each object of class IntegerSet can hold integers int the range 0 through 100.  A set is represented internally as an array of ones and zeros.  Array element a[i] is 1 if integer i is in the set.  Array element a[j] is 0 integer if integer j is not in the set.  The default constructor intializes a set to the so called "empty set," i.e., a set whose array representation contains all zeros.

I need to provide a unionOfIntegerSets member function that creates a third set which is set-theoretic union of two existing sets(i.e., and element of the third set's array is set to 1 if that element is 1 in either or both of the existing sets, and an element of the third set's array is set to 0 if that element is 0 in each of the existing sets.)

I also need to provide an intersectionOfIntegerSets member function that creates a third set which is the set-theoretic union of two existing sets(i.e. an element of the third set's array is set to 0 if that element is 0 in either or both of the existing sets.)

Need to also provide insertElement member function that insers a new integer k into a set(by setting a[k] to 1).  Provide a delete Element member function that deletes integer m(by setting a[m] to 0.) Also need setPrint member function that prints a set as a list of numbers seperated by spaces.  Print only thos elements that are present in the set(i.e, their position in the array has a value of 1.) Print---for an empty set.

Provide an isEqualTo member function that determines if two sets are equal.  Provide an additional constructor to take five integer arguments which can be used to initialize a set object.  To provide fewer than five elements in the set, use default arguments of -1 for the others.

Need driver program to test my IntegerSet class.  Instantiate several IntegerSet objects.  Test that all member functions work properly.  Please advise.  Del
0
 
MercantilumCommented:
Here is the code you requested (just changed the parts you pointed out).
It is not tested though, but it should help to finalize.
I change a bit c and d in your main function.
The changes made are near a  //++

- - -

IntegerSet::IntegerSet(int s)
{
   emptySet (); //++
   size = s;
   set = new int[size];
}

IntegerSet::IntegerSet(const IntegerSet &init)
{
   emptySet(); //++

   size = init.size;
   set = new int[size]; //++

   for(int i = 0; i < size; i++)
      set[i] = init.set[i]; //++

}

void IntegerSet::emptySet()
{
    delete [] set;  //++
}

void IntegerSet::inputSet()
{
   int number;

   do {
     cout << "Enter an element (-1 to end): ";
     cin >> number;

     if(validEntry(number))
       set[number] = 1;

       else if(number != -1)
       cout << "Invalid Element\n";

   } while(number != -1);
     cout << "Entry complete\n";

}

void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;

   cout << '{';

   for(int u = 0; u < size; ++u)
      if(set[u]) {
     cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
     empty = false;
     ++x;

      }

   if(empty)
     cout << setw(4) << "---";
     cout << setw(4) << "}" << '\n';

}

IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r)
{
   IntegerSet temp(size > r.size ? size : r.size);
   temp.emptySet();

   int iterations = (size < r.size ? size : r.size);

   for(int i = 0; i < iterations; i++)
      if(set[i] == 1 || r.set[i] == 1)
     temp.set[i] = 1;

   return temp;

}

IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
{
   //++
   // will change 'set' to be intersection of current set and 's'
   // check for each item of set, if it is in s.
   // a duplicate set item will remain duplicated in set, [if is in s of course]
   int *newset = new int[size];
   int i,j,remain;
   for (remain=i=0 ; i<size ; i++)
   {
       for (j=0 ; j<s.size && set[i]!=s.set[j] ; j++) /* */;
       if (j < s.size) // it's in
           newset[remain++] = set[i];
   }
   // change set to newset
   set = newset;
   size = remain;
   // could be a bit cleaner by deleting set and reallocating the exact right nb of data after intersection
}

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

}

void IntegerSet::deleteElement(int k)
{
   //++
   // here we just "remove" the item by copying the above data on it, size is -1
   if(validEntry(k))
   {
     for (int i=k+1 ; i<size ; i++) set[i-1] = set[i];
     size--;
   }
   else
     cout << "Invalid delete attempted!\n";
}

void IntegerSet::isEqualTo(const IntegerSet &s)
{
   //++
   int equal = (size == s.size);

   if (equal)
   {
      for (int i=0 ; i<size && set[i]==s.set[i] ; i++) /* */;
      if (i < size) equal = 0;
   }

   return (equal);
}

// int_driver.cpp

#include <iostream>

using namespace std;

int main()
{
   IntegerSet a(101);
   IntegerSet b(101);
   IntegerSet *c;  //++
   IntegerSet *d;  //++

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   //++ union of (a,b) in c
    c = new IntegerSet(a);
    c.unionOfIntegerSets(b);

   //++ intersection of (a,b) in d
    d = new IntegerSet(a);
    d.intersectionOfIntegerSets(b);

   cout << "\nUnion Of A and B is:\n";
   c.setPrint();
   cout << "Intersection of A and B is:\n";
   d.setPrint();

   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.setPrint();

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

   cout << endl;

   return 0;

}
0
 
MercantilumCommented:
OOoops in  IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
I missed the delete ...  let's save some memory :)
Last lines are


   // change set to newset
   delete [] set;
   set = newset;
   size = remain;
   // could be a bit cleaner by deleting set and reallocating the exact right nb of data after intersection
}
0
 
Sys_ProgCommented:
Delete should be this way

void IntegerSet::deleteElement(int k)
{
   if(validEntry(k))  {
        set[i-1] = 0 ;
   }
   else
        cout << "Invalid delete attempted!\n";
}


Amit
0
 
MercantilumCommented:
Ooops again the deleteelement is simpler than I thought:

void IntegerSet::deleteElement(int k)
{
   //++
   if(validEntry(k)) set[k] = 0;
   else
     cout << "Invalid delete attempted!\n";
}
0
 
Sys_ProgCommented:
Have a look at this



#include <iostream>
#include <string>
#include <sstream>

using namespace std ;

class IntSet {
      private :
            int *set ;
            int size ;
        public :
               IntSet ( int s ) ;
               IntSet ( const IntSet &s ) ;
               void InsertElement ( int k ) ;
               void DeleteElement ( int k ) ;
               bool IsValideElement ( int k ) ;
               void SetUnion ( const IntSet &s ) ;
               void SetIntersect ( const IntSet &s ) ;            
}

IntSet::IntSet ( int s ) : size ( s ) {
      set = new int [s] ;
      memset ( set, 0, s ) ;
}

IntSet::IntSet ( const IntSet &s )  : size ( s.size ) {
      set = new int [size] ;
      for ( int i = 0 ; i < size ; i ++ )
            set [i] = s.set [i] ;
}

bool IntSet::IsValideElement ( int k ) {
      if ( k < 0 || k >= size )
            return false ;
      else
             return true ;      
}

void IntSet::InsertElement ( int k ) {
      if ( IsValideElement ( k ) ) {
            cout << "Out of Range" ;
      }
      else {
            set [k-1] = 1 ;
      }
}

void IntSet::DeleteElement ( int k ) {
      if ( IsValideElement ( k ) ) {
            cout << "Out of Range" ;
      }
      else {
            set [k-1] = 0 ;
      }
}

int main(int argc, char* argv[])
{
      char str[] = "17445.21" ;

      float val = atoi ( str ) ;
        cout << val ;
    system("pause");
      return 0;
}

Amit
0
 
Sys_ProgCommented:
Sorry, ignore the main() part

Amit
0
 
Sys_ProgCommented:
Code for Intersection

void IntSet::SetIntersect ( const IntSet &s ) {

      int maxSize ;

      if ( s.size > size )
            maxSize = s.size ;
      else
             maxSize = size ;

      for ( int i = maxSize ; i >= 0 ; i++ ) {
            if ( set[i] == 1 && s.set[i] == 1 ) {
                  maxSize       = i ;
                  break ;
            }
      }
      
        IntSet newSet ( maxSize ) ;
        for ( int i = 0 ; i < maxSize ; i ++ ) {
              if ( set[i] == 1 && s.set[i] == 1 ) {
                    newSet [i] = 1 ;
              }
        }
        
}


Amit
0
 
edelossantosAuthor Commented:
A million thanks to the both of you.  Del
0
 
edelossantosAuthor Commented:
Cannot figure out error.  Please help.  Del

#include <iostream>
#include <cstring>
#include <sstream>

using namespace std ;

class IntSet {
     private :
          int *set ;
          int size ;
       public :
             IntSet ( int s ) ;
             IntSet ( const IntSet &s ) ;
             void InsertElement ( int k ) ;
             void DeleteElement ( int k ) ;
             bool IsValideElement ( int k ) ;
             void SetUnion ( const IntSet &s ) ;
             void SetIntersect ( const IntSet &s ) ;          
};

IntSet::IntSet ( int s ) : size ( s ) {
     set = new int [s] ;
     memset ( set, 0, s ) ;
}

IntSet::IntSet ( const IntSet &s )  : size ( s.size ) {
     set = new int [size] ;
     for ( int i = 0 ; i < size ; i ++ )
          set [i] = s.set [i] ;
}

bool IntSet::IsValideElement ( int k ) {
     if ( k < 0 || k >= size )
          return false ;
     else
           return true ;    
}

void IntSet::InsertElement ( int k ) {
     if ( IsValideElement ( k ) ) {
          cout << "Out of Range" ;
     }
     else {
          set [k-1] = 1 ;
     }
}

void IntSet::SetIntersect(const IntSet &s) {

     int maxSize;

     if(s.size > size)
       maxSize = s.size;
     else
       maxSize = s.size;

     for(int i = maxSize; i >= 0; i++) {
      if(set[i] == 1 && s.set[i] == 1) {
        maxSize = i;
       break;
        }

     }

     IntSet newSet(maxSize,int &s);

     for(int i = 0; i < maxSize; i++) {
      if(set[i] == 1 && s.set[i] == 1) {
        newSet[i] = 1;
        }
     }

}

void IntSet::DeleteElement ( int k ) {
     if ( IsValideElement ( k ) ) {
          cout << "Out of Range" ;
     }
     else {
          set [k-1] = 0 ;
     }
}

int main(int argc, char* argv[])
{
     char str[] = "17445.21" ;

     float val = atoi ( str ) ;
       cout << val ;
    system("pause");
     return 0;
}

Output:

test.cpp: In method `void IntSet::SetIntersect(const IntSet &)':
test.cpp:65: parse error before `&'
test.cpp:69: ANSI C++ forbids using pointer to a function in arithmetic
test.cpp:69: assignment of read-only location
test.cpp:69: assignment to `IntSet ()(...)' from `int'


0
 
MercantilumCommented:
1. you miss a } just before the declaration of newset
2. you miss a 'int' for maxSize in newset declaration
0
 
MercantilumCommented:
3. void IntSet::newSet (int maxSize, IntSet s)  {
       instead of ...;
4. newSet is not in the class definition
5. welll. .................. what is newSet supposed to do ?  [      newSet[i] = 1;  !?!?!  ]
0
 
Sys_ProgCommented:
>>  IntSet newSet(maxSize,int &s);

This line should be
  IntSet newSet(maxSize);

Amit
0
 
edelossantosAuthor Commented:
Also need this code repaired.  Please advise.  Del

#ifndef INT_H
#define INT_H

class IntegerSet {

public:

   IntegerSet(int);
   IntegerSet(const IntegerSet &);

   IntegerSet unionOfIntegerSets(const IntegerSet &);
   IntegerSet intersectionOfIntegerSets(const IntegerSet &);

   void emptySet();
   void inputSet();
   void insertElement(int);
   void deleteElement(int);
   void setPrint() const;
   bool isEqualTo(const IntegerSet &) const;

private:

   int *set;
   int size;

   bool validEntry(int x) const
   {
      return x >= 0 && x < size;

   }

};

#endif
/
/ int_1.cpp

#include <iostream>
#include <iomanip>

using namespace std;

#include "int.h"

IntegerSet::IntegerSet(int s)
{
   emptySet (); //++
   size = s;
   set = new int[size];
}

IntegerSet::IntegerSet(const IntegerSet &init)
{
   emptySet(); //++

   size = init.size;
   set = new int[size]; //++

   for(int i = 0; i < size; i++)
      set[i] = init.set[i]; //++

}

void IntegerSet::emptySet()
{
    delete [] set;  //++
}

void IntegerSet::inputSet()
{
   int number;

   do {
     cout << "Enter an element (-1 to end): ";
     cin >> number;

     if(validEntry(number))
       set[number] = 1;

       else if(number != -1)
       cout << "Invalid Element\n";

   } while(number != -1);
     cout << "Entry complete\n";

}

void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;

   cout << '{';

   for(int u = 0; u < size; ++u)
      if(set[u]) {
     cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
     empty = false;
     ++x;

      }

   if(empty)
     cout << setw(4) << "---";
     cout << setw(4) << "}" << '\n';

}

IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r)
{
   IntegerSet temp(size > r.size ? size : r.size);
   temp.emptySet();

   int iterations = (size < r.size ? size : r.size);

   for(int i = 0; i < iterations; i++)
      if(set[i] == 1 || r.set[i] == 1)
     temp.set[i] = 1;

   return temp;

}

IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
{
   //++
   // will change 'set' to be intersection of current set and 's'
   // check for each item of set, if it is in s.
   // a duplicate set item will remain duplicated in set, [if is in s of course]
   int *newset = new int[size];
   int i,j,remain;
   for (remain=i=0 ; i<size ; i++)
   {
       for (j=0 ; j<s.size && set[i]!=s.set[j] ; j++) /* */;
       if (j < s.size) // it's in
           newset[remain++] = set[i];
   }
   // change set to newset
   set = newset;
   size = remain;
   // could be a bit cleaner by deleting set and reallocating the exact right nb of data after intersection
}

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

}

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

void IntegerSet::isEqualTo(const IntegerSet &s)
{
   //++
   int equal = (size == s.size);

   if (equal)
   {
      for (int i=0 ; i<size && set[i]==s.set[i] ; i++) /* */;
      if (i < size) equal = 0;
   }

   return (equal);
}

// int_1_driver.cpp

#include <iostream>

using namespace std;

#include "int.h"

int main()
{
   IntegerSet a(101);
   IntegerSet b(101);
   IntegerSet *c;  //++
   IntegerSet *d;  //++

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   //++ union of (a,b) in c
    c = new IntegerSet(a);
    c.unionOfIntegerSets(b);

   //++ intersection of (a,b) in d
    d = new IntegerSet(a);
    d.intersectionOfIntegerSets(b);

   cout << "\nUnion Of A and B is:\n";
   c.setPrint();
   cout << "Intersection of A and B is:\n";
   d.setPrint();

   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.setPrint();

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

   cout << endl;

   return 0;

}

Output:


int_1.cpp:127: prototype for `void IntegerSet::isEqualTo(const IntegerSet &)' do
es not match any in class `IntegerSet'
int.h:19: candidate is: bool IntegerSet::isEqualTo(const IntegerSet &) const
int_1.cpp: In method `void IntegerSet::isEqualTo(const IntegerSet &)':
int_1.cpp:134: name lookup of `i' changed for new ANSI `for' scoping
int_1.cpp:133:   using obsolete binding at `i'
int_1.cpp:137: `return' with a value, in function returning void
int_1_driver.cpp: In function `int main()':
int_1_driver.cpp:23: request for member `unionOfIntegerSets' in `c', which is of
 non-aggregate type `IntegerSet *'
int_1_driver.cpp:27: request for member `intersectionOfIntegerSets' in `d', whic
h is of non-aggregate type `IntegerSet *'
int_1_driver.cpp:30: request for member `setPrint' in `c', which is of non-aggre
gate type `IntegerSet *'
int_1_driver.cpp:32: request for member `setPrint' in `d', which is of non-aggre
gate type `IntegerSet *'


0
 
MercantilumCommented:
Mainly my mistakes :)

1. first the differences between your code and the corrected one  (listing is below)

125c125
< bool IntegerSet::isEqualTo(const IntegerSet &s) const
---
> void IntegerSet::isEqualTo(const IntegerSet &s)
128c128
<    int i,equal = (size == s.size);
---
>    int equal = (size == s.size);
132c132
<       for (i=0 ; i<size && set[i]==s.set[i] ; i++) /* */;
---
>       for (int i=0 ; i<size && set[i]==s.set[i] ; i++) /* */;
140a141
> #include <iostream>
141a143,145
> using namespace std;
>
> #include "int.h"
157c161
<     c->unionOfIntegerSets(b);
---
>     c.unionOfIntegerSets(b);
161c165
<     d->intersectionOfIntegerSets(b);
---
>     d.intersectionOfIntegerSets(b);
164c168
<    c->setPrint();
---
>    c.setPrint();
166c170
<    d->setPrint();
---
>    d.setPrint();


2. listing  ------------------

#include <iostream>
#include <iomanip>

using namespace std;

#include "int.h"

IntegerSet::IntegerSet(int s)
{
   emptySet (); //++
   size = s;
   set = new int[size];
}

IntegerSet::IntegerSet(const IntegerSet &init)
{
   emptySet(); //++

   size = init.size;
   set = new int[size]; //++

   for(int i = 0; i < size; i++)
      set[i] = init.set[i]; //++

}

void IntegerSet::emptySet()
{
    delete [] set;  //++
}

void IntegerSet::inputSet()
{
   int number;

   do {
     cout << "Enter an element (-1 to end): ";
     cin >> number;

     if(validEntry(number))
       set[number] = 1;

       else if(number != -1)
       cout << "Invalid Element\n";

   } while(number != -1);
     cout << "Entry complete\n";

}

void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;

   cout << '{';

   for(int u = 0; u < size; ++u)
      if(set[u]) {
     cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
     empty = false;
     ++x;

      }

   if(empty)
     cout << setw(4) << "---";
     cout << setw(4) << "}" << '\n';

}

IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r)
{
   IntegerSet temp(size > r.size ? size : r.size);
   temp.emptySet();

   int iterations = (size < r.size ? size : r.size);

   for(int i = 0; i < iterations; i++)
      if(set[i] == 1 || r.set[i] == 1)
     temp.set[i] = 1;

   return temp;

}

IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
{
   //++
   // will change 'set' to be intersection of current set and 's'
   // check for each item of set, if it is in s.
   // a duplicate set item will remain duplicated in set, [if is in s of course]
   int *newset = new int[size];
   int i,j,remain;
   for (remain=i=0 ; i<size ; i++)
   {
       for (j=0 ; j<s.size && set[i]!=s.set[j] ; j++) /* */;
       if (j < s.size) // it's in
           newset[remain++] = set[i];
   }
   // change set to newset
   set = newset;
   size = remain;
   // could be a bit cleaner by deleting set and reallocating the exact right nb of data after intersection
}

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

}

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

bool IntegerSet::isEqualTo(const IntegerSet &s) const
{
   //++
   int i,equal = (size == s.size);

   if (equal)
   {
      for (i=0 ; i<size && set[i]==s.set[i] ; i++) /* */;
      if (i < size) equal = 0;
   }

   return (equal);
}

// int_1_driver.cpp



int main()
{
   IntegerSet a(101);
   IntegerSet b(101);
   IntegerSet *c;  //++
   IntegerSet *d;  //++

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   //++ union of (a,b) in c
    c = new IntegerSet(a);
    c->unionOfIntegerSets(b);

   //++ intersection of (a,b) in d
    d = new IntegerSet(a);
    d->intersectionOfIntegerSets(b);

   cout << "\nUnion Of A and B is:\n";
   c->setPrint();
   cout << "Intersection of A and B is:\n";
   d->setPrint();

   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.setPrint();

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

   cout << endl;

   return 0;

}
0
 
edelossantosAuthor Commented:
#include <iostream>
#include <cstring>
#include <sstream>

using namespace std ;

class IntSet {
     private :
          int *set ;
          int size ;
       public :
             IntSet ( int s ) ;
             IntSet ( const IntSet &s ) ;
             void InsertElement ( int k ) ;
             void DeleteElement ( int k ) ;
             bool IsValideElement ( int k ) ;
             void SetUnion ( const IntSet &s ) ;
             void SetIntersect ( const IntSet &s ) ;          
};

IntSet::IntSet ( int s ) : size ( s ) {
     set = new int [s] ;
     memset ( set, 0, s ) ;
}

IntSet::IntSet ( const IntSet &s )  : size ( s.size ) {
     set = new int [size] ;
     for ( int i = 0 ; i < size ; i ++ )
          set [i] = s.set [i] ;
}

bool IntSet::IsValideElement ( int k ) {
     if ( k < 0 || k >= size )
          return false ;
     else
           return true ;    
}

void IntSet::InsertElement ( int k ) {
     if ( IsValideElement ( k ) ) {
          cout << "Out of Range" ;
     }
     else {
          set [k-1] = 1 ;
     }
}

void IntSet::SetIntersect(const IntSet &s) {

     int maxSize;

     if(s.size > size)
       maxSize = s.size;
     else
       maxSize = s.size;

     for(int i = maxSize; i >= 0; i++) {
      if(set[i] == 1 && s.set[i] == 1) {
        maxSize = i;
       break;
        }

     }

     IntSet newSet(maxSize) {

     for(int i = 0; i < maxSize; i++) {
      if(set[i] == 1 && s.set[i] == 1) {
        newSet[i] = 1;
        }
     }

}

void IntSet::DeleteElement ( int k ) {
     if ( IsValideElement ( k ) ) {
          cout << "Out of Range" ;
     }
     else {
          set [k-1] = 0 ;
     }
}

int main(int argc, char* argv[])
{
     char str[] = "17445.21" ;

     float val = atoi ( str ) ;
       cout << val ;
    system("pause");
     return 0;
}

test.cpp: In method `void IntSet::SetIntersect(const IntSet &)':
test.cpp:66: parse error before `{'
test.cpp:68: name lookup of `i' changed for new ANSI `for' scoping
test.cpp:57:   using obsolete binding at `i'
test.cpp:68: parse error before `)'

0
 
Sys_ProgCommented:
void IntSet::SetIntersect(const IntSet &s) {

     int maxSize;

     if(s.size > size)
       maxSize = s.size;
     else
       maxSize = s.size;

     for(int i = maxSize; i >= 0; i++) {               =======>> i++ should actually be i-- ;
           if(set[i] == 1 && s.set[i] == 1) {
                maxSize = i;
                break;
            }
     }

     IntSet newSet(maxSize) {

     for(int i = 0; i < maxSize; i++) {
     if(set[i] == 1 && s.set[i] == 1) {
       newSet[i] = 1;
        }
     }

}


Also, consider following code snippet

for ( int j = 0 ; j < 10 ; j ++ ) {
     cout << j ;
}

In this, the scope of j is limited upto end of for loop
Thus if u cannot access j after the end of for loop
This is what the C+ standard says

However, some compilers like VC++6.0 do not obey this and hence consider j available even after for loop

Thus in your code
u have used two for loops one after the other with the same variable i BUT u have redeclared it in the second for loop
For your compiler, write the second for loop as

  for(i = 0; i < maxSize; i++) {
      if(set[i] == 1 && s.set[i] == 1) {
           newSet[i] = 1;
       }
   }

instead of
  for(int i = 0; i < maxSize; i++) {
      if(set[i] == 1 && s.set[i] == 1) {
           newSet[i] = 1;
        }
    }


Amit
0
 
MercantilumCommented:
Working code  (change in constructors in order not to call empySet since 'set' is not null and it crashes :)

#include <iostream>
#include <iomanip>

using namespace std;

#include "zz.h"

IntegerSet::IntegerSet(int s)
{
   size = s;
   set = new int[size];
}

IntegerSet::IntegerSet(const IntegerSet &init)
{
   size = init.size;
   set = new int[size]; //++

   for(int i = 0; i < size; i++)
      set[i] = init.set[i]; //++

}

void IntegerSet::emptySet()
{
    delete [] set;  //++
}

void IntegerSet::inputSet()
{
   int number;

   do {
     cout << "Enter an element (-1 to end): ";
     cin >> number;

     if(validEntry(number))
       set[number] = 1;

       else if(number != -1)
       cout << "Invalid Element\n";

   } while(number != -1);
     cout << "Entry complete\n";

}

void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;

   cout << '{';

   for(int u = 0; u < size; ++u)
      if(set[u]) {
     cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
     empty = false;
     ++x;

      }

   if(empty)
     cout << setw(4) << "---";
     cout << setw(4) << "}" << '\n';

}

IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r)
{
   IntegerSet temp(size > r.size ? size : r.size);
   temp.emptySet();

   int iterations = (size < r.size ? size : r.size);

   for(int i = 0; i < iterations; i++)
      if(set[i] == 1 || r.set[i] == 1)
     temp.set[i] = 1;

   return temp;

}

IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
{
   //++
   // will change 'set' to be intersection of current set and 's'
   // check for each item of set, if it is in s.
   // a duplicate set item will remain duplicated in set, [if is in s of course]
   int *newset = new int[size];
   int i,j,remain;
   for (remain=i=0 ; i<size ; i++)
   {
       for (j=0 ; j<s.size && set[i]!=s.set[j] ; j++) /* */;
       if (j < s.size) // it's in
           newset[remain++] = set[i];
   }
   // change set to newset
   delete [] set;
   set = newset;
   size = remain;
   // could be a bit cleaner by deleting set and reallocating the exact right nb of data after intersection
}

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

}

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

bool IntegerSet::isEqualTo(const IntegerSet &s) const
{
   //++
   int i,equal = (size == s.size);

   if (equal)
   {
      for (i=0 ; i<size && set[i]==s.set[i] ; i++) /* */;
      if (i < size) equal = 0;
   }

   return (equal);
}

// int_1_driver.cpp



int main()
{
   IntegerSet a(101);
   IntegerSet b(101);
   IntegerSet *c;  //++
   IntegerSet *d;  //++

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   //++ union of (a,b) in c
    c = new IntegerSet(a);
    c->unionOfIntegerSets(b);

   //++ intersection of (a,b) in d
    d = new IntegerSet(a);
    d->intersectionOfIntegerSets(b);

   cout << "\nUnion Of A and B is:\n";
   c->setPrint();
   cout << "Intersection of A and B is:\n";
   d->setPrint();

   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.setPrint();

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

   cout << endl;

   return 0;

}
0
 
MercantilumCommented:
change #include "zz.h"  with  #include "int.h"
(I used zz.h here :)
0
 
edelossantosAuthor Commented:
Mercantilum
      Your program runs but it faulted a core dump.  Is this normal?  If not, please advise on how to repair.  Del
0
 
MercantilumCommented:
Use the last one (previous answer) !

Here it is again:

>> Working code  (change in constructors in order not to call empySet since 'set' is not null and it crashes :)


--------------------------

#include <iostream>
#include <iomanip>

using namespace std;

#include "int.h"

IntegerSet::IntegerSet(int s)
{
   size = s;
   set = new int[size];
}

IntegerSet::IntegerSet(const IntegerSet &init)
{
   size = init.size;
   set = new int[size]; //++

   for(int i = 0; i < size; i++)
      set[i] = init.set[i]; //++

}

void IntegerSet::emptySet()
{
    delete [] set;  //++
}

void IntegerSet::inputSet()
{
   int number;

   do {
     cout << "Enter an element (-1 to end): ";
     cin >> number;

     if(validEntry(number))
       set[number] = 1;

       else if(number != -1)
       cout << "Invalid Element\n";

   } while(number != -1);
     cout << "Entry complete\n";

}

void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;

   cout << '{';

   for(int u = 0; u < size; ++u)
      if(set[u]) {
     cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
     empty = false;
     ++x;

      }

   if(empty)
     cout << setw(4) << "---";
     cout << setw(4) << "}" << '\n';

}

IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r)
{
   IntegerSet temp(size > r.size ? size : r.size);
   temp.emptySet();

   int iterations = (size < r.size ? size : r.size);

   for(int i = 0; i < iterations; i++)
      if(set[i] == 1 || r.set[i] == 1)
     temp.set[i] = 1;

   return temp;

}

IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
{
   //++
   // will change 'set' to be intersection of current set and 's'
   // check for each item of set, if it is in s.
   // a duplicate set item will remain duplicated in set, [if is in s of course]
   int *newset = new int[size];
   int i,j,remain;
   for (remain=i=0 ; i<size ; i++)
   {
       for (j=0 ; j<s.size && set[i]!=s.set[j] ; j++) /* */;
       if (j < s.size) // it's in
           newset[remain++] = set[i];
   }
   // change set to newset
   delete [] set;
   set = newset;
   size = remain;
   // could be a bit cleaner by deleting set and reallocating the exact right nb of data after intersection
}

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

}

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

bool IntegerSet::isEqualTo(const IntegerSet &s) const
{
   //++
   int i,equal = (size == s.size);

   if (equal)
   {
      for (i=0 ; i<size && set[i]==s.set[i] ; i++) /* */;
      if (i < size) equal = 0;
   }

   return (equal);
}

// int_1_driver.cpp



int main()
{
   IntegerSet a(101);
   IntegerSet b(101);
   IntegerSet *c;  //++
   IntegerSet *d;  //++

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   //++ union of (a,b) in c
    c = new IntegerSet(a);
    c->unionOfIntegerSets(b);

   //++ intersection of (a,b) in d
    d = new IntegerSet(a);
    d->intersectionOfIntegerSets(b);

   cout << "\nUnion Of A and B is:\n";
   c->setPrint();
   cout << "Intersection of A and B is:\n";
   d->setPrint();

   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.setPrint();

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

   cout << endl;

   return 0;

}
0
 
edelossantosAuthor Commented:
Excellent!!!! Thank you.  Del
0
 
edelossantosAuthor Commented:
I made some modifications but am still getting errors on this code.

#include <iostream>
#include <cstring>
#include <sstream>

using namespace std ;

class IntSet {
     private :
          int *set ;
          int size ;
       public :
             IntSet ( int s ) ;
             IntSet ( const IntSet &s ) ;
             void InsertElement ( int k ) ;
             void DeleteElement ( int k ) ;
             bool IsValideElement ( int k ) ;
             void SetUnion ( const IntSet &s ) ;
             void SetIntersect ( const IntSet &s ) ;          
};

IntSet::IntSet ( int s ) : size ( s ) {
     set = new int [s] ;
     memset ( set, 0, s ) ;
}

IntSet::IntSet ( const IntSet &s )  : size ( s.size ) {
     set = new int [size] ;
     for ( int i = 0 ; i < size ; i ++ )
          set [i] = s.set [i] ;
}

bool IntSet::IsValideElement ( int k ) {
     if ( k < 0 || k >= size )
          return false ;
     else
           return true ;    
}

void IntSet::InsertElement ( int k ) {
     if ( IsValideElement ( k ) ) {
          cout << "Out of Range" ;
     }
     else {
          set [k-1] = 1 ;
     }
}

void IntSet::SetIntersect(const IntSet &s) {

     int maxSize;

     if(s.size > size)
       maxSize = s.size;
     else
       maxSize = s.size;

     for(int i = maxSize; i >= 0; i--) {
      if(set[i] == 1 && s.set[i] == 1) {
        maxSize = i;
       break;
        }

     }


     IntSet newSet(maxSize) {

     int j;
     for(j = 0; j < 10; j++) {
      cout << j;
     }
     for(i = 0; i < maxSize; i++) {
      if(set[i] == 1 && s.set[i] == 1) {
        newSet[i] = 1;
        }
     }

}

void IntSet::DeleteElement ( int k ) {
     if ( IsValideElement ( k ) ) {
          cout << "Out of Range" ;
     }
     else {
          set [k-1] = 0 ;
     }
}

int main(int argc, char* argv[])
{
     char str[] = "17445.21" ;

     float val = atoi ( str ) ;
       cout << val ;
    system("pause");
     return 0;
}

test.cpp: In method `void IntSet::SetIntersect(const IntSet &)':
test.cpp:66: parse error before `{'
test.cpp:69: `j' undeclared (first use this function)
test.cpp:69: (Each undeclared identifier is reported only once
test.cpp:69: for each function it appears in.)
test.cpp:72: name lookup of `i' changed for new ANSI `for' scoping
test.cpp:57:   using obsolete binding at `i'
test.cpp:74: no match for `IntSet &[int &]'

0
 
MercantilumCommented:
What did you modify :)
0
 
edelossantosAuthor Commented:
void IntSet::SetIntersect(const IntSet &s) {

     int maxSize;

     if(s.size > size)
       maxSize = s.size;
     else
       maxSize = s.size;

     for(int i = maxSize; i >= 0; i--) {
     if(set[i] == 1 && s.set[i] == 1) {
       maxSize = i;
      break;
        }

     }


     IntSet newSet(maxSize) {

     int j;
     for(j = 0; j < 10; j++) {
     cout << j;
     }
     for(i = 0; i < maxSize; i++) {
     if(set[i] == 1 && s.set[i] == 1) {
       newSet[i] = 1;
        }
     }

}
0
 
MercantilumCommented:
- Hu... what is
   IntSet newSet(maxSize) {
in the middle of  SetIntersect
check the {} ... you'll see.

- newSet is not in the definition of the class
  if it is a declaration, why maxSize has no type ?
  and why 's' is not declared ?  (not even in the prototype)
  and, overall, I'd be curious to know what is
     newSet[i] = 1;   ?!?!
- what is supposed to do this newSet function
0
 
edelossantosAuthor Commented:
Taking a guess, probably the same thing emptySet would do.  I am not 100 % sure.  If possible...modify the code to make it run.  Your expertise is appreciated.  Del
0
 
MercantilumCommented:
Hu .... do you write your code on the fly, as a writer ... or you have any target, or the terms of a problem or....
How do you want me to guess what does a function you want me to write as even you, you don't know what's supposed to do...
I'm lost :)
Don't you have some terms of the problem ? Is it finally howework ?
The current body of newSet is so strange, cannot guess what it does [I thought it was supposed to enlarge the array ... ]
0
 
edelossantosAuthor Commented:
This was a code given to me and I just wanted to see it run.  Quite frankly, the code that you gave me will sullfice.  And yes, this was part of a homework assignment.  I look forward to working with you again.  A million thanks.  Del
0
 
MercantilumCommented:
Well if this is homework, you should try to fix *at least* the syntax of newSet ... not that difficult.
0
 
edelossantosAuthor Commented:
Will do.  Thanks again.  Del
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 14
  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now