Thread: Bingo Card Logic (no repetition)

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    4

    Question Bingo Card Logic (no repetition)

    Hi everybody, good afternoon.


    • I made a code to the problem below (in blue), but it seems that my logic is in some hole. Already I revised, and do not understand why I still have repeated numbers. Can anyone give me strength? Thank you!!




    • The logic that I thought code is as follows: I fill in a field of the matrix, with "i" and "j" and compare it to previous ones through another FOR with "k" and "z". If this field that just fill is equal to a previous field, I go back a position (j = j-1) and start the FOR again. To do so we made use FOR`s 4 (one inside the other, to enter the matrix and make the comparison), four variables (i, j, k, z) and an "aux" that serves as a precondition for my stop.




    • Important: I made the code with a 4x4 matrix to test it. Then I will replicate for a 99x99 matrix.




    • The problem: Make a program to automatically generate numbers between 0 and 99 for a bingo card. Knowing that each card should contain 5 lines and 5 numbers, manages this data so as not to have repeated numbers in the chart.




    • The Code:

    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main () {
    
    
    	int M[4][4], i,j,k,z, aux = 0;
    
    
    	srand(time(NULL));
    
    
    	for(i=0;i<4;i++){
    		for(j=0;j<4;j++){
    		
    
    
    			aux = 0;
    			M[i][j] = i;
    	
    			for(k = i; k >= 0; k--){
    
    
    				if(aux == 1){
    					break;
    				}
    
    
    				for(z = j; z >= 0; z--){
    					if((M[k][z] == M[i][j]) && ((k != i) && (z != j))){
    						aux = 1;
    						j = j - 1;
    						break;
    					}
    				}
    			}
    		}
    	}
    			
    
    
    	printf("\n\n\n\n");
    
    
    	for(i=0;i<4;i++){
    		for(j=0;j<4;j++){
    			printf("%d\t", M[i][j]);
    		}
    		printf("\n");
    	}
    
    
    	return 0;
    }




  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Generally, the approach to take is to try and solve the problem just using a pencil and paper.

    Solve such problems as
    - how to pick unique numbers in the range 0 to 99
    - how to randomly position 'n' of 0 to 99 on the grid

    If you've no idea how you would solve it on paper, using a series of repeatable steps, then just randomly writing code in hopes of stumbling on a solution won't work.

    Hint: consider how to implement a shuffle.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    4
    I will follow your advice, Salem.

    In fact, I did what you said before. But I will try to do it again...

    Thanks!

    Quote Originally Posted by Salem View Post
    Generally, the approach to take is to try and solve the problem just using a pencil and paper.

    Solve such problems as
    - how to pick unique numbers in the range 0 to 99
    - how to randomly position 'n' of 0 to 99 on the grid

    If you've no idea how you would solve it on paper, using a series of repeatable steps, then just randomly writing code in hopes of stumbling on a solution won't work.

    Hint: consider how to implement a shuffle.

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    4
    Salem,


    I have just made my logic again (with my head fresh ) and worked ...


    I really would like to know how you think when you are building algorithms.


    I am starting to do this now (I am learning in C) and something that are trivial in human thinking are not so trivial in computer thinking.


    For example, I spent some time to build a code that could make the Product (C) of two matriz (A * B = C), once the rows and columns dont go in parallels.



    Quote Originally Posted by Salem View Post
    Generally, the approach to take is to try and solve the problem just using a pencil and paper.

    Solve such problems as
    - how to pick unique numbers in the range 0 to 99
    - how to randomly position 'n' of 0 to 99 on the grid

    If you've no idea how you would solve it on paper, using a series of repeatable steps, then just randomly writing code in hopes of stumbling on a solution won't work.

    Hint: consider how to implement a shuffle.
    Attached Images Attached Images Bingo Card Logic (no repetition)-img_0964-jpg 

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    4
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main () {
    
    
    
    
    	int M[5][5], i,j,k,z,aux = 1;
    
    
    	srand(time(NULL));
    
    
    	for(i=0;i<5;i++){
    		for(j=0;j<5;j++){
    			
    			aux = 1;
    			M[i][j] = rand() % 100;
    			for(k=0;k<5;k++){
    				for(z=0;z<5;z++){
    
    
    					if((i == k && j == z) || (((k*5)+z) > ((i*5)+j))){
    						aux = 1;
    					}
    					else if(M[i][j] == M[k][z]){
    						aux = 0;
    						break;
    					}
    				}
    			
    				if(aux == 0){
    					j = j -1;
    					break;
    				}
    			}	
    		}
    	}
    
    
    
    
    
    
    	printf("Matriz Completa:\n");
    	for(i=0;i<5;i++){
    		for(j=0;j<5;j++){
    			printf("%d\t", M[i][j]);
    		}
    		printf("\n");
    	}
    
    
    	return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that there are two general approaches to problems like these:
    • Enumerate the possible numbers, then do a (partial) shuffle to obtain the list. This tends to work well when the number of numbers required is close to the number of possible numbers, e.g., you are trying to generate 15 distinct numbers using a 20 sided die.
    • Generate a (pseudo)random number, then search the previously generated numbers to see if it has been generated. If so, discard and repeat, otherwise add the number to the list. This tends to work well when the number of numbers required is much smaller than the number of possible numbers, e.g., you are trying to generate 3 distinct numbers using a 20 sided die.

    You have chosen the latter, and this seems appropriate to me for a 4x4 matrix with 100 possible numbers. However, I note that if you want to extend this to a 99x99 matrix, it becomes impossible to have unique numbers unless you also increase the number of possible numbers to at least 9801. Perhaps you meant 9x9 matrix instead, but for 81 numbers required out of 100, the former approach might be better.

    Anyway, going with the latter approach, I would start with a bit of abstraction:
    Code:
    #define BINGO_CARD_N 4
    
    struct BingoCard {
        int entries[BINGO_CARD_N][BINGO_CARD_N];
    };
    
    void bingo_card_populate(struct BingoCard *card);
    void bingo_card_print(const struct BingoCard *card);
    The named constant allows you to modify the code more easily to deal with 4x4, 5x5, etc, and also makes your code more understandable. The struct and functions help you to think more clearly what you are dealing with and what different parts of your program do.

    Now, to implement bingo_card_populate, I would also declare another function:
    Code:
    int bingo_card_find(const struct BingoCard *card, int n);
    The above function would return 0 if n is not found in the card, and 1 if it is found. This abstraction helps you to work bit by bit on your program, i.e., instead of trying to code a giant multiply nested loop, you can code a simpler nested loop to search the matrix for n, and then call the function in bingo_card_populate to check if the newly generated number is a duplicate. Note that you may need to modify this to account for the fact that you do not need to search all the entries at first, or perhaps you could just give the entries an initial invalid value (but that would be inefficient).
    Last edited by laserlight; 02-15-2016 at 12:08 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-19-2014, 04:06 AM
  2. Having problem with a program for Bingo
    By Magmadragoon in forum C Programming
    Replies: 13
    Last Post: 05-04-2011, 07:20 PM
  3. Bingo with words
    By smitty007 in forum C Programming
    Replies: 19
    Last Post: 04-14-2009, 05:14 PM
  4. Bingo Card Program. Any input?
    By dukebdx12 in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2008, 03:00 PM

Tags for this Thread