Created
November 20, 2019 21:36
-
-
Save yves-chevallier/18f58e458fe91ac860b8b170b6b83e5d to your computer and use it in GitHub Desktop.
Generate random students groups with some suspense...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generate random students groups with some suspense... | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
int randint(int max) | |
{ | |
return rand() % (max + 1); | |
} | |
int slow_print(char *str, unsigned int us) | |
{ | |
for (size_t i = 0; i < strlen(str); i++) { | |
printf("%c", str[i]); | |
fflush(stdout); | |
usleep(us); | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char names[100][64] = {0}; | |
// Random Seed | |
srand((argc > 2) ? atoi(argv[2]) : 0); | |
// Read students names | |
int count = 0; | |
while (fgets(names[count], sizeof(names[0]), stdin) == names[count] && strlen(names[count]) > 1) { | |
names[count][strcspn(names[count], "\n")] = 0; | |
count++; | |
} | |
// Shuffle students | |
for (int k = 0; k < count; k++) { | |
int r = randint(count - 1); | |
strcpy(names[count + 1], names[k]); | |
strcpy(names[k], names[r]); | |
strcpy(names[r], names[count + 1]); | |
} | |
// Show lovely teams | |
int group = (argc > 1) ? atoi(argv[1]) : 2; | |
int delay = (argc > 3) ? atoi(argv[3]) : 100000; | |
for (int k = 0; k < count; k++) { | |
int r = k % group; | |
if (r == 0) { | |
printf("%d. ", 1 + k / group); | |
slow_print(names[k], delay); | |
printf("%s", k < count - 1 ? " ♡ " : "\n"); | |
} | |
else if (r == group - 1) { | |
slow_print(names[k], delay); | |
printf("\n"); | |
} | |
else { | |
slow_print(names[k], delay); | |
printf("%s", k < count - 1 ? " ♡ " : "\n"); | |
} | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment