Hi, I'm new here and am trying to write a c program that shuffles elements in an array in a random order. I have shuffled them in random order but I now require to not have the same two elements side by side in the array. How can I do this?
Code:
for(i=1; i<10; i++){
j = rand()%(i+1);
strcpy(t, s[j]);
strcpy(s[j], s[i]);
strcpy(s[i], t);
sscanf(s[i-1],"%s, %*s, %*s, %*s", type_1);
sscanf(s[i],"%s, %*s, %*s, %*s", type);
printf("%s\n", type_1);
printf("%s\n\n", type);
if((strcmp(type_1, type)) == 0){
printf("YESSSS");
j = rand()%(i+1);
sscanf(s[j],"%s, %*s, %*s, %*s", type_2);
while(strcmp(type_2, type) == 0){
printf("\n\nentered\n\n");
j = rand()%(i+1);
sscanf(s[j],"%s, %*s, %*s, %*s", type_2);
}
strcpy(t, s[j]);
strcpy(s[j], s[i]);
strcpy(s[i], t);
}
}