0

I have a nxn grid of randomly generated numbers. I have a label that displays the element numbers for the X and Y axis:

enter image description here

It aligns up properly for single digit numbers, but when the grid size increases the labels become out of proportion and don't line up like so:

enter image description here

I'm wondering if there is any way to make the label line up with the numbers within the grid and scales depending on the size. Is this even possible?

2
  • Can you post your code? Commented Apr 24, 2017 at 2:30
  • Will the random numbers always be one digit? Commented Apr 24, 2017 at 2:43

2 Answers 2

1

You can use this to determine the number of digits in an integer:

int getDigits(int col)
{
    int len = 1;
    while ( col/= 10 )
    {
        len++;
    }
    return len;
}

With this you can determine the number of " " to print as you loop through.

std::string space(getDigits(column), ' ');
std::cout<<space<<num;
Sign up to request clarification or add additional context in comments.

Comments

0

You could use two rows for the top numbering and work out a simple calculation to determine whether or not to display the digit for that row. For instance, the top row could be for the 'tens' column and the second row for the units. You know you have 20 columns, so you could display a space character or zero if the current count of a loop is less than 10. If it's not less than 10, display a 1 character. The bottom row could then just cycle through 0 - 9 twice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.