Iterative loop repeats more times than I expect

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

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];
      cout<<" real:  "<<i<<"   "<<j<<"   "<<Array[i][j]<<endl;
    }
  }

  // arraycompare(Array,simu_times,randomrun,numberofrun);
 
  for(int j=0;j<simu_times;++j){
    cout<<"beginning:  "<<j<<endl;
    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<<"sanme: "<< j<<"   "<<i<<endl;
	  deleteArray(Array);
	  createArray(simu_times,randomrun,numberofrun);
	  //    deleteArray(Array);
	}
      }
    }
    cout<<"print begins0:  "<<j<<endl;
  }
 

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

  //  printArray(Array,simu_times,randomrun);
  cout<<"print ends:  "<<endl;
  return Array;

}




Last edited on
Please format the code with code tags, anyways what are the initial values for simu_times and randomrun?
Thanks, lostwithcpp, simu_times=5, randomrun=3,numberofrun=5
There's a chance of repetition in this case.
Line 31: You've forgotten to assign the newly created array to Array. As a consequence you're further accesing unallocated storage in your loop.

Line 26, 27: Tip: Start loop with j + 1 and yo don't need the following conditional.
Hi, @tcs, thanks for your reply.
I am pretty new to c++. By " You've forgotten to assign the newly created array to Array", I think I did it in Line 17? Between Line 30 and Line 31, I initialized Array again by putting this line in between,
1
2
3

 int Array[simu_times][randomrun]


but probably I get you wrong .....
In line 17 you've assign values to Array elements while in line 12 you've assign a value, an array of int*, to the variable Array itself. In line 30 you've destroyed this value (array of int*) so further access to Arrays elements result in undefined behaviour (usually a process crash). In line 31 you've called createArray recursivly to create a new array, which I think you want to use as Arrays new value. So write:

Array = createArray(simu_times,randomrun,numberofrun);

I hope this recursive call makes sense?
Hi, tcs, thanks first of all.
Previously, for the last loop from L41 to L46, the array is printed out more than once. The repetition depends on how many times createArray is called. Now I've change the line as suggested by you, the situation is still the same ......
Did you really intend createArray being a recursive function? Initially you've said that it should be an iterative one.
Sorry, tcs, it should be recursive. I made a mistake. I thought they had the same meaning. Now I figured it out, it should be recursive.
Topic archived. No new replies allowed.