PDA

View Full Version : object-c 5 different number




Tehy
Jul 22, 2006, 01:51 PM
Hi,

I got 5 random number:

srandom(time(NULL));

luku = random() % 70 + 1;
luku2 = random() % 70 + 1;
luku3 = random() % 70 + 1;
luku4 = random() % 70 + 1;
luku5 = random() % 70 + 1;

What would be the best and easiest way to check that all numbers are different numbers?



Vlade
Jul 22, 2006, 02:19 PM
Firsts its going to be easier if you make them into an array, then its easy to add more numbers later and makes the code smaller and easier to manage

int luku[5];
int i; //loop counter
srandom(time(NULL));

for(i=0; i<5; i++) {
luku[i] = random() % 70 + 1;
}

Then to check them add this code

int j; //Second loop counter
for(i=0; i<5; i++) {
for(j=i+1;j<5;j++) {
if (loku[i] == loku[j]) //Then we have a duplicate
}
}

Tehy
Jul 23, 2006, 12:27 AM
Thanks! I will try that :)