2nd array simple issue (IMPORTANT!)

This is a discussion on 2nd array simple issue (IMPORTANT!) within the C Programming forums, part of the General Programming Boards category; Hello,whats popin ? I got this 2nd array Code: for(i=0;i<GAME_BOARD_ROWS_SIZE;i++){ for(j=0;j<GAME_BOARD_COLUMNS_SIZE-1;j++){ if(game_board[i][j]==player_number){ cnt_player_paints_rows++; } if(game_board[i][j+1]!=player_number){ if(cnt_player_paints_rows>max) max=cnt_player_paints_rows; } else{ if(j+1==GAME_BOARD_ROWS_SIZE-1) ...

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

    2nd array simple issue (IMPORTANT!)

    Hello,whats popin ?

    I got this 2nd array


    Code:
    
           for(i=0;i<GAME_BOARD_ROWS_SIZE;i++){
              for(j=0;j<GAME_BOARD_COLUMNS_SIZE-1;j++){
                 if(game_board[i][j]==player_number){
                    cnt_player_paints_rows++;
                   }
                 if(game_board[i][j+1]!=player_number){
                    if(cnt_player_paints_rows>max)
                       max=cnt_player_paints_rows;
    
                   }
                 else{
                     if(j+1==GAME_BOARD_ROWS_SIZE-1)
                        cnt_player_paints_rows++;
                     }
                 if(game_board[j][i]==player_number){
                    cnt_player_paints_columns++;
                   }
                 if(game_board[j+1][i]!=player_number){
                    if(cnt_player_paints_columns>max)
                       max=cnt_player_paints_columns;
    
                       cnt_player_paints_columns=0;
                   }
                 else{
                     if(j+1==GAME_BOARD_COLUMNS_SIZE-1)
                        cnt_player_paints_columns++;
                     }
                 }
    
           switch(player_number){
    
            case 1: printf("\nThe longest path of the same painted nodes for Yellow is %d",max); break;
            case 2: printf("\nThe longest path of the same painted nodes for Green is %d",max); break;
            case 3: printf("\nThe longest path of the same painted nodes for Red is %d",max); break;
            case 4: printf("\nThe longest path of the same painted nodes for Black is %d",max); break;
            case 5: printf("\nThe longest path of the same painted nodes for Blue is %d",max); break;
            case 6: printf("\nThe longest path of the same painted nodes for Purpule is %d",max); break;
            }
           printf("\n\n");
    
           if(max>max_of_all){
              max_of_all=max;
              temp_winner=player_number;
           
              }
             }
    My problem is that i got 6 player,each has his number in a node in the array,what I want to do is count how much in total every player got and decied that biggest of all
    Please note that this 2nd array is ina while loop which changes the player number by 1 (playernumber++)

    Now I think I wrote it correctly,but when i full the array with numbers and hit enter in cmd,it gives the answer but cmd quits at the same second!

    Any thoughts?


    Original code :
    *The programe recieves player number and a row&column number,it sets the node with player number,if it already has a number in it then it gives 8 re-tries,if player 1 or 5 playing,it gives them a second turn ,if player doesnt want to play he/she enters -1 and the turn is passed to another player.
    There 6 players,an array of 100 nodes

    Code:
    
    #include <stdio.h>
    
    #define RETRIES_NUMBER 8
    #define GAME_BOARD_ROWS_SIZE 10
    #define GAME_BOARD_COLUMNS_SIZE 10
    #define ALL_PLAYERS 6
    #define NODES_TO_PAINT 100
    #define MAX_COLUMN 9
    #define MIN_COLUMN 0
    #define MAX_ROW 9
    #define MIN_ROW 0
    #define FIRST_PLAYER 1
    #define LAST_PLAYER 6
    #define FIRST_GIRL 1
    #define FIFTH_GIRL 5
    #define SUCCESSFUL_INPUT_TWO 2
    #define SUCCESSFUL_INPUT_THREE 3
    int main()
    {
      int row,column,player_number,cnt_total_inputs=0,cnt_tries=0,scanf_result,change_rolle;
      int max=0,cnt_player_paints_rows=0,cnt_player_paints_columns=0,max_of_all=0,temp_winner;
      int i,j;
      int game_board[GAME_BOARD_ROWS_SIZE][GAME_BOARD_COLUMNS_SIZE];
    
      for(i=0;i<GAME_BOARD_ROWS_SIZE;i++){
        for(j=0;j<GAME_BOARD_COLUMNS_SIZE;j++){
             game_board[i][j]=0;
           }
         }
      while(cnt_total_inputs<NODES_TO_PAINT){
       change_rolle=0;
       cnt_tries=0;
       printf("Please enter your number and the row and column: ");
       scanf_result=scanf("%d%d%d",&player_number,&row,&column);
       if(scanf_result!=SUCCESSFUL_INPUT_THREE){
         printf("Illegal input");
         return 1;
         }
       if(row==EOF){
          change_rolle=1;
         }
       if((change_rolle!=1)&&(player_number<FIRST_PLAYER||player_number>LAST_PLAYER||column<MIN_COLUMN||column>MAX_COLUMN||row<MIN_ROW||row>MAX_ROW)){
           printf("Illegal input");
           return 1;
         }
       if(change_rolle!=1&&game_board[row][column]==0){
              game_board[row][column]=player_number;
              cnt_total_inputs++;
              change_rolle=1;
         }
       if(change_rolle!=1&&game_board[row][column]!=0){
    
        printf("this node is already painted,You may retray again\n");
    
        while(change_rolle!=1&&cnt_tries<RETRIES_NUMBER){
    
            printf("\nYou have %d left to retray painting a node", RETRIES_NUMBER-cnt_tries);
            scanf_result=scanf("%d%d",&row,&column);
            if(scanf_result!=SUCCESSFUL_INPUT_TWO){
            printf("Illegal input");
            return 1;
            }
            if(row==EOF){
               break;
            }
            if((change_rolle!=1)&&(column<MIN_COLUMN||column>MAX_COLUMN||row<MIN_ROW||row>MAX_ROW)){
    
            printf("Illegal input");
            return 1;
            }
            if(change_rolle!=1&&game_board[row][column]==0){
    
             game_board[row][column]=player_number;
             cnt_total_inputs++;
             break;
           }
              cnt_tries++;
        }
        cnt_tries=0;
       }
    
       change_rolle=0;
       if(cnt_total_inputs<NODES_TO_PAINT)
       if(player_number==FIRST_GIRL||player_number==FIFTH_GIRL){
    
          printf("Just because you are a girl,you get a seconde time to play ... ...\n");
          scanf_result=scanf("%d%d",&row,&column);
          if(scanf_result!=SUCCESSFUL_INPUT_TWO){
             printf("Illegal input");
             return 1;
            }
          if(row==EOF){
            change_rolle=1;
            }
          if((change_rolle!=1)&&(column<MIN_COLUMN||column>MAX_COLUMN||row<MIN_ROW||row>MAX_ROW)){
              printf("Illegal input");
              return 1;
            }
          if(change_rolle!=1&&game_board[row][column]==0){
               game_board[row][column]=player_number;
               cnt_total_inputs++;
               change_rolle=1;
            }
          if(change_rolle!=1&&game_board[row][column]!=0){
              printf("this node is already painted,You may retray again\n");
    
            while(change_rolle!=1&&cnt_tries<RETRIES_NUMBER){
    
                  printf("\nYou have %d left to retray painting a node", RETRIES_NUMBER-cnt_tries);
                  scanf_result=scanf("%d%d",&row,&column);
                  if(scanf_result!=SUCCESSFUL_INPUT_TWO){
                  printf("Illegal input");
                  return 1;
                 }
                 if(row==EOF){
                    break;
                   }
                 if((change_rolle!=1)&&(column<MIN_COLUMN||column>MAX_COLUMN||row<MIN_ROW||row>MAX_ROW)){
                     printf("Illegal input");
                     return 1;
                    }
                 if(change_rolle!=1&&game_board[row][column]==0){
                    game_board[row][column]=player_number;
                    cnt_total_inputs++;
                    break;
                   }
                  cnt_tries++;
                 }
         cnt_tries=0;
          }
         }
    
       }
      printf("\n\n");
      player_number=1;
      while(player_number<=ALL_PLAYERS){
    
           for(i=0;i<GAME_BOARD_ROWS_SIZE;i++){
              for(j=0;j<GAME_BOARD_COLUMNS_SIZE-1;j++){
                 if(game_board[i][j]==player_number){
                    cnt_player_paints_rows++;
                   }
                 if(game_board[i][j+1]!=player_number){
                    if(cnt_player_paints_rows>max)
                       max=cnt_player_paints_rows;
    
                   }
                 else{
                     if(j+1==GAME_BOARD_ROWS_SIZE-1)
                        cnt_player_paints_rows++;
                     }
                 if(game_board[j][i]==player_number){
                    cnt_player_paints_columns++;
                   }
                 if(game_board[j+1][i]!=player_number){
                    if(cnt_player_paints_columns>max)
                       max=cnt_player_paints_columns;
    
                       cnt_player_paints_columns=0;
                   }
                 else{
                     if(j+1==GAME_BOARD_COLUMNS_SIZE-1)
                        cnt_player_paints_columns++;
                     }
                 }
    
           switch(player_number){
    
            case 1: printf("\nThe longest path of the same painted nodes for Yellow is %d",max); break;
            case 2: printf("\nThe longest path of the same painted nodes for Green is %d",max); break;
            case 3: printf("\nThe longest path of the same painted nodes for Red is %d",max); break;
            case 4: printf("\nThe longest path of the same painted nodes for Black is %d",max); break;
            case 5: printf("\nThe longest path of the same painted nodes for Blue is %d",max); break;
            case 6: printf("\nThe longest path of the same painted nodes for Purpule is %d",max); break;
            }
           printf("\n\n");
    
           if(max>max_of_all){
              max_of_all=max;
              temp_winner=player_number;
           }
              max=0;
              cnt_player_paints_rows=0;
              cnt_player_paints_columns=0;
              player_number++;
             }
            }
      switch(temp_winner){
    
            case 1: printf("The winner is the player with color Yellow"); break;
            case 2: printf("The winner is the player with color Green"); break;
            case 3: printf("The winner is the player with color Red"); break;
            case 4: printf("The winner is the player with color Black"); break;
            case 5: printf("The winner is the player with color Blue"); break;
            case 6: printf("The winner is the player with color Purpule"); break;
            }
      return 0;
      }
    Code:
    Inputs to try:
    
    
    1 0 0      0 1    2 0 2    2 0 3   2 0 4   2 0 5   2 0 6   2 0 7   1 0 8     0 9  3 -1 4
    1 1 0      1 1    2 1 2    2 1 3   2 1 4   2 1 5   2 1 6   2 1 7   1 1 8     1 9 
    3 2 0    3 2 1    1 2 2      2 3   5 2 4     2 5   5 2 6     2 7   4 2 8   6 2 9 
    3 3 0    3 3 1    1 3 2      3 3   5 3 4     3 5   5 3 6     3 7   4 3 8   6 3 9 
    6 4 0    6 4 1    2 4 2    2 4 3   6 4 4   6 4 5   2 4 6   2 4 7   6 4 8   6 4 9 
    1 5 0      5 1    2 5 2    2 5 3   2 5 4   2 5 5   2 5 6   2 5 7   1 5 8     5 9 
    1 6 0      6 1    2 6 2    2 6 3   2 6 4   2 6 5   2 6 6   2 6 7   1 6 8     6 9 
    3 7 0    3 7 1    1 7 2      7 3   5 7 4     7 5   5 7 6     7 7   4 7 8   6 7 9 
    3 8 0    3 8 1    1 8 2      8 3   5 8 4     8 5   5 8 6     8 7   4 8 8   6 8 9  5 -1 3    -1 4
    6 9 0    6 9 1    2 9 2    2 9 3   6 9 4   6 9 5   2 9 6   2 9 7   6 9 8   6 9 9
    
    
    
    1 0 0      0 1    2 0 2    2 0 3   2 0 4   2 0 5   2 0 6   2 0 7   1 0 8     0 9  3 -1 4
    1 1 0      1 1    2 1 2    2 1 3   2 1 4   2 1 5   2 1 6   2 1 7   1 1 8     1 9 
    3 2 0    3 2 1    1 2 2      2 3   5 2 4     2 5   5 2 6     2 7   4 2 8   6 2 9 
    3 3 0    3 3 1    1 3 2      3 3   5 3 4     3 5   5 3 6     3 7   4 3 8   6 3 9 
    6 4 0    6 4 1    2 4 2    2 4 3   6 4 4   6 4 5   2 4 6   2 4 7   6 4 8   6 4 9 
    1 5 0      5 1    2 5 2    2 5 3   2 5 4   2 5 5   2 5 6   2 5 7   1 5 8     5 9 
    1 6 0      6 1    8 6 2    2 6 3   2 6 4   2 6 5   2 6 6   2 6 7   1 6 8     6 9 
    3 7 0    3 7 1    1 7 2      7 3   5 7 4     7 5   5 7 6     7 7   4 7 8   6 7 9 
    3 8 0    3 8 1    1 8 2      8 3   5 8 4     8 5   5 8 6     8 7   4 8 8   6 8 9  5 -1 3    -1 4
    6 9 0    6 9 1    2 9 2    2 9 3   6 9 4   6 9 5   2 9 6   2 9 7   6 9 8   6 9 9
     
    
    
    
    
    
    
    1 0 0      0 1    2 0 2    2 0 3   2 0 4   2 0 5   2 0 6   2 0 7   1 0 8     0 9  3 -1 4
    1 1 0      1 1    2 1 2    2 1 3   2 1 4   2 1 5   2 1 6   2 1 7   1 1 8     1 9  3  1 9
      2 0    3 2 1    1 2 2      2 3   5 2 4     2 5   5 2 6     2 7   4 2 8   6 2 9 
    3 3 0    3 3 1    1 3 2      3 3   5 3 4     3 5   5 3 6     3 7   4 3 8   6 3 9 
    6 4 0    6 4 1    2 4 2    2 4 3   6 4 4   6 4 5   2 4 6   2 4 7   6 4 8   6 4 9 
    1 5 0      5 1    2 5 2    2 5 3   2 5 4   2 5 5   2 5 6   2 5 7   1 5 8     5 9 
    1 6 0      6 1    2 6 2    2 6 3   2 6 4   2 6 5   2 6 6   2 6 7   1 6 8     6 9 
    3 7 0    3 7 1    1 7 2      7 3   5 7 4     7 5   5 7 6     7 7   4 7 8   6 7 9 
    3 8 0    3 8 1    1 8 2      8 3   5 8 4     8 5   5 8 6     8 7   4 8 8   6 8 9  5 -1 3    -1 4
    6 9 0    6 9 1    2 9 2    2 9 3   6 9 4   6 9 5   2 9 6   2 9 7   6 9 8   6 9 9
    all rights reserved for Tamer Kadmany

    Thanks
    Last edited by Kadmany; 04-23-2011 at 01:09 AM. Reason: CopyRights

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To troubleshoot it, I would do two things:

    1) Add an assert line of code (or make your own), that will print out the value of i or j, if that variable ever equals the size of the row or the column. If you are using j+1 or i+1, then use the value of j+1 (or i+1) as your test. This is always the most likely cause of a seg fault when working with arrays - somehow, those indices find a way to sneak outside the boundary of the array.

    2) Temporarily, // mark out the while loop around your checking. Get your code right for just ONE player first - then after it's all working right for one player, go back to making it loop through all the players.

    Take it step by step, for now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple programming issue!
    By ke121885 in forum C Programming
    Replies: 5
    Last Post: 09-27-2009, 06:57 PM
  2. Simple issue please help
    By te5la in forum C++ Programming
    Replies: 5
    Last Post: 09-17-2008, 12:11 PM
  3. Simple issue with bytes
    By Nositi in forum C Programming
    Replies: 6
    Last Post: 03-25-2008, 11:06 AM
  4. Simple operator overloading issue
    By Desolation in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2007, 08:56 PM
  5. Simple issue but I am STUCK
    By jedispy in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2006, 01:02 AM

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