Click to See Complete Forum and Search --> : Why does my array still empty?


nicoletonyf
October 18th, 2004, 02:06 PM
hello there, when I try to combine 2 arrays together it displays "0".
I think I don't understand the concept of constant. This is my code:

//header file
#ifndef INTSET_H
#define INTSET_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

//main file
#include <iostream>
#include <iomanip>
#include <new>
using std::cout;
using std::cin;
using std::setw;

#include "integerset.h"


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

IntegerSet::IntegerSet( const IntegerSet &init )
{
size = init.size;
set = new int[ size ]; /* write statement to allocate sufficient memory */
emptySet();

for ( int i = 0; i < size; i++ )
insertElement(set[i]); /* write statement to copy elements of init */

}

void IntegerSet::emptySet()
{
for ( int i = 0; i < size; i++ )
set[ i ] = 0;
}

void IntegerSet::inputSet()
{
int number;

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

// check number first
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; // assume set is empty

cout << '{';

for ( int u = 0; u < size; ++u )

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

}

if ( empty )
cout << setw( 4 ) << "---"; // display an empty set

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 &r )
{
IntegerSet inter( size > r.size ? size : r.size );

inter.emptySet();

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

for ( int i = 0; i < reduction; i++ )
if ( set[ i ] == 1 && r.set[ i ] == 1 )
inter.set[ i ] = 1;

return inter;
}

void IntegerSet::insertElement( int k )
{
if ( validEntry( k ) )
set[ k ] = 1;

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

else
cout << "Invalid delete attempted!\n";
}
bool IntegerSet::isEqualTo( const IntegerSet& eql ) const
{
for ( int i = 0; i < size; i++ )
if ( set[i] = eql.set[i] )
return true;
else
return false;
}


//test file

#include <iostream>
using std::cout;
using std::endl;

#include "integerset.h"

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

cout << "Enter set A:\n";
a.inputSet();
cout << "\nEnter set B:\n";
b.inputSet();
c = a.unionOfIntegerSets( b );
d = a.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();
b.setPrint();

cout << endl;
return 0;

}

NoHero
October 18th, 2004, 02:15 PM
You should not make arrays on you own. The STL has the perfect template for you: std::vector. So why not using them?

You should implement new members because that is just a basic structure ....


class IntegerSet
{
private:

vector<int> m_Set;

public:

IntegerSet ( void )
{
}

IntegerSet ( const IntegerSet& toadd )
{
// constructor goes here
}

void Add ( int Value )
{
m_Set.push_back(Value);
}

int GetValue ( int iIndex ) const
{
return m_Set[iIndex];
}

long GetCount ( void ) const
{
return (long)m_Set.size();
}
// and so on

IntegerSet& operator = ( const IntegerSet& toAdd )
{
IntegerSet set;
long i = 0;
for ( i =0; i < GetCount(); i++ ) set.Add(GetValue(i));
for ( i =0; i < toAdd.GetCount(); i++) set.Add(toAdd.GetValue(i));
return set;
}
}

nicoletonyf
October 18th, 2004, 03:28 PM
Thank you for your reply, but we have not studied template yet. Nicole

NoHero
October 18th, 2004, 03:43 PM
Thank you for your reply, but we have not studied template yet. Nicole

Thats not a big deal:

Include the vector header at first:

#include <vector>

using namespace std;


Then use following syntax to create you own vector:


vector<type> variablename;


Where type is the type of data you want to store:


vector<int> int_vector; // Create an integer vector
vector<char> char_vector; // create character vector
// ... and so on


Than you can easily use the members:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfvectorat.asp

push_back() adds an element, at() will retrieve an object specified by its index (you also can use the [] operator), erase() will delete an item, size() returns the size of vector ...


#include <vector>
#include <ios>
#include <iostream>

using namespace std;

vector<int> int_vector;

int main ( int argc, char* argv[] )
{
int_vector.push_back(10); // add some elements
int_vector.push_back(68);
int_vector.push_back(34);

cout << "The first item is: " << int_vector[0] << endl;
// delete second item: It has index 1
int_vector.erase(int_vector.begin()+1);
cout << "All items: " << endl;
for ( size_t i = 0; i < int_vector.size(); i++ )
{
cout << int_vector[i] << endl;
}
return 0;
}


(please notify if something is wrong)

Regards

In addition to your code, where does the error exactly happen, because I cannot find an error ...

nicoletonyf
October 18th, 2004, 06:10 PM
Hello, If you run the program, and add numbers in each set, you will see 0 for unionOfIntegerSet, and 0 for intersectionOfIntegerSet. Thanks

mehdi62b
October 22nd, 2004, 05:47 AM
Hi there,
Thats not a big deal:

Include the vector header at first:

#include <vector>

using namespace std;

I'm just familiar with C# and .NET namespaces
can anyone tell me where is std namespace? I didnt find it ...
is that in .NET namespaces?
what is STL here?are they the same as .NET namespaces?
sorry I dont know this concepts.
for example in Visual C++.NET for usingn a namespace I do it like below

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;

I know System namespace is in mscorlib.dll file but what is stdafx.h here?
can someone help me get these concepts.
Thanks in advance.

Kheun
October 22nd, 2004, 06:18 AM
Hi mehdi62b, the above discussion is mainly on C++ but not C# or .Net. The STL stands for Standard Template Library and it resides within the std namespace which is different from the .Net namespaces.

As for stdafx.h file, it is mainly used in Visual C for generating precompiled header which helps to shorten the compilation time.

mehdi62b
October 22nd, 2004, 06:41 AM
Thank you for getting me underestood.