I'm trying to write a program that can place numbers from 1 to 20 in a random and non-repetitive way to a 20 element array.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int i,j,A[20],flag;
srand(time(NULL));
for(i=0; i<20; i++){
flag=1;
A[i]=1+rand()%19;
for(j=0;j<20;j++){
if(A[i]==A[j]){
flag=0;
break;
}
}
if(flag=0){
continue;
}
else
printf("%d ",A[i]);
}
return 0;
}
It gives repeated output.