populating arrays

This is a discussion on populating arrays within the C Programming forums, part of the General Programming Boards category; I am new to C. I am trying to make sure that the rows and column adjacent to the row ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    populating arrays

    I am new to C.

    I am trying to make sure that the rows and column adjacent to the row and column the user input are blocked( being populated by an 'X' behind the UI so a move can not be made there. This is to stay out of the line of the queens moves. Is my for loop correct? how can I display the 'X' to make sure that these spaces are being populated?
    Thanks,

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define SIZE 8 /* Size of board*/
    
    /* function prototype */
    void display(char board[] [SIZE]);
    int valid_moves(char board[] [SIZE], int moves[] [SIZE], char player);
    
    int main()
    {
          char board [SIZE] [SIZE] ={ 0 }; /* The board*/
          int moves[SIZE] [SIZE] ={0}; /* valid moves */
          int row = 0;
          int col = 0;
          char queenie = 'Q';     /* user selection*/
          int y =0;        /* column number*/
          int x =0;         /* row number*/
          char player = 'Q';
    
           /* empty all the squares */
          for(row = 0; row < SIZE; row++)
            for(col = 0; col < SIZE; col++)
              board[row] [col] = ' ';
    
    
    
          
          /*fflush(stdin);          /*flush the buffer: keyboard--stdin*/
          printf("please enter the row numer 0-7 and column number 0-7 to place the queen:");
          scanf("%d %d", &x,&y);
    
           board[x][y]='Q';
    		
    	   for(x=x; x-1==0 && x+1==7; x++)
    		{
    			x='X';
    		printf("board mark",board[x][y]);		
    		}
    		for(x= x; x-1==0 && x+1==7; x--)
    		{
    			x='X';
    					
    		}
    		for(y= y; y-1==0&&y+1==7; y++)
    		{
    		y='X';
    					
    		}
    		for(y= y; y-1==0&&y+1==7; y--)
    		{
    		y='X';
    				
    		}
    				
    	
    
          
          if(x<0 && y<0 || x>SIZE && y>SIZE)
          {
                printf(" That is an invalid entry!\n");
          }
         
    
    	display(board);
    
    }
    
    /* display board funtion definition*/
    void display(char board[] [SIZE])
    {
          int row = 0;      /* row index */
          int col = 0;      /* col index */
          char col_label = 'a';         /* column label */
    
          printf("\n ");    /*start top line */
          for(col = 0; col<SIZE; col++)
          printf("   %c", col_label+col);    /* display the top line*/
          printf("\n");     /*end of top line*/
    
    /*display  the intermediate rows*/
          for(row = 0; row < SIZE; row++)
          {
          printf("   +");
          for(col = 0; col<SIZE; col++)
          printf("---+");
          printf("\n%2d|",row + 1);
    
          for(col = 0; col< SIZE; col++)
          printf(" %c |", board[row][col]);   /* display counters in row*/
          printf("\n");
          }
    
          printf("   +");   /*start the bottom line*/
          for(col = 0; col<SIZE ; col++)
          printf("---+");   /*display the bottom line*/
          printf("\n");     /* end bottom line*/
          }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    This is a strange piece of code. In the loop you are using x as an index. In the start of the loop you are always setting x to 'X'. So the x++ in the loop makes no sense. So you are always printing the same board [x][y].

    Code:
    for(x=x; x-1==0 && x+1==7; x++)
    {
      x='X';
      printf("board mark",board[x][y]);
    }
    The same with the others. Usually one would introduce a new variable to use as variable in the loop.

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Also, x is an integer, you can not assign it a char value. atleast you can, from what i believe, it will assign the ascii value. You need to either make a second variable thats char data type, or like shiro says, create a second variable of type int to increment the for loop. You will then have to change the data type of x to char.

    p.s. ascii value of 'X' is 88.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Sorry is this better.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define SIZE 8 /* Size of board*/
    
    /* function prototype */
    void display(char board[] [SIZE]);
    int valid_moves(char board[] [SIZE], int moves[] [SIZE], char player);
    
    int main()
    {
          char board [SIZE] [SIZE] ={ 0 }; /* The board*/
          int moves[SIZE] [SIZE] ={0}; /* valid moves */
          int row = 0;
          int col = 0;
          char queenie = 'Q';     /* user selection*/
          int y =0;        /* column number*/
          int x =0;         /* row number*/
          char player = 'Q';
    	  int i =0;
    
           /* empty all the squares */
          for(row = 0; row < SIZE; row++)
            for(col = 0; col < SIZE; col++)
              board[row] [col] = ' ';
    
    
    
          
          /*fflush(stdin);          /*flush the buffer: keyboard--stdin*/
          printf("please enter the row numer 0-7 and column number 0-7 to place the queen:");
          scanf("%d %d", &x,&y);
    
           board[x][y]='Q';
    
    
    	   for(i=x; i-1==0 && i+1==7; i++)
    		{
    			i='X';
    			
    		}
    		for(i= x; i-1==0 && i+1==7; i--)
    		{
    			i='X';
    					
    		}
    		for(i= y; i-1==0&&i+1==7; i++)
    		{
    		i='X';
    					
    		}
    		for(i= y; i-1==0&&i+1==7; i--)
    		{
    		i='X';
    				
    		}
    				
    	
    
          
          if(x<0 && y<0 || x>SIZE && y>SIZE)
          {
                printf(" That is an invalid entry!\n");
          }
         
    
    	display(board);
    
    }
    
    /* display board funtion definition*/
    void display(char board[] [SIZE])
    {
          int row = 0;      /* row index */
          int col = 0;      /* col index */
          char col_label = 'a';         /* column label */
    
          printf("\n ");    /*start top line */
          for(col = 0; col<SIZE; col++)
          printf("   %c", col_label+col);    /* display the top line*/
          printf("\n");     /*end of top line*/
    
    /*display  the intermediate rows*/
          for(row = 0; row < SIZE; row++)
          {
          printf("   +");
          for(col = 0; col<SIZE; col++)
          printf("---+");
          printf("\n%2d|",row + 1);
    
          for(col = 0; col< SIZE; col++)
          printf(" %c |", board[row][col]);   /* display counters in row*/
          printf("\n");
          }
    
          printf("   +");   /*start the bottom line*/
          for(col = 0; col<SIZE ; col++)
          printf("---+");   /*display the bottom line*/
          printf("\n");     /* end bottom line*/
          }

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Wrong again, now in the for loop, you are assigning i = 'X' it wont work. i is type integer. You need to keep your for loop using x and make i type char. And keep your i = 'X'.
    The keyboard is the standard device used to cause computer errors!

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Better?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define SIZE 8 /* Size of board*/
    
    /* function prototype */
    void display(char board[] [SIZE]);
    int valid_moves(char board[] [SIZE], int moves[] [SIZE], char player);
    
    int main()
    {
          char board [SIZE] [SIZE] ={ 0 }; /* The board*/
          int moves[SIZE] [SIZE] ={0}; /* valid moves */
          int row = 0;
          int col = 0;
          char queenie = 'Q';     /* user selection*/
          int y =0;        /* column number*/
          int x =0;         /* row number*/
          char player = 'Q';
    	  char i ='X';
    
           /* empty all the squares */
          for(row = 0; row < SIZE; row++)
            for(col = 0; col < SIZE; col++)
              board[row] [col] = ' ';
    
    
    
          
          /*fflush(stdin);          /*flush the buffer: keyboard--stdin*/
          printf("please enter the row numer 0-7 and column number 0-7 to place the queen:");
          scanf("%d %d", &x,&y);
    
           board[x][y]='Q';
    						
    	   for(x=x; x-1==0 && x+1==7; x++)
    		{
    			i='X';
    			
    		}
    		for(x= x; x-1==0 && x+1==7; x--)
    		{
    			i='X';
    					
    		}
    		for(x= y; x-1==0&&x+1==7; x++)
    		{
    		i='X';
    					
    		}
    		for(x= y; x-1==0&&x+1==7; x--)
    		{
    		i='X';
    				
    		}
    				
    	
    
          
          if(x<0 && y<0 || x>SIZE && y>SIZE)
          {
                printf(" That is an invalid entry!\n");
          }
         
    
    	display(board);
    
    }
    
    /* display board funtion definition*/
    void display(char board[] [SIZE])
    {
          int row = 0;      /* row index */
          int col = 0;      /* col index */
          char col_label = 'a';         /* column label */
    
          printf("\n ");    /*start top line */
          for(col = 0; col<SIZE; col++)
          printf("   %c", col_label+col);    /* display the top line*/
          printf("\n");     /*end of top line*/
    
    /*display  the intermediate rows*/
          for(row = 0; row < SIZE; row++)
          {
          printf("   +");
          for(col = 0; col<SIZE; col++)
          printf("---+");
          printf("\n%2d|",row + 1);
    
          for(col = 0; col< SIZE; col++)
          printf(" %c |", board[row][col]);   /* display counters in row*/
          printf("\n");
          }
    
          printf("   +");   /*start the bottom line*/
          for(col = 0; col<SIZE ; col++)
          printf("---+");   /*display the bottom line*/
          printf("\n");     /* end bottom line*/
          }

  7. #7
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Yes, better, but there are still flaws. why in your for loop is there x = x. Whats the point? Also, why are you using multiple for loops that all make i = 'X' which was done by the inizialization?

    Check your personal messages, I have sent you something
    The keyboard is the standard device used to cause computer errors!

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    ok, so what's the problem now? it compiles and works as well, but one thing, arrays in C are from 0 to N-1, so the first col in your program is 0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. populating two dimensional arrays
    By garycastillo in forum C Programming
    Replies: 2
    Last Post: 04-05-2002, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21