lundi 22 novembre 2021

check if the numbers already given in a array

This is a program from a book that I'm studying. I don't get how this program keeps track of the numbers that have already been taken. The book is terse and I don't understand their explanation. Could someone please help me to understand the details of this code better? Specifically the part indicated in the code comment.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const int maxrange = 49;
const int maxballs = 6;

int rnd(int range);
void seedrnd(void);

int main()
{
    int numbers[maxrange];
    int i, b;

    printf("L O T T O  Z I E H U N G E N\n");
    seedrnd();

    for (i = 0; i < maxrange; i++)
    {
        numbers[i] = 0;
    }
    printf("Drueken sie eingabe fuer die Zahlen dieser Woche: ");
    getchar();

    printf("Es geht los\n");

    //This is the part I am stuck at: where the numbers in the array
    //are checked to see if the number has been used

    for (i = 0; i < maxballs; i++);
    {
        do
        {
            b = rnd(maxrange);
        } while (numbers[b - 1]);
        numbers[b - 1] = 1;
        printf("%i ", b);
    }

    printf("\n\nViel Glueck\n");
    return 0;
}

int rnd(int range)
{
    int r;
    r = rand() % range + 1;
    return (r);
}

void seedrnd(void)
{
    srand((unsigned)time(NULL));
}



Aucun commentaire:

Enregistrer un commentaire