Thread: invert an array

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    invert an array

    i have an 8x8 array is there a way to invert it. the way its assigned to the board element 0,0 is bottom left corner and i want to turn the board so 0,0 becomes the top most corner ie
    Code:
    // way it is now
    
    //7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7
    //6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7
    // etc etc
    //1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7
    //0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7
    
    //inverted
    //0,7 0,6 0,5 0,4 0,3 0,2 0,1 0,0
    //1,7 1,6 1,5 1,4 1,3 1,2 1,2 1,1
    // etc etc
    //6,7 6,6 6,5 6,4 6,3 6,2 6,1 6,0
    //7,7 7,6 7,5 7,4 7,3 7,2 7,1 7,0
    effectively i have turned the board 180 degrees
    many thanks
    coop

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    this is getting on my nerves now
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int i, j , array[8][8], count = 0;
    
        for (j = 0; j < 8; j++)
        {
            for (i = 0; i < 8; i ++)
            {
                array[i][j] = count;
                count++;
            }
        }
    
        for (j = 7; j >= 0; j--)//(j = 0; j <8; j++)// swap in
        {
            for (i = 0; i < 8; i ++)//(i = 7; i >= 0; i --)same here swap in
            {
                printf("%d\t", array[i][j]);
            }
            printf("\v\n");
        }
        return 0;
    }
    
    //i get the output
    //56 57 58 59 60 61 62 63
    //48 49 50 51 52 53 54 55
    //etc etc
    //8  9 10 11 12 13 14 15
    //0  1  2  3  4 5  6  7  8
    all cool if i change the second set of for loops to:
    Code:
        for (j = 0; j <8; j++)  //(j = 7; j >= 0; j--)//(j = 0; j <8; j++)// swap in
        {
            for (i = 7; i >= 0; i --) //(i = 0; i < 8; i ++)//(i = 7; i >= 0; i --)same here swap in
            {
                printf("%d\t", array[i][j]);
            }
            printf("\v\n");
        }
    
    // i get the output:
    //  7   6   5   4   3   2 1 0
    //15 14 13 12 11 10 9 8
    // etc etc
    //55 54 53 52 51 50 49 48
    //63 62 61 60 59 58 57 56
    Bingo just what i wanted!
    however
    Code:
    void draw_board(char board[][ROW_MAX + 1])
    {
        int column, row, temp_array[8][8];
        bool change_player = false; //whites go
    
        if (change_player == false)
        {
            for (row = ROW_MAX; row >= ROW_MIN; row--)
            {
                for (column = COLUMN_MIN; column <= COLUMN_MAX; column++)
                {
                    temp_array[column][row] = board[column][row];
                }
            }
        }
    
        for (row = ROW_MAX; row >= ROW_MIN; row--)
        {
            for (column = COLUMN_MIN; column <= 3; column++)
            {
                if (column == 0)
                {
                    printf("   *****************************************************************\n");
                }
                else if (row % 2 == 0)
                {
                    if (column % 2 == 0)
                    {
                        change_player ? printf(" %d *   %c   *********   %c   *********   %c   *********   %c   *********\n", row, board[0][row], board[2][row], board[4][row], board[6][row]) :
                        printf(" %d *   %c   *********   %c   *********   %c   *********   %c   *********\n", row, temp_array[0][row], temp_array[2][row], temp_array[4][row], temp_array[6][row]);
                    }
                    else
                    {
                        printf("   *       *********       *********       *********       *********\n");
                    }
                }
                else
                {
                    if (column % 2 == 0)
                    {
                        change_player ? printf(" %d *********   %c   *********   %c   *********   %c   *********   %c   *\n",row, board[1][row], board[3][row], board[5][row], board[7][row]) :
                        printf(" %d *********   %c   *********   %c   *********   %c   *********   %c   *\n",row, temp_array[1][row], temp_array[3][row], temp_array[5][row], temp_array[7][row]);
                    }
                    else
                    {
                        printf("   *********       *********       *********       *********       *\n");
                    }
                }
            }
        }
        printf("   *****************************************************************\n");
        printf("       0        1       2      3       4       5       6       7    \n");
    }
    the for loops in the if statment make no difference what so ever to how the pieces appear on the screen!!!
    How come?
    coop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to invert an inputted number?
    By george7378 in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2010, 01:03 PM
  2. Read a string and invert it
    By ekosix in forum C Programming
    Replies: 2
    Last Post: 05-12-2010, 08:16 AM
  3. Need to invert the output!
    By alvifarooq in forum C++ Programming
    Replies: 11
    Last Post: 09-23-2004, 10:46 AM
  4. Invert Pen/Line?
    By Aidman in forum Windows Programming
    Replies: 2
    Last Post: 10-13-2003, 03:54 PM
  5. 5 * 5 matrix invert
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2001, 08:26 AM

Tags for this Thread