Thread: Logic of my for loops.

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Question Logic of my for loops.

    I am having problems getting my matches counter to work properly. The program is meant to simulate a bingo game. When a number is matched, the counter should go up by one.. instead, it often increases by two or three, claiming that other locations in the array equal the number being checked.

    I believe the problem lies in the logic of my for loops:
    Code:
     		for (a = 0; a < 3; a++){		/* a is y dimension of array being checked */
    
    
    			for (b = 0; b < 9; b++){	/* b is x dimension of array being checked */
    		
    				if (Bingo_Numbers[x] == Bingo_Card[a][b]){
    
    					Match_Count++;
    					printf ("Match on number %2d\n", Bingo_Numbers[x]);
    					printf ("You now have %2d numbers left to match!\n", 15 - Match_Count);
    					printf ("Bingo Card Location x: %d  y: %d\n", b,a);
    					}
    			}
    		}
                            
    	}



    here is the full source code so you can see where the variables are coming from:
    Code:
    /* Draws the bingo numbers */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void main(void)
    
    {
    	int Bingo_Card[9][3] = {  1,  2,  3,  4,  5,  6,  7,  8,  9,
    				 10, 11, 12, 13, 14, 15, 16, 17, 18,
    				 81, 82, 83, 84, 85, 86, 87, 88,  0 };
    
    	int  Bingo_Numbers[90];
    	int  Random;
    	int  x = 0;
    	int  y;
    
                    int  a,b,c;
    	int  Match_Count = 0;
    
    
       	srand((unsigned)time(NULL));
    
    
    	for (c = 0; Match_Count < 15; x++){ 		/*Matching Numbers Loop*/
    
    
    	printf ("\nPress any key to draw another number");
    	getche();
    
    
    	Random = rand() % 90 + 1;
    
    		for (y = 0; y < 90; ){
                    	
    		if (Random == Bingo_Numbers[y])
    			{
    			 Random = rand() % 90 + 1;
    			 y = 0;
                    	}
    		else y++;               
    
                    }
    
    
                    Bingo_Numbers[x] = Random;
    		printf ("\nDrawn Number %2d:  %2d\n", x + 1, Bingo_Numbers[x]);
    
    
    		for (a = 0; a < 3; a++){		/* a is y dimension of array being checked */
    
    
    			for (b = 0; b < 9; b++){	/* b is x dimension of array being checked */
    		
    				if (Bingo_Numbers[x] == Bingo_Card[a][b]){
    
    					Match_Count++;
    					printf ("Match on number %2d\n", Bingo_Numbers[x]);
    					printf ("You now have %2d numbers left to match!\n", 15 - Match_Count);
    					printf ("Bingo Card Location x: %d  y: %d\n", b,a);
    					}
    			}
    		}
                            
    	}
    
    
    
    
    	printf ("BINGO!");
    	
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    not sure if this is the problem, but you've defined your array for cards:
    Code:
    Bingo_Card[9][3]
    but you've accessed these integers as if you defined them
    Code:
    Bingo_Card[3][9]
    try either changing the [3] and [9] around in the declaration or change the loop so a is a<9 and b is b<3. Not sure if thats the prob coz not 100% on how it assigns the memory and all that in 2D arrays, but give it a go, may be the prob.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    THANKS

    Thank you so much mate,

    thats exactly where the problem was.

    i've been frying my brain all afternoon over this!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main(void)
    This is wrong, main returns an int and nothing else. It should be
    int main ( void )

    >getche();
    getche is defined in conio.h, yet you don't include that header.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  2. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM