dimanche 29 mai 2016

Randomizing nodes in linked list, C program

the following function is part of sort of a game with questions and answers. I have a problem with the randomization of the questions by their complexity-some questions appear more than once and the first also appears in every level, which should not happen. Please help!

typedef struct
{
    char question[300];
    char date[30], author[30], ansr1[80], ansr2[80], ansr3[80], ansr4[80];
    int id, correctAnsr, level;
} game;

typedef struct Node
{
    game data;
    struct Node* next;
}  node;

void printRandom1(node *head)
{
    if (isEmpty(head))
        return;

    srand(time(NULL));

    node *result = head->data.question;

    node *current = head;
    int n;
    for (n = 17; current != NULL; n++)
    {
        if (rand() % n == 0 && current->data.level == 0)
            result = current->data.question;
        current = current->next;
    }
    printf("%s\n", result);
    int i, ans;

    printf("1.-%s\n", result->data.ansr1);
    printf("2.-%s\n", result->data.ansr2);
    printf("3.-%s\n", result->data.ansr3);
    printf("4.-%s\n", result->data.ansr4);

    printf("Enter the correct answer: ");
    scanf("%d", &ans);
    if (ans == result->data.correctAnsr)
        printf("CORRECT ANSWER!\n");
    else
    {
        printf("WRONG ANSWER\n");
        printf("GAME OVER");
        printf("The correct answer is: %d\n", result->data.correctAnsr);
        return menu();
        exit(1);
    }
}




Aucun commentaire:

Enregistrer un commentaire