This is part of my code that I came across. I donβt understand why he is doing it wrong. I have an array where it stores numbers 0-25, which are cases. Numbers must be randomized and overwritten into an array. The only condition is that no number can be doulbes, there can only be one of this number. I do not ask you to make your code, but give me a hint or point me in the letters. I'm trying to learn:)
The problem is the second do loop. I can get numbers to be randomized, but I get doubles. I created a loop to check and fix this, but it does not work. The code really works, and doubling is still happening, and I don't understand why. It looks right to me. Please see, thanks (:
This is what I did initially (at the very end, where I am now):
int check_double = 0; int i = 0; int counter = 0; int array_adder = 0; int random_number = 0; int cases[] = { 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 }; float money[] = { 0.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,750000,100000,200000,300000,400000,500000,750000,1000000 }; //Randomize all case number and realine them in the array srand ( time(NULL) ); do { cases[counter]= rand() % 26; counter += 1; printf("%d\n", cases[counter]); } while (counter <= 25); //make sure there are no doubles in the array, just 0 - 25 and not a single number repeated twice do { check_double = 0; for (i = 0; i < counter; i++) { if (cases[counter] == cases[i]) { cases[counter] = rand()% 26; check_double == 1; } } } while (check_double != 0);
Currently, what I got after that was combing both loops and checking for doubling as the array goes. This is what I did, it still has doublings, and I'm not sure why, I just sent a goat with both combinations:
do { cases[counter]= rand() % 26; if (cases[counter]>=1); for(i=0;i<=counter;i++) if (cases[counter]==cases[i]) { cases[counter]=rand()% 26; } printf("%d\n",cases[counter]); counter+=1; }