-1

I tried my best but I can't figure out the problem. At the last part of "createArray", I output the final array created. I mean it to repeat once but then it repeat more times than I expect. createArray is an iterative function. If it repeats 3 times, than at last the array created which fulfil the criterion would be printed out 3+1 times.

I am trying to create an array with 3 numbers 5 times, resulting in a 2D array. The 3 numbers in a array are picked from 0 - 5. I enter createArray(5,3,5). Then these 5 arrays are compared with each other to see if there are repetitions. If there are, the whole process begins again, 5 arrays with 3 numbers each will be picked again and compared with each other. If there are no repetitions at last, there 5 arrays would be printed out.

#include <algorithm>
#include <iterator>


void deleteArray(int** array){
    delete[] array;
 }


int** createArray(int simu_times, int randomrun,int numberofrun){

    vector<Int_t>fChosenRun;
    int** Array = new int*[simu_times];

    for(int i = 0; i < simu_times; ++i) {
        fChosenRun=getRandom(1,randomrun,numberofrun);
        Array[i] = new int[randomrun]; 
        for(int j = 0; j < randomrun; ++j){ 
            Array[i][j] = fChosenRun[j];     
        }
    }

in the following doubly-nested loop, I compare the arrays with each other. If there are any repetitions, this array would be deleted and createArray is called to create the arrays again.

    for(int j=0;j<simu_times;++j){    
        for(int i=0+j;i<simu_times;++i){
            if(j!=i) {
                if (std::equal(Array[j], Array[j]+ sizeof Array[j] / sizeof *Array[j], Array[i])){
                    cout<<"same: "<< j<<"   "<<i<<endl;
                    deleteArray(Array);
                    createArray(simu_times,randomrun,numberofrun);
                }
            }
        }
    }

When the arrays have no repetition, they would be printed out. All arrays should be printed out once, but they are printed out many times.

    for(int i=0;i<simu_times;++i){
        for(int j=0;j<randomrun;++j){
            cout<< i<<"   "<<j<<"   "<<Array[i][j]<<endl;;
       }
        cout<<endl;
    }

    return Array;

}
china_108
  • 11
  • 1
  • 3
    *Please* fix the indentation – Borgleader Nov 28 '14 at 13:04
  • 3
    Use a debugger, that's what they are for. – vsoftco Nov 28 '14 at 13:08
  • Side note: You're not deleting the entire array properly. – barak manos Nov 28 '14 at 13:09
  • @vsoftco Could you elaborate further? – china_108 Nov 28 '14 at 13:11
  • You are calling `create_array` recursively from inside a doubly-nested loop; the least you could do is comment the logic behind that. – Scott Hunter Nov 28 '14 at 13:11
  • Side note #2: not sure where you call that "function-less" code from (perhaps I would if you bothered to copy the entire code and not just "pieces of it"), but most likely the compiler translates `sizeof Array[j]` into the size of an `int*`). – barak manos Nov 28 '14 at 13:11
  • @barakmanos this is actually the entire code. For "std::equal(Array[j], Array[j]+ sizeof Array[j] / sizeof *Array[j], Array[i])", I actually copied it somewhere while I was searching for "how to compare arrays". http://stackoverflow.com/questions/12866413/comparing-arrays-c – china_108 Nov 28 '14 at 13:18
  • So my comment above is correct then. You cannot use `sizeof(Array[j])` and expect it to give you the specific value of `randomrum` that you've allocated it to. – barak manos Nov 28 '14 at 13:19
  • @barakmanos Thanks for your comment, so do you have any suggestions as how I can correct it? – china_108 Nov 28 '14 at 13:22
  • Yes, save those values in a separate array and use them instead. – barak manos Nov 28 '14 at 13:27

1 Answers1

0

It seems you output the loop inside the createArray(), which leads to the extra outputs. Because:

            if (std::equal(Array[j], Array[j]+ sizeof Array[j] / sizeof *Array[j], Array[i])){
                cout<<"same: "<< j<<"   "<<i<<endl;
                deleteArray(Array);
                createArray(simu_times,randomrun,numberofrun);
// after having finally created an Array without duplicates, our successful method
// returns to this point of its parent method. Now the parent method _also_
// will run the following lines of code ...
            }
        }
    }
}

// ... and produce extra output at this point.
for(int i=0;i<simu_times;++i){
    for(int j=0;j<randomrun;++j){
        cout<< i<<"   "<<j<<"   "<<Array[i][j]<<endl;;
   }
    cout<<endl;
}

return Array;

Therefore every try for an Array-creation will produce an output. And since you work with a pointer to an array it will output your final, valid array every time.