Thread: Compare multiple variables

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    Compare multiple variables

    Hi
    I have 17 variables, that all need to be unique in value. I'd like to avoid doing
    Code:
    if(a==b||a==c||a==d)
    etc.
    Is it possible?
    cheers

  2. #2
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    There is probably some elegant c way to do it, but you could store the values in an array, and then use two for loops, comparing the values. Do you just need to test if they are unique or do you need to change the values if they are not?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    It depends what type of variables.

    If they're 1-byte characters, you could always store them in an array, and then (assuming 8-bit long) only do three compares:
    Code:
    unsigned long ext = a * 0x0101010101010101ul; /* extends byte value */
    char *ptr = array; /* your array */
    
    if (*(unsigned long *)&ptr == ext && *(unsigned long *)&(ptr + 8) == ext && *(unsigned char *)&(ptr + 16) == a) {
        /* body */
    }
    It's not tested, but something like this should work. Alternately, you could use a loop if this isn't what you want/they're bigger than 1 byte values.

    If they're not in array, the most efficient way is probably manually comparing, although I'm probably missing something. .

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    How are you generating the values for the variables? (Where do they come from?)
    What is the range of values?

    First and foremost, why do you have 17 separate variables. Why not an array?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    With that many, whether you have them in an array or not, the only sensible/efficient way of doing it is to put them into an array first. Otherwise you've looking at 50-100 or so individual hard-coded compares at least, which is just ridiculous.
    Then you just sort the array and check for the uniqueness of adjacent values.

    However it also depends on what you plan to do if a duplicate is found. If you only care about the existence of a duplicate then the above can be used as-is. Otherwise you'll need to make it an array containing a struct what has the value and some indicator of where it came from.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Thanks for the replies
    Basically, they all hold positions on a 9x9 grid, so variable p1_a_coord (one variable I'm using) can hold A1 through to J9. They are changed by the user, so a simple printf telling them that certain variables are the same can be used. I've had a look at arrays, but I still don't understand them all that much. I know you start with array[9][9], which gives a 9x9 array, but thats about it...

  7. #7
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    1) Using multiple variables in place of arrays will result in unmanageable code and painful coding. So stop what you're doing and a wrap your head around simple array usage.
    2) Tell us the full requirements of your program. From a high level (ultimate goal, what the program 'does') and a low level (what data types/logic you plan on using)

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Ok.
    I'm making a battleship game in console (Purely because I can ) I have options to setup ships for player 1 and 2. Haven't coded the option to begin firing yet, but that should be simple enough. There are 5 ships for each player, Aircraft carrier with length 5, battleship length 4, submarine length 3, cruiser length 3 and destroyer length 2. I have started the code, can post it if you want it?

  9. #9
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Aha, I'll take you on your word about this not being homework. I can think of lots of different ways to implement this. But go ahead and show your code.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Trust me, its not. I just got looking at a function that created the 9x9 grid, and got tinkering

    Code:
    
    /* 5 types of ship: Aircraft Carrier(5), Battleship(4), Submarine(3), Cruiser(3), Destroyer(2) */
    
    
    #include <stdio.h>
    //NEED TO FIND A WAY TO COMPARE 17 VARIABLES TO CHECK FOR UNIQUE VALUES!!!//
    int main()
    {
    	int p1_a_y1, p1_a_y2, p1_a_y3, p1_a_y4, p1_a_y5, p1_b_y1, p1_b_y2, p1_b_y3, p1_b_y4, p1_s_y1, p1_s_y2, p1_s_y3, p1_c_y1, p1_c_y2, p1_c_y3, p1_d_y1, p1_d_y2, p2_a_y1, p2_a_y2, p2_a_y3, p2_a_y4, p2_a_y5, p2_b_y1, p2_b_y2, p2_b_y3, p2_b_y4, p2_s_y1, p2_s_y2, p2_s_y3, p2_c_y1, p2_c_y2, p2_c_y3, p2_d_y1, p2_d_y2;
    	/* These are y positions */
    	char p1_a_x1, p1_a_x2, p1_a_x3, p1_a_x4, p1_a_x5, p1_b_x1, p1_b_x2, p1_b_x3, p1_b_x4, p1_s_x1, p1_s_x2, p1_s_x3, p1_c_x1, p1_c_x2, p1_c_x3, p1_d_x1, p1_d_x2, p2_a_x1, p2_a_x2, p2_a_x3, p2_a_x4, p2_a_x5, p2_b_x1, p2_b_x2, p2_b_x3, p2_b_x4, p2_s_x1, p2_s_x2, p2_s_x3, p2_c_x1, p2_c_x2, p2_c_x3, p2_d_x1, p2_d_x2;
    	/* These are x postions, one for each section of the corresponding ship */
    	char p1_a_pos, p1_b_pos, p1_s_pos, p1_c_pos, p1_d_pos, p2_a_pos, p2_b_pos, p2_s_pos, p2_c_pos, p2_d_pos;
    	/* These are either horizontal or vertical */
    	char p1_a_coord1[2], p1_a_coord2[2], p1_a_coord3[2], p1_a_coord4[2], p1_a_coord5[2], p1_b_coord1[2], p1_b_coord2[2], p1_b_coord3[2], p1_b_coord4[2], p1_s_coord1[2], p1_s_coord2[2], p1_s_coord3[2], p1_c_coord1[2], p1_c_coord2[2], p1_c_coord3[2], p1_d_coord1[2], p1_d_coord2[2];
    	char p2_a_coord1[2], p2_a_coord2[2], p2_a_coord3[2], p2_a_coord4[2], p2_a_coord5[2], p2_b_coord1[2], p2_b_coord2[2], p2_b_coord3[2], p2_b_coord4[2], p2_s_coord1[2], p2_s_coord2[2], p2_s_coord3[2], p2_c_coord1[2], p2_c_coord2[2], p2_c_coord3[2], p2_d_coord1[2], p2_d_coord2[2];
    	/* These are to check if ships overlap or not using sprintf() */
    	int grid_y=1, y_loc, menu, p1_ship, p2_ship, a;
    	char grid_x='A', x_loc, c, buffer[3], grid[9][9];
    	
    	printf("\n###### WELCOME TO BATTLESHIPS! ######\n\n");
    	/*Main Menu*/
    	do
    	{
    		
    		printf("### MAIN MENU ###");
    		printf("\n1) View Grid");
    		printf("\n2) Setup Player 1 Ships");
    		printf("\n3) Setup Player 2 Ships");
    		printf("\n4) Start Firing!");
    		printf("\n5) Quit\n");
    		scanf("%i", &menu);
    				
    		switch(menu)
    		{
    			case 1: /* Print the grid to play on */
    				grid_y=1;
    				grid_x='A';
    				for(grid_y=1;grid_y<10;grid_y++)
    				{
    					for(grid_x='A';grid_x<'J';grid_x++)
    					{
    						printf("%c%i ",grid_x,grid_y);
    					}
    					printf("\n");
    				}
    			printf("\n");
    			break;
    			
    			case 2: /* PLayer 1 setup */
    				do{
    					printf("\n### Player 1 Setup ###");
    					printf("\n1) Aircraft Carrier");
    					printf("\n2) Battleship");
    					printf("\n3) Submarine");
    					printf("\n4) Destroyer");
    					printf("\n5) Patrol Boat");
    					printf("\n6) Check Boat Alignment");
    					printf("\n7) Main Menu\n");
    					scanf(" %i", &p1_ship);
    					switch(p1_ship)
    					{
    						case 1: /* Player 1 Aircraft Carrier */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p1_a_x1);
    							if (p1_a_x1>'J'||p1_a_x1<'A')
    							{
    								printf("\nInvalid choice!\n");
    								break;
    							}
    							
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p1_a_y1);
    							if(p1_a_y1<1||p1_a_y1>9)
    							{
    								printf("\nInvalid choice!\n");
    								break;
    							}
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p1_a_pos);
    							if (p1_a_pos=='h'||p1_a_pos=='H')
    							{
    								p1_a_x2=p1_a_x1+1;
    								p1_a_x3=p1_a_x1+2;
    								p1_a_x4=p1_a_x1+3;
    								p1_a_x5=p1_a_x1+4;
    								
    								p1_a_y2=p1_a_y1;
    								p1_a_y3=p1_a_y1;
    								p1_a_y4=p1_a_y1;
    								p1_a_y5=p1_a_y1;
    								
    								if(p1_a_x5>='K')
    								{
    									printf("\nSelected position is out of bounds!\n");
    								}
    							}
    							else if (p1_a_pos=='v'||p1_a_pos=='V')
    							{
    								p1_a_y2=p1_a_y1+1;
    								p1_a_y3=p1_a_y1+2;
    								p1_a_y4=p1_a_y1+3;
    								p1_a_y5=p1_a_y1+4;
    								
    								p1_a_x2=p1_a_x1;
    								p1_a_x3=p1_a_x1;
    								p1_a_x4=p1_a_x1;
    								p1_a_x5=p1_a_x1;
    								
    								if(p1_a_y5>=10)
    								{
    									printf("\nSelected position is out of bounds!\n");
    								}
    								
    							}
    							else
    							{
    								printf("\nInvalid choice!\n");
    							}
    						sprintf(p1_a_coord1,"%c%i",p1_a_x1,p1_a_y1);
    						sprintf(p1_a_coord2,"%c%i",p1_a_x2,p1_a_y2);
    						sprintf(p1_a_coord3,"%c%i",p1_a_x3,p1_a_y3);
    						sprintf(p1_a_coord4,"%c%i",p1_a_x4,p1_a_y4);
    						sprintf(p1_a_coord5,"%c%i",p1_a_x5,p1_a_y5);
    						printf("%s, %s, %s, %s, %s",p1_a_coord1,p1_a_coord2,p1_a_coord3,p1_a_coord4,p1_a_coord5);
    						break;
    						
    						case 2: /* Player 1 Battleship */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p1_b_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p1_b_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p1_b_pos);
    							if (p1_b_pos='h')
    							{
    								p1_b_x2=p1_b_x1+1;
    								p1_b_x3=p1_b_x1+2;
    								p1_b_x4=p1_b_x1+3;
    								
    								p1_b_y2=p1_b_y1;
    								p1_b_y3=p1_b_y1;
    								p1_b_y4=p1_b_y1;
    								
    							}
    							else if(p1_b_pos='v')
    							{
    								p1_b_y2=p1_b_y1+1;
    								p1_b_y3=p1_b_y1+2;
    								p1_b_y4=p1_b_y1+3;
    								
    								p1_b_x2=p1_b_x1;
    								p1_b_x3=p1_b_x1;
    								p1_b_x4=p1_b_x1;
    								
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						sprintf(p1_b_coord1,"%c%i",p1_b_x1,p1_b_y1);
    						sprintf(p1_b_coord2,"%c%i",p1_b_x2,p1_b_y2);
    						sprintf(p1_b_coord3,"%c%i",p1_b_x3,p1_b_y3);
    						sprintf(p1_b_coord4,"%c%i",p1_b_x4,p1_b_y4);
    						printf("%s, %s, %s, %s",p1_b_coord1,p1_b_coord2,p1_b_coord3,p1_b_coord4);
    						break;
    						
    						case 3: /* Player 1 Submarine */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p1_s_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p1_s_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p1_s_pos);
    							if (p1_s_pos='h')
    							{
    								p1_s_x2=p1_s_x1+1;
    								p1_s_x3=p1_s_x1+2;
    								
    								p1_s_y2=p1_s_y1;
    								p1_s_y3=p1_s_y1;
    							}
    							else if(p1_s_pos='v')
    							{
    								p1_s_y2=p1_s_y1+1;
    								p1_s_y3=p1_s_y1+2;
    								
    								p1_s_x2=p1_s_x1;
    								p1_s_x3=p1_s_x1;
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						sprintf(p1_s_coord1,"%c%i",p1_s_x1,p1_s_y1);
    						sprintf(p1_s_coord2,"%c%i",p1_s_x2,p1_s_y2);
    						sprintf(p1_s_coord3,"%c%i",p1_s_x3,p1_s_y3);
    						printf("%s, %s, %s, %s",p1_s_coord1,p1_s_coord2,p1_s_coord3);
    						break;
    					
    						case 4: /* Player 1 Cruiser */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p1_c_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p1_c_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p1_c_pos);
    							if (p1_c_pos='h')
    							{
    								p1_c_x2=p1_c_x1+1;
    								p1_c_x3=p1_c_x1+2;
    								
    								p1_c_y2=p1_c_y1;
    								p1_c_y3=p1_c_y1;
    							}
    							else if(p1_c_pos='v')
    							{
    								p1_c_y2=p1_c_y1+1;
    								p1_c_y3=p1_c_y1+2;
    								
    								p1_c_x2=p1_c_x1;
    								p1_c_x3=p1_c_x1;
    
    
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						sprintf(p1_c_coord1,"%c%i",p1_c_x1,p1_c_y1);
    						sprintf(p1_c_coord2,"%c%i",p1_c_x2,p1_c_y2);
    						sprintf(p1_c_coord3,"%c%i",p1_c_x3,p1_c_y3);
    						printf("%c, %c, %c, %c",p1_c_coord1,p1_c_coord2,p1_c_coord3);
    						break;
    						
    						case 5: /* Player 1 Destroyer */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p1_d_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p1_d_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p1_d_pos);
    							if (p1_d_pos='h')
    							{
    								p1_d_x2=p1_d_x1+1;
    								
    								p1_d_y2=p1_d_y1;
    							}
    							else if(p1_d_pos='v')
    							{
    								p1_d_y2=p1_d_y1+1;
    								
    								p1_d_x2=p1_d_x1;
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						sprintf(p1_d_coord1,"%c%i",p1_d_x1,p1_d_y1);
    						sprintf(p1_d_coord2,"%c%i",p1_d_x2,p1_d_y2);
    						printf("%c, %c, %c, %c",p1_d_coord1,p1_d_coord2);
    						break;
    						
    						case 6: /* Player 1 boat check */
    							if()
    							{
    							
    							}
    						break;
    					}
    			}while (p1_ship!=7);
    			break;
    			
    			case 3: /* Player 2 Setup */
    				do{
    					printf("\n### Player 2 Setup ###");
    					printf("\n1) Aircraft Carrier");
    					printf("\n2) Battleship");
    					printf("\n3) Submarine");
    					printf("\n4) Destroyer");
    					printf("\n5) Patrol Boat");
    					printf("\n6) Boat Alignment Check");
    					printf("\n7) Main Menu\n");
    					scanf(" %i", &p2_ship);
    					switch(p2_ship)
    					{
    						case 1: /* Player 2 Aircraft carrier */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p2_a_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p2_a_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p2_a_pos);
    							if (p2_a_pos='h')
    							{
    								p2_a_x2=p2_a_x1+1;
    								p2_a_x3=p2_a_x1+2;
    								p2_a_x4=p2_a_x1+3;
    								p2_a_x5=p2_a_x1+4;	
    							}
    							else if (p2_a_pos='v')
    							{
    								p2_a_y2=p2_a_y1+1;
    								p2_a_y3=p2_a_y1+2;
    								p2_a_y4=p2_a_y1+3;
    								p2_a_y5=p2_a_y1+4;
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						break;
    						
    						case 2: /* Player 2 Battleship */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p2_b_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p2_b_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p2_b_pos);
    							if (p2_b_pos='h')
    							{
    								p2_b_x2=p2_b_x1+1;
    								p2_b_x3=p2_b_x1+2;
    								p2_b_x4=p2_b_x1+3;	
    							}
    							else if(p2_b_pos='v')
    							{
    								p2_b_y2=p2_b_y1+1;
    								p2_b_y3=p2_b_y1+2;
    								p2_b_y4=p2_b_y1+3;
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						break;
    						
    						case 3: /* Player 2 Submarine */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p2_s_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p2_s_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p2_s_pos);
    							if (p2_s_pos='h')
    							{
    								p2_s_x2=p2_s_x1+1;
    								p2_s_x3=p2_s_x1+2;
    							}
    							else if(p2_s_pos='v')
    							{
    								p2_s_y2=p2_s_y1+1;
    								p2_s_y3=p2_s_y1+2;
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						break;
    					
    						case 4: /* Player 2 Cruiser */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p2_c_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p2_c_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p2_c_pos);
    							if (p2_c_pos='h')
    							{
    								p2_c_x2=p2_c_x1+1;
    								p2_c_x3=p2_c_x1+2;	
    							}
    							else if(p2_c_pos='v')
    							{
    								p2_c_y2=p2_c_y1+1;
    								p2_c_y3=p2_c_y1+2;
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						break;
    						
    						case 5: /* Player 2 Destroyer */
    							printf("\nEnter starting X co-ordinate (A-J): ");
    							scanf(" %c", &p2_d_x1);
    							printf("\nEnter starting Y co-ordinate (1-9): ");
    							scanf(" %i", &p2_d_y1);
    							
    							printf("\nHorizontal(h) or vertical(v) alignment: ");
    							scanf(" %c", &p2_d_pos);
    							if (p2_d_pos='h')
    							{
    								p2_d_x2=p2_d_x1+1;	
    							}
    							else if(p2_d_pos='v')
    							{
    								p2_d_y2=p2_d_y1+1;
    							}
    							else
    							{
    								printf("\nNot a valid selection.\n");
    							}
    						break;
    						
    						case 6: /* Player 2 Boat check */
    						
    						break;
    					}
    			}while (p2_ship!=7);
    			break;
    			
    			case 4:
    				printf("Let's play battleships!\n\n");
    			break;
    			
    		}
    	}
    	while(menu!=5);
    	printf("Quitting...\n");
    }

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Trying to do this without using a matrix array[9][9] for the board is COMPLETELY NUTS. Honestly.

    If you want to learn to program, you have to learn some syntax, etc, beyond just declaring some ints and using loops and ==. What you are doing now looks like someone trying to make a table with some sticks and a roll of twine. Perhaps not impossible, but why bother? There are screws, hammers, boards, available.

    The best thing you can do for yourself right now is write a short program using a 2D array and play with that until you get the idea.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by MK27 View Post
    Trying to do this without using a matrix array[9][9] for the board is COMPLETELY NUTS. Honestly.
    Hehe maybe I am completely nuts?
    But anyways I will give that a go. Thanks!

  13. #13
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Well, it's a great start, but there are some problems. One, the code is largely monolithic. You haven't used any functions to help you, and thus you can't reuse any of the code. An example of this player 1 setup and player 2 setup. You have two very large blocks of code, that are essentially identical. Whenever you have 2 chunks of code that are nearly identical it's a red flag that you should use a function. Also you have used 17 separate variables (34 really) instead of using arrays or structs. This is going to make moving forward difficult. I seriously applaud your effort but you are going to start hitting roadblocks left and right. I think you need a to make a decision:
    1) I want to just get the program 'working' (sort of , kinda , maybe)
    2) I want to get better at coding and rewrite the code using functions / arrays and improve my 'style'

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quackers View Post
    Hehe maybe I am completely nuts?
    But anyways I will give that a go. Thanks!
    Here's something to get you started:

    Code:
    #include <stdio.h>
    
    #define DIM 9
    #define CARRIER 1
    #define BATTLESHIP 2
    
    void displayBoard (int board[DIM][DIM]) {
    	int i, j;
    
    	for (i = 0; i < DIM; i++) {
    		for (j = 0; j < DIM; j++)
    			printf("%d ", board[i][j]);
    		printf("\n");
    	}
    }
    
    int main (void) {
    	int board[DIM][DIM] = { { 0 } };
    	int i;
    
    // add carrier vertically
    	for (i = 2; i < 7; i++) board[i][2] = CARRIER;
    
    // add battleship horizontally
    	for (i = 4; i < 8; i++) board[5][i] = BATTLESHIP;
    
    	displayBoard(board);
    
    	return 0;
    }
    You could distinguish between players by using odd and even numbers, or you could make a more elaborate board using two chars for each square. You probably want functions like this:

    Code:
    int addVertically (int board[9][9], int shipID, int size, int bow[2]);
    int addHorizontally (int board[9][9], int shipID, int size, int bow[2]);
    So "bow" would be a coordinate to start at. The function would check to make sure there's nothing there, or the ship goes off the board, and return -1 if it can't be placed as requested. These could correspond to player choices (add vertically, add horizontally, then enter starting coordinate).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by javaeyes View Post
    2) I want to get better at coding and rewrite the code using functions / arrays and improve my 'style'
    like I said, I'm just doing it to mess around, so I want to actually learn it! I guess I need to look at making functions and learning arrays.

    Quote Originally Posted by MK27 View Post
    Here's something to get you started:

    Code:
    #include <stdio.h>
    
    #define DIM 9
    #define CARRIER 1
    #define BATTLESHIP 2
    
    void displayBoard (int board[DIM][DIM]) {
        int i, j;
    
        for (i = 0; i < DIM; i++) {
            for (j = 0; j < DIM; j++)
                printf("%d ", board[i][j]);
            printf("\n");
        }
    }
    
    int main (void) {
        int board[DIM][DIM] = { { 0 } };
        int i;
    
    // add carrier vertically
        for (i = 2; i < 7; i++) board[i][2] = CARRIER;
    
    // add battleship horizontally
        for (i = 4; i < 8; i++) board[5][i] = BATTLESHIP;
    
        displayBoard(board);
    
        return 0;
    }
    I don't understand what this function actually does, particularly the bit
    Code:
    int board[DIM][DIM] = { { 0 } };
    Can you give me some help on using arrays for starters? Point me to some guides maybe? I've lost my C coders guide

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple nonlocal variables
    By Furious5k in forum C++ Programming
    Replies: 19
    Last Post: 08-24-2011, 09:01 AM
  2. comparing multiple variables..
    By myrddin in forum C Programming
    Replies: 9
    Last Post: 10-16-2007, 07:03 PM
  3. Compare Struct Variables
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 06-17-2007, 12:06 PM
  4. Multiple variables
    By Kaho in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 04:32 AM
  5. multiple instances and variables
    By FOOTOO in forum Windows Programming
    Replies: 1
    Last Post: 04-07-2005, 10:54 AM