Thread: Newb Help: Full Arrays and Functions

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9

    Question Newb Help: Full Arrays and Functions

    Hey,

    I'm trying to make a simple game that has a 6x6 grid of numbers 1-36, it asks the player to submit 6 numbers, and for each number it will eliminate the row and column that number was in so none of those numbers can be selected after that.

    I'm having trouble understanding how to transfer or reference an entire array as opposed to just a single number out of one (if that's possible at all), and how to get those to work with functions.

    My coding does load up for me, and it works more or less correctly except it doesn't do the elimination part, which is where the full arrays are being used.

    Here's the coding as I have it now, any and all feedback is greatly appreciated.

    Code:
    /* simple little math game that takes six numbers,
     each selection eliminates the row and column from which it was selected.
     This project was begun mid-morning Jan 18, 2008 */
    
    #include <stdio.h>
    
    void elim( int *subNum, int *row1, int *row2, int *row3, int *row4, int *row5, int *row6, int *col1, int *col2, int *col3, int *col4, int *col5, int *col6 );
    void show_grid ( int *r1, int *r2, int *r3, int *r4, int *r5, int *r6 );
    
    int main() 
    { 	
    	int currentNumber, number1, number2, number3, number4, number5, number6, guess, total, x;
    	char name[256];
    /* This is setting up the grid, so that each number is a member of a column and a row. */
    
    	int row1[6] = {1, 2, 3, 4, 5, 6};
    	int row2[6] = {7, 8, 9, 10, 11, 12};
    	int row3[6] = {13, 14, 15, 16, 17, 18};
    	int row4[6] = {19, 20, 21, 22, 23, 24};
    	int row5[6] = {25, 26, 27, 28, 29, 30};
    	int row6[6] = {31, 32, 33, 34, 35, 36};
    	int col1[6] = {1, 7, 13, 19, 25, 31};
    	int col2[6] = {2, 8, 14, 20, 26, 32};
    	int col3[6] = {3, 9, 15, 21, 27, 33};
    	int col4[6] = {4, 10, 16, 22, 28, 34};
    	int col5[6] = {5, 11, 17, 23, 29, 35};
    	int col6[6] = {6, 12, 18, 24, 30, 36};
    
    
    /* This is the number that the game will guess.  It isn't supposed to change. */
    
    	guess = 111;
    	
    	
    /* This section introduces the idea of the game */	
    	printf( "Hi, What is your name?\n" );
    	fgets ( name, 256, stdin );
    
    	/* Just getting rid of the default \n at the end for later */
    		for ( x = 0; x < 256; x++ )
    		{
        		if ( name[x] == '\n' )
        		{
            		name[x] = '\0';
            		break;
        		}
    		}
    	printf( "I want to play a game.\n" );
    	getchar();
    	printf( "I will show you a grid of numbers; you pick any number, and I will eliminate the row and column that number was in.\n" );
    	getchar();
    	printf( "you choose six numbers, and I will guess the sum of those numbers before you even pick the first, okay? \n" );
    
    
    
    		show_grid(row1, row2, row3, row4, row5, row6);
    
    		
    
    	
    	printf( "I think that the total of your six numbers will be %d.\n", guess  );
    	getchar();
    	printf( "So enter your first number, cowboy.\n" );
    	scanf( "%d", &number1 );
    	
    	currentNumber = number1;
    
    
    elim(&currentNumber, row1, row2, row3, row4, row5, row6, col1, col2, col3, col4, col5, col6);
    
    /* This is to make sure the correct number is stored in memory for later (ref: elim loop) */	
    	number1 = currentNumber;
    
    		show_grid( row1, row2, row3, row4, row5, row6);
    	
    	printf( "Enter your second number\n" );
    	scanf( "%d", &number2 );
    	currentNumber = number2;
    
    	
    elim(&currentNumber, row1, row2, row3, row4, row5, row6, col1, col2, col3, col4, col5, col6);
    
    	number2 = currentNumber;
    
    		show_grid( row1, row2, row3, row4, row5, row6);
    	
    	printf( "Enter your third number\n" );
    	scanf( "%d", &number3 );
    	currentNumber = number3;
    	
    elim(&currentNumber, row1, row2, row3, row4, row5, row6, col1, col2, col3, col4, col5, col6);
    
    	number3 = currentNumber;
    
    		show_grid( row1, row2, row3, row4, row5, row6);
    
    	printf( "Enter your fourth number\n" );
    	scanf( "%d", &number4 );
    	currentNumber = number4;
    	
    elim(&currentNumber, row1, row2, row3, row4, row5, row6, col1, col2, col3, col4, col5, col6);
    
    	number4 = currentNumber;
    
    		show_grid( row1, row2, row3, row4, row5, row6);
    
    	printf( "Enter your fifth number\n" );
    	scanf( "%d", &number5 );
    	currentNumber = number5;
    	
    elim(&currentNumber, row1, row2, row3, row4, row5, row6, col1, col2, col3, col4, col5, col6);
    
    	number5 = currentNumber;
    
    		show_grid( row1, row2, row3, row4, row5, row6);
    
    	printf( "Enter your final number\n" );
    	scanf( "%d", &number6 );
    	currentNumber = number6;
    	
    elim(&currentNumber, row1, row2, row3, row4, row5, row6, col1, col2, col3, col4, col5, col6);
    
    	number6 = currentNumber;
    	
    /* This part just adds the numbers together, and shows the player the numbers
     they chose so they can do it themselves if they'd like */
    	total = number1 + number2 + number3 + number4 + number5 + number6;
    	printf( "well %s\n", name );
    	printf( "you chose the numbers %d, %d, %d, %d, %d and %d", number1, number2, number3, number4, number5, number6 );
    	getchar();
    	printf( "The total of those numbers are %d\n", total );
    
    	getchar();
    
    	if(total == guess)
    		printf( "Ha! I guessed right! I win!\n" );
    	
    	else 
    		printf( "Wow, I guess I'm not as good at this game as I thought I was.\n  Good Job %s, you beat me.\n", name );
    	
    	getchar();
    	
    	
    	return 0;
     }
    
    
    
    
    /* This is the function to display the grid.  It should update itself based on what numbers have been eliminated. */
    void show_grid (int *r1, int *r2, int *r3, int *r4, int *r5, int *r6)
    {
    	printf( "-------------------------------\n" );
    	printf( "| %d | %d | %d | %d | %d | %d |\n", r1[0], r1[1], r1[2], r1[3], r1[4], r1[5]);
    	printf( "-------------------------------\n" );
    	printf( "| %d | %d | %d | %d | %d | %d |\n", r2[0], r2[1], r2[2], r2[3], r2[4], r2[5] );
    	printf( "-------------------------------\n" );	
    	printf( "| %d | %d | %d | %d | %d | %d |\n", r3[0], r3[1], r3[2], r3[3], r3[4], r3[5] );
    	printf( "-------------------------------\n" );
    	printf( "| %d | %d | %d | %d | %d | %d |\n", r4[0], r4[1], r4[2], r4[3], r4[4], r4[5] );
    	printf( "-------------------------------\n" );
    	printf( "| %d | %d | %d | %d | %d | %d |\n", r5[0], r5[1], r5[2], r5[3], r5[4], r5[5] );
    	printf( "-------------------------------\n" );
    	printf( "| %d | %d | %d | %d | %d | %d |\n", r6[0], r6[1], r6[2], r6[3], r6[4], r6[5] );
    	printf( "-------------------------------\n" ); 
    	
    }	
    	
    
    /* This eliminates the rows and columns of numbers based on what the player has selected */	
    void elim( int *subNum, int *row1, int *row2, int *row3, int *row4, int *row5, int *row6, int *col1, int *col2, int *col3, int *col4, int *col5, int *col6 )
     {
    int validNum = 0, number, counter;
    
     while (validNum != 1) {
    	
    			if( *subNum == *col1 ) {
    			*col1 = 0, 0, 0, 0, 0, 0; 
    			validNum = 1;
    				}
    			
    			else if( *subNum == *col2 ) {
    				*col2 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    			
    			else if( *subNum == *col3 ) {
    				*col3 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    			
    			else if( *subNum == *col4 ) {
    				*col4 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    			
    			else if( *subNum == *col5 ) {
    				*col5 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    			
    			else if( *subNum == *col6 ) {
    				*col6 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    			else {
    			printf( "That number is not a valid selection. Pick a new one\n" );
    			getchar();
    			printf( "Enter your number\n" );
    			scanf( "%d", &number );
    			*subNum = number;
    			} 
    			
    		}
    
    
    validNum = 0;
    
    
    while (validNum != 1) {
    
    			if( *subNum == *row1 ) {
    				*row1 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    			
    			else if( *subNum == *row2 ) {
    				*row2 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    			
    			else if( *subNum == *row3 ) {
    				*row3 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    				
    			else if( *subNum == *row4 ) {
    				*row4 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    				
    			else if( *subNum == *row5 ) {
    				*row5 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				}
    			
    			else if( *subNum == *row6 ) {
    				*row6 = 0, 0, 0, 0, 0, 0;
    				validNum = 1;
    				break;
    				}
    			else {
    			printf( "That number is not a valid selection. Pick a new one\n" );
    			getchar();
    			printf( "Enter your number\n" );
    			scanf( "%d", &number );
    			*subNum = number;
    			} 
    					
    }
    						
    validNum = 0;
    }
    Nothing like jumping in headfirst in the deep end to learn a programming language. Heh.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can have multi-dimension arrays in C, you're not limited to just 1 dimension.

    I suggest storing the array in 2 dimensions, rows and columns.

    Code:
    int row1[6] = {1, 2, 3, 4, 5, 6};
    int row2[6] = {7, 8, 9, 10, 11, 12};
    int row3[6] = {13, 14, 15, 16, 17, 18};
    int row4[6] = {19, 20, 21, 22, 23, 24};
    int row5[6] = {25, 26, 27, 28, 29, 30};
    int row6[6] = {31, 32, 33, 34, 35, 36};
    int col1[6] = {1, 7, 13, 19, 25, 31};
    int col2[6] = {2, 8, 14, 20, 26, 32};
    int col3[6] = {3, 9, 15, 21, 27, 33};
    int col4[6] = {4, 10, 16, 22, 28, 34};
    int col5[6] = {5, 11, 17, 23, 29, 35};
    int col6[6] = {6, 12, 18, 24, 30, 36};
    Could be written as:
    Code:
    #define GRID_ROWS 6
    #define GRID_COLUMNS 6
    
    int grid[GRID_ROWS][GRID_COLUMNS];
    size_t r = 0, c = 0, total = 1;
    
    for(r = 0; r < GRID_ROWS; r++)
    {
        for(c = 0; c < GRID_COLUMNS; c++)
        {
            grid[r][c] = total;
            ++total;
        }
    }
    Allowing for a grid of variable size.


    Code:
    for ( x = 0; x < 256; x++ )
    {
        if ( name[x] == '\n' )
        {
            name[x] = '\0';
            break;
        }
    }
    Don't hardcode array sizes, this could also be simply written as:
    Code:
    char * nl = NULL;
    
    if((nl = strchr(name, '\n')) != NULL)
        *nl = '\0';
    Code:
    *col2 = 0, 0, 0, 0, 0, 0;
    Doesn't do what you think it does.

    As for if the numbers are eliminated, set their value to -1 or 0, something that you won't encounter. Since it's a grid you can use row, column style coordinates.

    As for showing the grid, that's much like the initialization of the grid as I stated above.
    Last edited by zacs7; 01-31-2008 at 04:44 AM.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    The above is good advice, get used to working with 2dimensional arrays and you will, as i have, find them very useful indeedy-do.

    one thing to always bear in mind is that the 'Vertical' or up/down co-ordinate is represented first and the Horizontal co-ordinate second.

    i.e grid_Coordinate[VERT][HORIZ]
    i say this because i always tend to picture coordinates as Horiz, Vert (x , y) and has often led me to msitakes when using 2d array

  4. #4
    Registered User
    Join Date
    Jan 2008
    Location
    Calgary, Alberta
    Posts
    9
    Thanks for the advice.

    I had decided to stick to single dimensions since I didn't have a clue how to eliminate both the row and column on a 2d grid. Though the coding zacs7 provided makes quite a bit of sense to me so I will try to use that.

    the thing I'm having the most trouble with is setting those rows and columns to 0/-1.
    I'm trudging through the tutorials here, and on several other sites but I haven't come across anything that mentions how to rewrite en entire array at once. Or, in the case of the newfangled 2D array, how to eliminate 6 of thoe variables at once.

    Do I have to individually enter each value one at a time?

    Thanks again.

  5. #5

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    /* to adjust an entire row in a 2D array */
    
    int RowToBeSet = 2;  
    int row = RowToBeSet;
    
    for(col = 0; col < maxColumnNum; c++)
         array[row][col] = ValueToSet;
    A similar for loop with rows this time, will adjust all the column elements you want reset.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM