0

The problem is (see output), obj2 elements look like union of obj2 as passed in main method and obj1. also why do both obj1 and obj2 always start with 1,2 no matter what no. of and what elements are present in them. I have now spent an entire night on this question, it had other problems previously : In C++ program which passes array in constructor, execution stops Howsoever trivial this question may look to you. I would appreciate any help..please..and instead of suggesting complex yet efficient solutions available in library of c++, please try to suggest where i am going wrong as a novice :/

thanks in anticipation!

 //partial "integerset.h"

 class IntegerSet{
  public :
         IntegerSet( int [] );
         void insertEl(int);
         void deleteEl(int); //delete is a keyword, can't be identifier
         void printSet();
  private :
         int setArr[20];//no.s can be 1 to 20            
 };

 //partial "integerset.cpp"

 //libraries included

IntegerSet :: IntegerSet( int arr[] ){ 
       for(int i = 0; i < 20; i++)
               setArr[i] = 0; //for consistent data at start,avoid garbage
       for( int i = 0; i < 20; i++){
            if ( arr[i] >= 1 && arr[i] <= 20)
               this->insertEl(arr[i]);           
       }
}
void IntegerSet :: insertEl(int item){
            if ( setArr[item-1] != 1) //-1 so that 5 is checked at 4th position, etc.
                setArr[item-1] = 1; //set 4th array element to 1 if item = 5 
 }
void IntegerSet :: deleteEl(int item){ //delete is a keyword, can't be identifier
            if ( setArr[item-1] != 0 )
               setArr[item-1] = 0;
}
void IntegerSet :: printSet(){
    for ( int i = 0; i < 20; i++){
          if( this->setArr[i] == 1) 
                cout<<i+1<<" "; // + 1 important so that 2 displayed at 1st position
    }
}

   //partial "main.cpp"


  int main(){
      int a[] = {9,10,15,18,19};
      int b[] = {1,3,12,14,15};
      IntegerSet obj1(a);   
      IntegerSet obj2(b);
      cout<<"\nintial obj1\n";
     obj1.printSet();  
     cout<<"\ninitial obj2\n";
     obj2.printSet(); 

    obj1.deleteEl(18);
    cout<<"\nafter deletion of 18 \n";
    obj1.printSet();
    obj1.insertEl(7);
    cout<<"\nafter insertion of 7\n";
    obj1.printSet();

    system("PAUSE");
    return EXIT_SUCCESS;
  }

 //here's the output

 ![output of program][1]


  http://tinypic.com/view.php?pic=25uiceo&s=5

2 Answers 2

1

You are passing garbage to your constructor because your input arrays contain only 5 elements, yet you are indexing them as if they contained 20 elements.

Change:

 int a[] = {9,10,15,18,19};
 int b[] = {1,3,12,14,15};

to:

 int a[20] = {9,10,15,18,19};
 int b[20] = {1,3,12,14,15};

Note that elements which are not explicitly initialised will contain 0, so this is now equivalent to:

 int a[20] = {9,10,15,18,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 int b[20] = {1,3,12,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
Sign up to request clarification or add additional context in comments.

4 Comments

oh yes! and why didn't i make the effort to put up the question hours ago? could you please provide some more explanation or useful link regarding this case of array initialization. Thanks very much!
Now would be a good time to learn to use a debugger - if you had stepped through the constructor in your debugger you would have immediately seen the problem and it would have been fixed a lot sooner.
May be there's something I am not doing so right but my DEV C++ IDE debugger works fine for single files. this was a project(console app) which said .. tinypic.com/view.php?pic=fny612&s=5 after which nothing happened , on next click on debug same thing happens..tis dialog appears n times!
@ShivaniDhanked if your compiler is GNU or similar (probably anything DEV C++ uses that isn't MSVC), ensure that the "-g" flag is present on both compilation and linking commands; this generates debugging information but it's important that it be on the link as well as the compile commands (for sources you want symbolic information for).
1

It is better not to use such magic numbers as 20. Pass it as constructor parameter and store as class member.

IntegerSet::IntegerSet( unsigned n, int data[] );

Or for example:

IntegerSet::IntegerSet( std::vector<int> &data );

If it is important to init object with int[20], pass int[20]:

int a[20] = {9,10,15,18,19};
IntegerSet obj1(a);

Remember that array name in C/C++ is just a raw pointer. It don't contain any information about number of elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.