Thread: Help printing 4x4 grid

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    22

    Help printing 4x4 grid

    Hi guys, I'm having some trouble printing out a 4x4 grid. A 1x1 grid is supposed to look like this:

    Code:
    +----+
    |    |
    |    |
    |    |
    +----+
    I've come close but I'm not quite there yet.


    Code:
    int main() {
    
    	row_count = 2;
    	col_count = 2;
    	
    	strcpy(row0NoX, "-----+");
    	strcpy(row1NoX, "     |");
    	strcpy(row2NoX, "     |");
    	strcpy(row3NoX, "     |");
    	strcpy(row4NoX, "-----+");
    	
    	for (row = 0; row < row_count; row++) {
    		
    		for (tiny_row = 1; tiny_row < N_ROWS; tiny_row++) {
    
    			printf("%c", row0NoX[strlen(row0NoX)-1]);
    			printf("%c", row1NoX[strlen(row1NoX)-1]);
    			printf("%c", row2NoX[strlen(row2NoX)-1]);
    			printf("%c", row3NoX[strlen(row3NoX)-1]);
    			printf("%c", row4NoX[strlen(row4NoX)-1]);
    			
    			
    			for (col = 0; col < col_count; col++) {
    				
    				switch (board[row][col]) {
    					case 0:
    						switch (tiny_row) {
    							case 1:
    								printf("%s", row0NoX);
    								break;
    							case 2:
    								printf("%s", row1NoX);
    								break;
    							case 3:
    								printf("%s", row2NoX);
    								break;
    							case 4:
    								printf("%s", row3NoX);
    								break;
    							case 5:								
    								printf("%s", row4NoX);
    								break;
    							default:
    								printf("Error");
    								break;
    						}						
    						break;	
    				}				
    			}
    			printf("\n");			
    		}		
    	}	
    	
    	return (0);
    }
    What I'm getting if I set the grid to 2x2 is this:
    Code:
    +|||+-----+-----+
    +|||+     |     |
    +|||+     |     |
    +|||+     |     |
    +|||+-----+-----+
    +|||+-----+-----+
    +|||+     |     |
    +|||+     |     |
    +|||+     |     |
    +|||+-----+-----+
    The problem with this is its not printing the | at the beginning of the grid but +s. Plus, it's also printing out a space between the two grids which might be because of the \n I put in there...
    Last edited by Watabou; 03-09-2011 at 11:31 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Watabou View Post
    Code:
    			printf("%c", row0NoX[strlen(row0NoX)-1]);
    			printf("%c", row1NoX[strlen(row1NoX)-1]);
    			printf("%c", row2NoX[strlen(row2NoX)-1]);
    			printf("%c", row3NoX[strlen(row3NoX)-1]);
    			printf("%c", row4NoX[strlen(row4NoX)-1]);
    Next time, please give enough code to compile. You were missing all your variable definitions, making our lives much more difficult. That also leads me to believe that you are using global variables, which are evil. Stop using them if you are.

    Now, those statements up there need to be executed at the beginning of their appropriate line only. You're seeing the entire right column of a 1x1 grid being printed at the beginning of each row. Move them into the correct case of your switch (tiny_row), and only print them when you're on column zero.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    Oh sorry about that.

    Yeah I do have global variables but I guess I will move them inside main.


    Okay so you mean I should include each of them in the printf statements in each of the cases or somewhere else?
    Last edited by Watabou; 03-09-2011 at 12:00 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That was the tough one. There's one or two more changes you will have to make, but they should be pretty simple, so I'll leave them up to you.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    Okay so is this what you mean because I'm still not there yet.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define N_ROWS 6
    
    char board[25][25];
    char row0NoX[8];
    char row1NoX[8];
    char row2NoX[8];
    char row3NoX[8];
    char row4NoX[8];
    
    char command[100];
    int input;
    
    int row;
    int col;
    int tiny_row;
    int row_count;
    int col_count;
    
    int main() {
    
    	row_count = 2;
    	col_count = 2;
    	
    	strcpy(row0NoX, "-----+");
    	strcpy(row1NoX, "     |");
    	strcpy(row2NoX, "     |");
    	strcpy(row3NoX, "     |");
    	strcpy(row4NoX, "-----+");
    	
    	for (row = 0; row < row_count; row++) {
    		
    		for (tiny_row = 1; tiny_row < N_ROWS; tiny_row++) {
    			
    			
    			for (col = 0; col < col_count; col++) {
    				
    				switch (board[row][col]) {
    					case 0:
    						printf("%c", row0NoX[strlen(row0NoX)-1]);
    						switch (tiny_row) {
    							case 1:
    								printf("%s", row0NoX);
    								break;
    							case 2:
    								printf("%s", row1NoX);								
    								break;
    							case 3:
    								printf("%s", row2NoX);							
    								break;
    							case 4:
    								printf("%s", row3NoX);								
    								break;
    							case 5:								
    								printf("%s", row4NoX);
    								break;
    							default:
    								printf("Error");
    								break;
    						}						
    						break;	
    				}				
    			}
    			printf("\n");			
    		}		
    	}	
    	return (0);
    }
    I included it when col is 0...but do I put others in the tiny row switch loop?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, you didn't include it when col is zero. You included it when board[row][col] is zero. I don't even know what board[row][col] is for; you can probably delete the declaration and the switch statement with only one case.

    There was a whole block of printf("%c"... statements. You need to move each of those inside the appropriate case of the switch (tiny_row), and only do that if col == 0.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Any reason why you've defined five distinct strings?

    After all, in your design, rows 1 & 5 are the same, as are rows 2, 3, & 4.

    Also, do you want the joints between rows and columns to repeat like:

    Code:
    +----++----+
    |    ||    |
    |    ||    |
    |    ||    |
    +----++----+
    +----++----+
    |    ||    |
    |    ||    |
    |    ||    |
    +----++----+
    or prettier:

    Code:
    +----+----+
    |    |    |
    |    |    |
    |    |    |
    +----+----+
    |    |    |
    |    |    |
    |    |    |
    +----+----+

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an 4x4 grid and storing it in an array?
    By Watabou in forum C Programming
    Replies: 10
    Last Post: 03-02-2011, 03:03 AM
  2. Another hard C problem
    By alexbcg in forum C Programming
    Replies: 12
    Last Post: 11-23-2010, 08:03 AM
  3. 4x4 grid in C
    By amorvisa in forum C Programming
    Replies: 7
    Last Post: 10-17-2007, 11:13 PM
  4. More Windows Trouble
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 04-12-2005, 10:02 AM
  5. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 05:38 PM