1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

BINGO game problem

Discussion in 'C' started by jarkeethan, Aug 18, 2009.

  1. jarkeethan

    jarkeethan New Member

    Joined:
    Jul 30, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    this a binggo game but im stuck with checking the numbers in the loop.
    i need an output that display a different random numbers without repeating any number.
    i made a checking function but I'm not sure if it is applicable to this program.:crazy:
    can anyone help me with this program,:surrender

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    int x[5][5];
    int CheckIfExist(int num)
    {
    int flag = 0, i, j;
    for (i=0;i<5;i++)
    for(j=0;j<5;j++)
    if (x[i][j]==num) {
    flag = 1;
    break;
    }
    return flag;
    }
    
    main()
    {
    int xx, ctr=0, b=0, i=0, n=0, g=0, o=0, w, q;
    clrscr();
    
    for(;;) {
    xx=random(75)+1;
    if(CheckIfExist(xx)==0) {
    if (xx>=1 && xx<16) {
    x[0][b]=xx;
    b++;
    ctr++;
    }
    if (xx>=16 && xx<31) {
    x[1][i]=xx;
    i++;
    ctr++;
    }
    if (xx>=31 && xx<46) {
    x[2][n]=xx;
    n++;
    ctr++;
    }
    if (xx>=46 && xx<61) {
    x[3][g]=xx;
    g++;
    ctr++;
    }
    if (xx>=61 && xx<76) {
    x[4][o]=xx;
    o++;
    ctr++;
    }
    }
    if(ctr>24)
    break;
    }
    
    for(w=0;w<5;w++)
    for(q=0;q<5;q++) {
    gotoxy((w*3)+1,q+1);
    printf("%d",x[w][q]);
    }
    
    getch();
    return 0;
    }
     
    Last edited by a moderator: Aug 18, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Please use code blocks when posting code; it makes code much easier to read.

    Looking through it though, it seems mostly OK. What does the program not do that you want?

    The only fault I could see with the code (without running it) is that you need to check that b,i,n,g,o don't exceed 4; once you've written to, for example, x[0][4], next time you get a number between 1 and 16 you should do nothing, otherwise you'll write to x[0][5] which will cause undefined behaviour.

    CheckIfExist() seems fine though. Why do you think it might not apply to this program? If you're going to eliminate duplicates you're going to need a mechanism of some kind to do that.
     
  3. jarkeethan

    jarkeethan New Member

    Joined:
    Jul 30, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    i try to use CheckIfExist() for checking that there is no duplicate no. in b,i,n,g,o but it always display a same number and also display 0...i want the get different numbers without repeating any number..
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    The reason for the zeroes is that you keep overflowing the end of the arrays. You aren't guaranteed anything about random numbers, that's the point, and you aren't guaranteed to get just as many numbers from 1 to 16 as you are in any other range, which your code assumes to be the case. So one or more of b,i,n,g,o are exceeding 4 and writing somewhere else in memory, which is a UB bug. This also means ctr reaches 24 before all numbers are filled in, hence the zeroes.

    So what you need to do as I already said is to add a check for each of b,i,n,g,o and do nothing if it's greater than 4, e.g.
    Code:
    			if (xx>=1 && xx<16 && b<5) 
    			{
    				x[0][b]=xx;
    				b++;
    				ctr++;
    			}
    
    With this change the code ran fine in Visual Studio 2008 and I got no duplicates or zeroes.
     

Share This Page