Bingo booklet in c

Asked

Viewed 1,499 times

-1

I was solving a college exercise on matrices and I ended up crashing.

Ex: Build a program to automatically generate numbers between 0 and 99 from a bingo card. Knowing that each card should contain 5 lines of 5 numbers, generate this data so that it does not have repeated numbers inside the cards. The program must display on the screen the generated card.

I tried to solve it this way :

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define n 5

int main ()
{ 
  int a[n][n],cont;

  srand(time(NULL));

      for (int i=0;i<n;i++)
    {
        for (int j=0;j<n;j++)
        {
            a[i][j]==-1;
        }
    }
    // Preencher a matriz com valores n repitidos o "do/while" compararia td os valores da matriz e caso tenha repeticao aumemta um contador q executa o looping

    for (int i=0;i<n;i++)
    {
        for (int j=0;j<n;j++)
        {
            do
            {   
                cont=0;
                a[i][j]=rand()%99;

                for (int l=0;l<n;l++)
                {
                    for (int c=0;c<n;c++)
                    {
                      if (a[i][j]==a[l][c] && (i!=l && j!=c))
                        cont++;
                    }
                }
             } while(cont!=0);
        }
    }



                for (int l=0;l<n;l++)
                {
                    for (int c=0;c<n;c++)
                    {
                       printf(" \t %d ", a[l][c]);
                    }
                    printf("\n");
                }

return 0;
}

But it didn’t work and I have no idea how to fix it.

2 answers

0

I made some changes to your code and it seems to be working

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
  int a[5][5], cont, i, j, c, l;

  srand(time(NULL));


    // Preencher a matriz com valores n repitidos o "do/while" compararia td os valores da matriz e caso tenha repeticao aumemta um contador q executa o looping

    for (i=0;i<5;i++){
        for (j=0;j<5;j++){
            do{   
                cont=0;
                a[i][j]=rand()%99;

                for (l=0;l<5;l++){
                    for (c=0;c<5;c++){
                      if (a[i][j]==a[l][c] && (i!=l && j!=c)){
                      cont++;
                      }
                    }
                }
             } while(cont!=0);
        }
    }

                for (l=0;l<5;l++)
                {
                    for (c=0;c<5;c++)
                    {
                       printf(" \t %d ", a[l][c]);
                    }
                    printf("\n");
                }
    return 0;
}
  • User158513, your answer may be useful! but it will help the questioner more if you comment on the code with the improvements made

0

Username 7873, I’ve set up a function that might help you. The idea is to NOT repeat the numbers ever. I assemble two lists: A list of all the numbers that will be drawn (this list always decreases in size with .Removeat) and a list that will receive the numbers that have already been drawn. In the case of BINGO are 75 NUMBERS because, are 15 numbers for each letter! Thus: The letter "B" goes from 1 to 15, the "I" goes from 16 to 30, the letter "N" goes from 31 to 45, the letter "G" goes from 46 to 60 and the letter "O" goes from 61 to 75. Then I REMOVE (.Removeat) the numbers from a list and draw with a number less (INDEX) the internal number of the array remains "unchanged" (those that have already been drawn fall out...). Detail... is in C#, but I think you will get an idea. The function "DRAW" will always receive 1 number less to draw...

public static List<Resultado> SorteiaNumeros(string StrNumero)
{
    List<int> strListaNumero = new List<int>();
    List<Resultado> strLista = new List<Resultado>();
    int intPosicaoSorteada;
    int IntContador = Convert.ToInt32(StrNumero);

    //Pegar todos os números que podem ser sorteados e colocar em uma lista.
    for (int i = 1; i <= IntContador; i++)
    {
        strListaNumero.Add(i);
    }

    // ir retirando os números da lista conforme o INDEX.
    do
    {
        intPosicaoSorteada = Convert.ToInt32(Sorteia(strListaNumero.Count.ToString()));

        strLista.Add(new Resultado
        {
            Posicao = (strLista.Count + 1).ToString(),
            Nome = Convert.ToString(strLista.Count + 1) + " - " + strListaNumero[intPosicaoSorteada - 1] + ".",
            Ordem = strListaNumero[intPosicaoSorteada - 1].ToString()
        });

        strListaNumero.RemoveAt(intPosicaoSorteada - 1);

    } while (strListaNumero.Count > 0);

    return strLista;
}

public static string Sorteia(string StrNumero)
{
    Random randNum = new Random();
    return randNum.Next(1, Convert.ToInt32(StrNumero) + 1).ToString();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.