set::set()
{
     cardinality = 0;
     setlist.resize(DEFAULTSIZE, false);
}

set::set(int n)
{
     cardinality = 0;
     setlist.resize(50000, false);
}

bool set::empty() const
{
     if ( setlist.empty() )
     {
       return true;
     }
     return false;
}

void set::insert(int x)
{
     if (x >= 0 && x <= DEFAULTSIZE)
     {
           setlist.insert(setlist.end(), 1, x);
           cardinality++;
     }
}

int set::getLargest() const
{
    if ( !setlist.empty() )
    {
         *( max_element( setlist.begin(), setlist.end() ) );
    }
} 

int set::getSmallest() const
{
    if ( !setlist.empty() )
    {
         *( min_element( setlist.begin(), setlist.end() ) );
    }
}

void set::dump() const
{
     copy (setlist.begin(), setlist.end(), ostream_iterator<int>(cout," "));
     cout << endl;
}

bool set::isIn(int x) const
{
     *( find( setlist.begin(), setlist.end(), x) );
}

THIS IS THE CLASS FILE:

public :
   set(); // default constructor - constructs an empty  set of integers in range 0 to DEFAULTSIZE -1
   set(int n);
     // pre : n >= 0 and n <= 500000
     // post : constructs an empty  set of integers in range 0 to n-1
   bool empty() const;
     // pre : none
     // post : returns true if set empty and otherwise false
   bool isIn(int x) const; 
     // pre : none
     // post : returns true if x is in set and otherwise false
   void dump() const;
     // pre : none
     // post : dumps out the valus in set (separated by white space) 
   int getCardinality() const;
     // pre : none
     // post :  returns the number of elements in the set
   void insert(int x);
     // pre : none
     // post : inserts x into this set (if in range)
   int getSmallest() const;
     // pre : set is not empty
     // post : returns smallest element in the set
   int getLargest() const;  
     // pre : set is not empty
     // post : returns largest element in the set

There are a few problems with the way these functions work:
1. When I insert elements into the vector, I cannot insert an element higher than 1.. only 0's and 1's.
2. When I dump the elements all I see are 0's and 1's
3. I don't understand if set(int n) is correct or not? Can anyone clarify?
4. Do all the other functions look right?

Help would be appreciated.
Thanks :)

1. When I insert elements into the vector, I cannot insert an element higher than 1.. only 0's and 1's.

What is DEFAULTSIZE set to because if it is 1 then yeah 0s and 1s are within the range you set in your if() statement.

2. When I dump the elements all I see are 0's and 1's

Your dump is showing you what you have inside of the vector so if you put 1s and 0s in you are going to get 1s and 0s out.

3. I don't understand if set(int n) is correct or not? Can anyone clarify?

You are not setting it to n you have the number 50000 in the resize parameter which would make the vector have 50000 elements and fill them with 0s. I think you want it to be resize(n); putting false in there is just redundant.

4. Do all the other functions look right?

As for the rest it looks fine aside from the fact that your getLargest(), getSmallest() and isIn() functions have no return value. Just put

return *( min_element( setlist.begin(), setlist.end() ) );

and it will return the smallest value and do the same for the others.

Also, you have this cardinality variable. Is this supposed to track how big the vector is? If so you want your set(int n) function to be

set::set(int n)
{
     if( n >= 0 )
     {
          cardinality = n;
          setlist.resize(n, false);
     }
}

Again I have no idea what DEFAULTSIZE is set to because when I tried to use it to see what the value is it said that it was undeclared. So for your set() and insert() function where that variable pops up that could be causing you problems.

Comments
I like your way.

Thanks heeps :)

DEFAULTSIZE is set to 100.

EDIT: Still not working, inserting and dumping only 0's and 1's for some reason.. still not sure :S

Edited 7 Years Ago by scantraXx-: n/a

Comments
Not working (:

Sorry, it's just really frustrating.
I've used that site before.. but it's not enough unfortunately.. just trying to find the mistake I made.. hmm.

Post your main function please, also.
Or tell us what operations you've tried before dumping output.
Also check what values those functions return: i believe some of them has to do something with boolean.

Debug it hard!
Execute step by step, see which rows get executed and when.
View the values of the vector dynamically and see how they change.

Btw, what do you mean you can't _insert_ other values? Does it _accept_ only 0's and 1's ? or do you insert random values and they get converted to binary symbols?

I also don't see where you get to insert these values, i mean, where is the function or fields?
There are things that i don't understand there, in that code.Probably because of my lack of knowledge but neither do i see the basics stuff.

ps: Do consider debugging as an important tool for error solving. Do it, and tell us where exactly the code meets the odds.

If I input integers larger than 1 it dumps out only 0's and 1's or it only accepts 0's and 1's.. no idea.

Show me where is the function and where are the fields/variables that you use to store input values.

Why don't you answer all of my questions... it's not too pleasant to re ask them, did you know that? Or isn't time valuable for you , anyhow?...

I'm not sure if you have solved your problem in this but when I used the code from your first post with my modifications from the seconds post I got it working fine.

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

class set
{
	int cardinality;
	vector<int> setlist;
	const static int DEFAULTSIZE = 100;
	public :
	set();
	set(int n);
	bool empty() const;
	bool isIn(int x) const; 
	void dump() const;
	int getCardinality() const;
	void insert(int x);
	int getSmallest() const;
	int getLargest() const;  
};

set::set()
{
     cardinality = DEFAULTSIZE;
     setlist.resize(DEFAULTSIZE, false);
}

set::set(int n)
{
     cardinality = n;
     setlist.resize(n, false);
}

bool set::empty() const
{
     if ( setlist.empty() )
     {
       return true;
     }
     return false;
}

void set::insert(int x)
{
     if (x >= 0 && x <= DEFAULTSIZE)
     {
           setlist.insert(setlist.end(), 1, x);
           cardinality++;
     }
}

int set::getLargest() const
{
    if ( !setlist.empty() )
    {
    	return *( max_element( setlist.begin(), setlist.end() ) );
    }
} 

int set::getSmallest() const
{
    if ( !setlist.empty() )
    {
    	return *( min_element( setlist.begin(), setlist.end() ) );
    }
}

void set::dump() const
{
     copy (setlist.begin(), setlist.end(), ostream_iterator<int>(cout," "));
     cout << endl;
}

bool set::isIn(int x) const
{
     return *( find( setlist.begin(), setlist.end(), x) );
}


int main()
{
	set test(0);
	test.insert(5);
	test.insert(10);
	test.dump();
	
	system("PAUSE");
	return 0;
}

The way you have it set up for your set() constructor it will output one-hundred 0s and then what you put in with your insert() function. No idea where you are getting the 1s and 0s from.

Any chance you could attach your source files so I can see exactly what you have and not just sections?
Click the Reply to Thread button and right below the input box is Additional Options where you can attach your .h and .cpp files.

Because after looking at your screen shot I have a feeling you aren't showing everything that plays a role in this.

Here are the files and the test file im using.

We can't change the set.h file for specific reasons. So maybe it's the test file? It looks fine to me though :S

Thanks again :)

Edited 7 Years Ago by scantraXx-: n/a

Attachments
#include "set.h"
#include <iterator>

using namespace std;


set::set()
{
     cardinality = 0;
     setlist.resize(DEFAULTSIZE, false);
}

set::set(int n)
{
     cardinality = n;
     setlist.resize(n, false);
}

bool set::empty() const
{
     if ( setlist.empty() )
     {
       return true;
     }
     return false;
}

void set::insert(int x)
{
     if (x >= 0 && x <= DEFAULTSIZE)
     {
           setlist.insert(setlist.end(), 1, x);
           cardinality++;
     }
}

int set::getLargest() const
{
    if ( !setlist.empty() )
    {
    	return *( max_element( setlist.begin(), setlist.end() ) );
    }
} 

int set::getSmallest() const
{
    if ( !setlist.empty() )
    {
    	return *( min_element( setlist.begin(), setlist.end() ) );
    }
}

void set::dump() const
{
     copy (setlist.begin(), setlist.end(), ostream_iterator<int>(cout," "));
     cout << endl;
}

bool set::isIn(int x) const
{
     return *( find( setlist.begin(), setlist.end(), x) );
}
#ifndef SETH
#define SETH
// definition of a set of integers 
#include <iostream>
#include <vector>
using namespace std;
const int DEFAULTSIZE = 100;

class set
{
  public :
   set(); // default constructor - constructs an empty  set of integers in range 0 to DEFAULTSIZE -1
   set(int n);
     // pre : n >= 0 and n <= 500000
     // post : constructs an empty  set of integers in range 0 to n-1
   bool empty() const;
     // pre : none
     // post : returns true if set empty and otherwise false
   bool isIn(int x) const; 
     // pre : none
     // post : returns true if x is in set and otherwise false
   void dump() const;
     // pre : none
     // post : dumps out the valus in set (separated by white space) 
   int getCardinality() const;
     // pre : none
     // post :  returns the number of elements in the set
   void insert(int x);
     // pre : none
     // post : inserts x into this set (if in range)
   int getSmallest() const;
     // pre : set is not empty
     // post : returns smallest element in the set
   int getLargest() const;  
     // pre : set is not empty
     // post : returns largest element in the set
   set unions(int x) const;
     // pre : none
     // post : returns the union of this set and x (if x in range)
   set unions(const set &other) const;
     // pre : both  sets are in same range (domain)
     // post : returns the union of this set and other set
   set intersect(int x) const;
     // pre :  none
     // post : returns the intersection of this set and x (if x in range)
   set intersect(const set &other) const;
     // pre : both sets are in same range (domain)
     // post : returns the intersection of this set and other set
   void display()const;
     // pre : none
     // displays the elements of this set enclosed in braces and
     // separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
   set difference(const set &other) const;
     // pre :  both sets are in same range (domain)
     // post : returns the difference of this set and other set
   set difference(int x)const;
     // pre : none
     // post : returns the difference of this set and x (if x in range)
   int getUpperRange()const;
     // pre : none
     // post : returns the upper value in the domain (upper range)
   bool isEqual (const set & other) const;
     // pre : both sets are in the same range (domain)
     // post : returns true if this set is equal to other set
  private:
     vector <bool> setlist;  // indicates set elements as true
     int cardinality; // number of items in set

}; 
#endif
#include "set.h"
#include <iostream>
using namespace std;
void printMenu();

int main()
{
   char choice = '#';
   int element;
   set myset;
   cout << " This program can be used for testing basic set implementation " << endl;
  
   while (choice != 'Q')
   {
      printMenu();
      cin >> choice;
      choice = toupper(choice);   
      switch (choice)
      {
        case 'I' : cout << " Enter int element " << endl;
                   if (cin >> element)
                   myset.insert(element);
                   break;
        case 'E' : if (myset.empty()) cout << "Set is empty" << endl;
                   else cout << "Set is not empty " << endl;
                   break;
        case 'L' : if (!myset.empty()) cout << "Largest is " << myset.getLargest() << endl; break;
        case 'S' : if (!myset.empty()) cout << "Smallest is " << myset.getSmallest() << endl; break;
        case 'D' : myset.dump(); break;
        //case 'P' : myset.display(); break;
        case 'F' : cout << "Enter int element " << endl;
                   if ((cin >> element) && myset.isIn(element)) 
                      cout << element <<" is in set " << endl; 
                   else cout << element << " is not in set " << endl; break;
        case 'Q' : break;
        default : cout << "Invalid choice " << endl;
      }
  }
  system("pause");
  return 0;
}  
    

void printMenu()
{
   cout << "\t Testing set " << endl;
   cout << " I : Insert an element into the set " << endl;
   cout << " E : test if set is Empty" << endl;
   cout << " L : show Largest element in set " << endl;
   cout << " S : show Smallest element in set " << endl;
   cout << " D : Dump the elements in set " << endl;
   cout << " F : Find if element x is in set " << endl;
   cout << " P : Pretty display of elements in set " << endl;
   cout << " Q : Quit " << endl;
   cout << endl;
}

Well I was right that the source of the problem wasn't being posted.

vector <bool> setlist;  // indicates set elements as true

bool type translates to 0s and 1s so anything that is not false is a true meaning everything that is not 0 becomes 1.
If you change this to <int> this should fix your problem.

Ahhh I see now :)

But the whole project is meant to be based on vector <bool> setlist :S
Does that mean my functions arn't right? Could you please show me how to fix them?

How would I make the getSmallest and getLargest and dump functions work with vector <bool> ?

Because they need to work the same way as if it were <int> otherwise the dump would always just dump 0's and 1's and not the elements in the vector and getSmallest would always be 0 and getLargest always 1.. hmm..

I have removed your empty() function because it made no sense at all. It's no different form the empty() function that a std::vector has.
I also included: #include <algorithm> because it is required for "max_element", "find" etc.

I also fixed your warnings. I've put in comment what I've changed and why:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm> // ADDED THIS for max_element etc

using namespace std;

class set
{
	int cardinality;
	vector<int> setlist;
	const static int DEFAULTSIZE = 100;
	public :
	set();
	set(int n);
	bool isIn(int x) const; 
	void dump() const;
	int getCardinality() const;
	void insert(int x);
	int getSmallest() const;
	int getLargest() const;  
};

set::set()
{
     cardinality = DEFAULTSIZE;
     setlist.resize(DEFAULTSIZE, false);
}

set::set(int n)
{
     cardinality = n;
     setlist.resize(n, false);
}


void set::insert(int x)
{
     if (x >= 0 && x <= DEFAULTSIZE)
     {
           setlist.insert(setlist.end(), 1, x);
           cardinality++;
     }
}

int set::getLargest() const
{
    if ( !setlist.empty() )
    {
    	return *( max_element( setlist.begin(), setlist.end() ) );
    }
    else return -1; // ADDED THIS. All paths must return a value.
    // You should check for -1 when calling this function
} 

int set::getSmallest() const
{
    if ( !setlist.empty() )
    {
    	return *( min_element( setlist.begin(), setlist.end() ) );
    }
    else return -1; // ADDED THIS. All paths must return a value.
    // You should check for -1 when calling this function
}

void set::dump() const
{
     copy (setlist.begin(), setlist.end(), ostream_iterator<int>(cout," "));
     cout << endl;
}

bool set::isIn(int x) const
{
     //return *( find( setlist.begin(), setlist.end(), x) ); // DELETED
    //If you want to return a  boolean, you do not need the value.
    if (find( setlist.begin(), setlist.end(), x) == setlist.end()) return false; //ADDED
    else return true; //ADDED
}


int main()
{
	set test(0);
	test.insert(5);
	test.insert(10);
	test.dump();
	
	cin.get();
	return 0;
}

When I run your code it outputs: 5 10 which is expected output IMO.

I'm curious how you got to this line: copy (setlist.begin(), setlist.end(), ostream_iterator<int>(cout," ")); That's not a very common way to print a vector. Did you think of it yourself?

Yeah you would think that its an int array but he wants it to work with a bool array.

I'm pretty sure making it int is the only way to go.

In your testset.cpp source file it says:
case 'I' : cout << " Enter int element " << endl;

If it wants you to put in a int and expects to get that int back with dump then the vector has to be a int type because there is no way you are going to get a number other than 1 and 0 outta bool.

You can't do it the way you want if you convert the values to binary units.

I was thinking that maybe the assignment meant to convert a int value into a binary one... like
input 9 (int)= output 1000 (b)
If so , you'll have to use a decimal map.

But getting a value and casting it to boolean is surely the wrong way to do if you want your original int values at the output.

This article has been dead for over six months. Start a new discussion instead.