Thread: putting symbols center of board

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    95

    putting symbols center of board

    I try to put 4 symbols on a user-sized board. Firstly, it works for `n x n` but when I want to convert `m x n` I can't get expected result. It works for some tryings but not for all. For example:
    Original algorithm:
    Code:
     
                    table[center][center - 1] = p_symbol;
                    table[center - 1][center] = p_symbol;
                    table[center][center] = c_symbol;
                    table[center - 1][center - 1] = c_symbol;
    Code:
        Expected result:
        row = 4, col = 8
          abcdefgh
         1........
         2...xo...
         3...ox...
         4........
         Output result:
           abcdefgh
         1........
         2........
         3...xo...
         4...ox...
        
         Expected result:
        row = 12, col = 6
           abcdef
         1......
         2......
         3......
         4......
         5......
         6..xo..
         7..ox..
         8......
         9......
        10......
        11......
        12......
        Output result:
          abcdef
         1......
         2......
         3......
         4......
         5......
         6....xo
         7....ox
         8......
         9......
        10......
        11......
        12......
    Code:
    Code:
        #include <iostream>
        #include <iomanip>
        using namespace std;
        
        char const c_symbol = 'x';
        char const p_symbol = 'o';
        char const what_symbol = '.';
        
        void show(char const table[][100], short const row_size, short const col_size)
        {
            cout << endl << setw(3);
            for(short i = 0 ; i < col_size ; ++i)
                cout << static_cast<char>(i + 'a');
            cout << endl;
            
            for(short i = 0; i < row_size; ++i)
            {
                cout << setw(2) << i + 1;
                for(short j = 0; j < col_size; j++)
                    cout << table[i][j];
                cout << endl;
            }
            cout << endl;
        }
        
        int main()
        {
            int row_size;
            int col_size;
            char table[100][100];
            
            cin >> row_size >> col_size;
            
            short center = 0;
            for(short i = 0; i < row_size; ++i)
                for(short j = 0; j < col_size; ++j)
                {
                    table[i][j] = what_symbol;
                }
            if (row_size < col_size)
            {
                center = col_size / 2;
                if (col_size % 2 == 0)
                {
                    table[center - 1][center - 1] = p_symbol;
                    table[center - 2][center] = p_symbol;
                    table[center - 1][center] = c_symbol;
                    table[center - 2][center - 1] = c_symbol;
                }
                else
                {
                    table[center][center - 1] = p_symbol;
                    table[center - 1][center] = p_symbol;
                    table[center][center] = c_symbol;
                    table[center - 1][center - 1] = c_symbol;
                }
            }
            else
            {
                center = row_size / 2;
                if (row_size % 2 == 0)
                {
                    table[center][center - 2] = p_symbol;
                    table[center - 1][center - 1] = p_symbol;
                    table[center][center - 1] = c_symbol;
                    table[center - 1][center - 2] = c_symbol;
                }
                else
                {
                    table[center][center - 1] = p_symbol;
                    table[center - 1][center] = p_symbol;
                    table[center][center] = c_symbol;
                    table[center - 1][center - 1] = c_symbol;
                }
            }
            
            show(table, row_size, col_size);
            
            return 0;
        }
    How can be the issue overcome? I have thought for hours but am stuck.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you want to work with non-square arrays you'll need two variables to keep track of the "center".

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. center text
    By Emerald in forum C Programming
    Replies: 4
    Last Post: 09-02-2009, 06:09 AM
  2. How do you center a window?
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 05-28-2005, 06:25 PM
  3. center the GetOpenFileName
    By Joelito in forum Windows Programming
    Replies: 4
    Last Post: 04-15-2005, 11:26 AM
  4. Has anyone tried XP media center ed?
    By andyhunter in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 12-20-2004, 06:16 PM
  5. Center text
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-29-2002, 11:18 AM