0

I am trying to set elements of a set at an index to 1 if an array contains those indices as its elements. Array size is 20 i.e. index 0 to 19

For ex -

 int myArray[5] = {1,4,2}; //user input or statically defined in driver(main)
 int set[] = {0,1,1,0,1}; //successfully set in constructor
 IntegerSet intObj(set);//At a point, program stops execution. Any idea why?

Here is the partial code

    //integerset.h 

    class IntegerSet{
          public :
              IntegerSet( int [] );
              .....
          private :
              int set[20];            
    };

  //integerset.cpp (header files included)

  IntegerSet :: IntegerSet( int arr[]){
       for(int i = 0; i <20; i++) //works fine (printed "test" in loop)
               set[i] = 0; //if not this then garbage elems set
       for ( int i = 0; arr[i] != '\0' ; i++ ) //works fine. (printed "test" in loop)
          set [arr[i]] = 1;        
       for ( int i = 0; i < 20; i++ ) //program stops execution here
           cout<<i<<"\t"<<set[i]<<"\n"; //tried to print "test" in loop,program crashed
  }

  //main.cpp (header files included)


int main(){
             int a[] = {1,3,0,12,14,15,'\0'}; // premature end??
             IntegerSet obj2(a);
             system("pause");
             return 0;
   }
7
  • 3
    bad practice to use set as variable name. Commented May 24, 2013 at 3:51
  • Why do you think arr is zero terminated? Commented May 24, 2013 at 3:54
  • doesn't change the program output/execution. set isn't a keyword. i changed it to setArray though set looked more intuitive but what about the answer to question? Commented May 24, 2013 at 3:54
  • @Sergey : in the for loop with != '\0' condition, if you print "test" it is printed 7 times. i found the answer on stackoverflow to the question ..how to find length of array when array is passed as argument, because sizeof operator in such case would return size of pointer which in my machine is 4 Commented May 24, 2013 at 3:58
  • set [arr[i]] = 1; what is this , could someone explain? Commented May 24, 2013 at 3:58

2 Answers 2

1

In arr[i] != '\0', your array does not have a null terminator, so the loop continues and indexes elements pass the end of the array.

It is best practice to use std::array or std::vector. Another option is the following fix to your code:

template <int N>
IntegerSet :: IntegerSet(int (&arr) [N]) : set() {
       for (int i = 0; i < N ; i++)
          set [arr[i]] = 1;        
       for (int i = 0; i < 20; i++)
           cout<<i<<"\t"<<set[i]<<"\n";
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

int a[] = {1,3,4,12,14,15,'\0'} in main.cpp does the job! Thanks all!
@ShivaniDhanked: That can cause problems, try the following array: int a[] = {1,3,4,0,14,15,'\0'};. The loop will stop prematurely.
oh yes, i soon realized it after posting this comment.. but how come the loop printed "test string" 7 times i.e. correctly parsed to length of array without using terminating null...isn't there a simpler way out than what you have just suggested. i am looking for something closest to my code., Thanks
strings are special, they do have an implicit null terminator at the end. Another way to solve your problem is to pass the length of the array as the second parameter, IntegerSet :: IntegerSet( int arr[], int length) and loop until then: for (int i = 0; i < length ; i++).
Thanks but know it and can't do that, the set is the only private data member, so it alone can be passed to the constructor.
|
0

When you parse an array to a function as a parameter with its name, it refers to the address of the first element in the array. So in your case a is a type of int *. So you need to change your constuctor parameter as int *

1 Comment

int [] and int * mean the same thing as a parameter.

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.