I want to create an array which has 20 elements and each element holds different integer numbers from closed interval [0-19].
Here is the codeblock:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 20
int main() {
srand(time(NULL));
int array[SIZE]={0},arrayhold[SIZE]={0},hold,i;
for(i=0;i<SIZE;i++) {
hold = rand()%20+1;
if(arrayhold[hold]==0){
array[i]=hold;
arrayhold[hold]++;
}
else {
while(arrayhold[hold]!=0)
hold = rand()%20+1;
array[i]=hold;
}
}
for(i=0;i<SIZE;i++) {
printf("array[%d]=%d\n",i,array[i]);
}
}
When i run the program, output gives array[random]=1 for many times, but never gives array[random]=3 (just for example).
Aucun commentaire:
Enregistrer un commentaire