Thread: Arrays not behaving well

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    1

    Wink Arrays not behaving well

    I am a bit beginner in c language and I am creating a program to play tic tac toe randomly. Unfortunately some of the functions are not working well.
    Code:
    #include <stdio.h>
    int done1[8], done2[8], chance;
    void print(){
        int prnt[8];
        int i, j;
        for (i=0; i<9; i++)
            prnt[i] = 0;
        for (j=0; j<9; j++){
            if (done1[j]){
                   if (!chance)
                       prnt[j] = 'X';
                   else 
                       prnt[j] = 'O';
            } 
            else 
                if (!prnt[j])
                    prnt[j] = '-';
        }
        for (j=0; j<9; j++){
            if (done2[j]){
                   if (chance)
                       prnt[j] = 'X';
                   else 
                       prnt[j] = 'O';
            } 
            else 
                if (!prnt[j])
                    prnt[j] = '-';
        }
        printf("\n %c | %c | %c \n %c | %c | %c \n %c | %c | %c \n", prnt[0], prnt[1], prnt[2], prnt[3], prnt[4], prnt[5], prnt[6], prnt[7], prnt[8]);
    }
    void input_data(){
        int input;
        while(1){
            input = getchar();
            input -= 48;
            if (input < 1 || input > 9)
                continue;
            input--;
            if (!done1[input] && !done2[input]){
                done1[input] = 1;
                break;
            }
        }
        chance = 1;
    }
    int main (){
        int j;
        for (j=0; j<9; j++){
            done1[j]=0;
            done2[j]=0;
        }
        input_data();
        print();
    }
    and this
    Code:
    #include <stdio.h>
    int done[1][8], chance;
    void print(){
        int prnt[8];
        int i, j;
        for (i=0; i<9; i++)
            prnt[i] = 0;
        for (i=0; i<2; i++)
            for (j=0; j<9; j++){
                if (done[i][j]){
                    if (i==chance)
                        prnt[j] = 'X';
                    else 
                        prnt[j] = 'O';
                } 
                else 
                    if (!prnt[j])
                        prnt[j] = '-';
            }
        printf("\n %c | %c | %c \n %c | %c | %c \n %c | %c | %c \n", prnt[0], prnt[1], prnt[2], prnt[3], prnt[4], prnt[5], prnt[6], prnt[7], prnt[8]);
    }
    void input_data(){
        int input;
        while(1){
            input = getchar();
            input -= 48;
            if (input < 1 || input > 9)
                continue;
            input--;
            if (!done[0][input] && !done[1][input]){
                done[0][input] = 1;
                break;
            }
        }
        chance = 1;
    }
    int main (){
        int i, j;
        for (i = 0; i<2; i++)
            for (j=0; j<9; j++)
                done[i][j]=0;
        input_data();
        print();
    }
    This both are unexpectedly are having done[0][8] = 1 and done[1][0] = 1 and done1[8] = 1 without any reason. I was excepted to have simply the mark entered by the user example if user types 5 the middle one should have mark. Kindly help with this.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You simply aren't making your arrays large enough. In the declarations, the number between the square brackets indicates the actual number of elements, not the highest allowed index, which as you know is one less than the actual number of elements. So all those 8's need to be 9's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File not behaving that way I want it too:
    By jocdrew21 in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2014, 12:34 PM
  2. waitpid behaving strange...
    By mattholm in forum C Programming
    Replies: 6
    Last Post: 11-28-2011, 01:12 PM
  3. fprintf behaving strangely
    By cwmccart in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2008, 09:56 AM
  4. Printf... behaving Strange...
    By vsriharsha in forum C Programming
    Replies: 3
    Last Post: 04-02-2002, 02:38 AM
  5. while loop not behaving properly.
    By Dreamerv3 in forum C++ Programming
    Replies: 20
    Last Post: 01-08-2002, 05:51 PM

Tags for this Thread