I don't know exactly what you are trying to do, but it appears that inside gen() you are attempting to generate random numbers until you find one that hasn't been called yet. When you do, you set the array value at that index to 1 so that it won't be called again. Since your array has a size of 25, you can do this 25 times before the array is full. Luckily, you reset the array to all 0's after 25 times calling gen(), so your idea should work just fine.
Let me try to explain the problem graphically. When you declared int called[25];, it created 25 consecutive ints in memory. The next thing you declared is int number;, which just happens to be right after the called array in memory. So before the program does anything, the uninitialized memory looks like this:
Code:
... called number <-- Variable name
0 1 2 ... 24 <-- Array index
... [ ? | ? | ? | ... | ? ] ? <-- Memory value
You then call initial_card() and initial_called();. Inside initial_called(), you set array indexes 0 though 25 to 0. That overwrites the array. Luckily for you, the variable number is next in memory, and it just gets set to 0 accidentally without any real problems. (NOTE: Don't count on this to happen, never overwrite your array on purpose). So after calling initial_called(), your memory looks like this:
Code:
... called number <-- Variable name
0 1 2 ... 24 <-- Array index
... [ 0 | 0 | 0 | ... | 0 ] 0 <-- Memory value
Next, you enter your three for loops in main(). Inside the loops, gen() is called once. Inside gen(), a random number is generated - 16, and your code adds one to it making it 17. Since called[17] == 0, you set called[17] = 1;, and memory looks like this:
Code:
... called number <-- Variable name
0 1 2 ... 17 ... 24 <-- Array index
... [ 0 | 0 | 0 | ... | 1 | ... | 0 ] 0 <-- Memory value
In your for loops, gen keeps getting called, and each time it finds a new random number and sets that array value to 1. At some point, like maybe the 6th call to gen(), the random number is 24, and since you add 1, that makes 25. However, your array only goes from 0 to 24, so again you access memory passed the end of your array. Your code checks that array[25] == 0, but since that is really the number variable, which at this point is 25, your check returns false. In fact, whenever the random number generates 24, to which you add 1 to make 25, the if statement will always return false, because number is never 0 at that point. Look at the memory when the if statement is called with number = 25:
Code:
... called number <-- Variable name
0 1 2 ... 17 ... 24 <-- Array index
... [ 0 | 1 | 0 | ... | 1 | ... | 0 ] 25 <-- Memory value
Since the compiler thinks that number is called[25], it will always return false for if(called[25]==0).
So anyway, because of that, by the time gen() has been called 24 times, your memory looks like this:
Code:
... called number <-- Variable name
0 1 2 ... 17 ... 24 <-- Array index
... [ 0 | 1 | 1 | ... | 1 | ... | 1 ] ?? <-- Memory value
Notice that the array value at index 0 is still 0. This is because you add 1 to your random number, and so you never actually get 0. On the 25th call to gen, it tries to find a number from 1 to 25 that hasn't been called yet. The previous 24 calls to gen() set the numbers 1-24, so only 25 is left, but because that is passed the end of the array like I explained above, the if statement always returns false. The code can't find a suitable number, so it goes on forever.
The solution of course is to stop accessing data past the end of the array. In intial_called(), you should only set the values to 0 for indexes from 0 to less than 25. And in gen(), you should be generating random numbers from 0 to 24, NOT from 1-25.
Originally posted by stalker
mind showing me the correct source code ?
Sorry, I won't show you the correct source code, because then you wouldn't learn. That last paragraph should help you understand how to fix it if the novel above didn't. If you still have troubles then maybe somebody else can explain it better.