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.

commented: I like your way. +17

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

commented: Not working (: -3

scantraXx,
Don't put piggy word like urgent. It's a bad manner. Take a look at Vector Reference.

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.

Really? wow still doesn't work for me.. here is a screenshot showing it.. it just dumps my insert values > 0 as a 1.. hmm..

Here is what it looks like:
[img]http://img196.imageshack.us/img196/1278/hmme.jpg[/img]

Any idea?
Thanks for your help, much appreciated.

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 :)

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.

Hmm okay. Thanks for the help guys :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.