Note that there are some explanatory texts on larger screens.

plurals
  1. POC programming - A array and a random number combined question?
    text
    copied!<p>this is the part of my code I'm having trouble with. I can't understand why its doing it wrong. I have an array where it stores numbers 0 - 25 which are cases. The numbers are to be randomized and overwritten into the array. Only condition is is that no number can be doulbes, there can only be one of that number. I'm not asking you to <em>do my code</em> but do hint me or point me in the write directions. I am trying to learn :)</p> <p>The problem lies within the second do loop. I can get the numbers to be randomized, but I get doubles. I have created a loop to check and fix this, but it's not working. The code does run, and doubles do still happen and I can't see why. It looks correct to me. Please look, thank you (:</p> <p>This is what I have done originally (at the very end is where I am at now):</p> <pre><code>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 &lt;= 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 &lt; counter; i++) { if (cases[counter] == cases[i]) { cases[counter] = rand()% 26; check_double == 1; } } } while (check_double != 0); </code></pre> <p>Currently, what I had achived after that was combing both loops and check for doubles as the array goes. This is what I made, it still has doubles and im not sure why, I only posted the cose with both loops combined:</p> <pre><code>do { cases[counter]= rand() % 26; if (cases[counter]&gt;=1); for(i=0;i&lt;=counter;i++) if (cases[counter]==cases[i]) { cases[counter]=rand()% 26; } printf("%d\n",cases[counter]); counter+=1; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload