In Short, I am trying to fill the array list[11] with 10 random generated numbers from 0-9 while not duplicating any numbers in that array. If it finds a duplicate number it gets a new random number and starts the check all over again. I filled the array with the number 20 as a place holder to start off with.
When I step through the code with the debugger with break points it executes like I expect and all duplicate numbers are caught and replaced. But as I run the program normally 'build and go' it adds duplicate numbers in every now and then. This is part of a bigger program I am making to deal out cards and to check if it has already delt a card depending on it's number.
I know it is not the best way of zero terminating an int array but I don't think that is the problem.
-Lars
When I step through the code with the debugger with break points it executes like I expect and all duplicate numbers are caught and replaced. But as I run the program normally 'build and go' it adds duplicate numbers in every now and then. This is part of a bigger program I am making to deal out cards and to check if it has already delt a card depending on it's number.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, const char * argv[]) {
srand(clock());
int list[11] = {20,20,20,20,20,20,20,20,20,20,'\0'};
int randNumberRoll, i, count;
count = 0;
do{
randNumberRoll = rand() %10; // creates a random number 0-9
for (i=0; i < 9; i++) //finds the next space and increments count
{
if (list[count] != 20)
count++;
}
do{
for (i = 0; i <= count; i++) // Checks to see if the rand number is already in the list[]
{
if (randNumberRoll != list[i])
{
;
}
else{ // if it finds a duplicate number it resets i to 0 and rolls a new rand number.
i = 0;
randNumberRoll = rand() %10;
}
}
}while(i < count);
list[count] = randNumberRoll; // adds the rand number to the next spot in list[]
printf("count is %d and the list number is %d\n", count, list[count]); // prints the results
} while(count < 9); // keeps going till the list array is full.
return 0;
}
I know it is not the best way of zero terminating an int array but I don't think that is the problem.
-Lars