Thread: printing grid with spaces regard to number of digits

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

    printing grid with spaces regard to number of digits

    I try to print the grid with spaces regard to digits(units digit, tens digit,hundreds digit etc. ). It prints as expected when entered just one decimal numbers.(one digit numbers) If it is thought for `mxn` grid for example 4x12 or 4x101. My aim is to separate using spaces regards to units digit, tens digit,hundreds digit etc. I try to explain it on example:


    Code:
        4 12
        
        Output:
        
          123456789101112
         1............
         2.....xo.....
         3.....ox.....
         4............
        
         Expected output:
        
          123456789101112
         1......... . . .
         2.....xo.. . . .
         3.....ox.. . . .
         4......... . . .
        
         4 103
        
         Output
        
          123456789101112.....100101102103
         1.........................
         2.................xo......
         3.................ox......
         4.........................
        
         Expected output:
        
          123456789101112.....100101102103
         1......... . . . . . .  .  .  .
         2......... . . . x o .  .  .  .
         3......... . . . o x .  .  .  .
         4......... . . . . . .  .  .  .
                     |         ||
                one space      two spaces


    I have also a function to calculate how many decimal places there are.


    Code:
        int decimalPlace(int num)
        {
            int decPlace = 0;
            do
                {
                    decPlace++;
                    num=num/10;
                }
                while(num);
            return decPlace;
        }
    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[][10000], int row_size, int col_size)
        {
            cout << endl << setw(3);
            for(int i = 0 ; i < col_size ; ++i)
                cout << i + 1;
            cout << endl;
            
            for(int i = 0; i < row_size; ++i)
            {
                cout << setw(2) << i + 1;
                for(int j = 0; j < col_size; j++)
                    cout << table[i][j];
                cout << endl;
            }
            cout << endl;
        }
        
        int main()
        {
            int row_size;
            int col_size;
            char table[10000][10000];
            
            cin >> row_size >> col_size;
            
            int center = 0;
            for(int i = 0; i < row_size; ++i)
                for(int j = 0; j < col_size; ++j)
                {
                    table[i][j] = what_symbol;
                }
            int row = row_size / 2;
            int col = col_size / 2;
                    table[row][col - 1] = p_symbol;
                    table[row - 1][col] = p_symbol;
                    table[row][col] = c_symbol;
                    table[row - 1][col - 1] = c_symbol;
            
            show(table, row_size, col_size);
            
            return 0;
        }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    It doesn't seem you actually asked a question.

    I see you have a function, decimalPlace(), that appears to generate a value that is meant to be used, I assume, as the "width" of a column, and thus used to generate spaces.

    So... just where are you calling it in your main code?

    -Mike

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > char table[10000][10000];
    Mmm, the default stack size on most systems is between 1MB and 8MB.

    A 100Mb array just seems plain wrong and an invitation to disaster.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of occurence of digits in a number
    By Azeem in forum C Programming
    Replies: 16
    Last Post: 10-12-2012, 11:46 PM
  2. Help printing 4x4 grid
    By Watabou in forum C Programming
    Replies: 6
    Last Post: 03-09-2011, 12:48 PM
  3. Printing an 4x4 grid and storing it in an array?
    By Watabou in forum C Programming
    Replies: 10
    Last Post: 03-02-2011, 03:03 AM
  4. Replies: 5
    Last Post: 12-13-2007, 12:14 PM
  5. Printing a specific number of digits
    By JizJizJiz in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2006, 08:14 PM