Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:23:18+00:00 2026-06-06T23:23:18+00:00

I often run into a situation where I want to print a 2d array

  • 0

I often run into a situation where I want to print a 2d array to screen or file. My standard approach is this:

for(int q=0; q<x; q++)
{
    cout << "\n";
    for(int w=0; w<y; w++)
        cout << number[q][w] << "\t";
}

However, when the elements of number vary in length, this typically leads to the columns not being aligned properly. How can I add the correct amount of whitespace after (or before) each number to make the columns line up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:23:19+00:00Added an answer on June 6, 2026 at 11:23 pm

    Use std::setw(), std::left and std::right.

    #include <iostream>
    #include <iomanip>
    
    void printNumber(int x) {
        std::cout << "X:" << std::setw(6) << x << ":X\n";
    }
    
    void printStuff() {
        printNumber(528);
        printNumber(3);
        printNumber(73826);
        printNumber(37);
    }
    
    int main() {
        std::cout << "Left-aligned\n";
        std::cout << std::left;
        printStuff();
        std::cout << "Right-aligned\n";
        std::cout << std::right;
        printStuff();
    }
    

    Output:

    Left-aligned
    X:528   :X
    X:3     :X
    X:73826 :X
    X:37    :X
    Right-aligned
    X:   528:X
    X:     3:X
    X: 73826:X
    X:    37:X
    

    (Demo: http://ideone.com/6IdIc.)

    In this example, I’ve used 6 as the maximum width expected. If you don’t know the maximum, you’ll either need to do an initial pass through your data to find out, or just use the maximum possible for the data-type you’re using (to make this portable, you could use std::numeric_limits<T>::max).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often run into the situation where I want to disable some code while
I often run into a situation where I want to subscribe to an event,
Situation: I often run into this problematic and never know how to solve it.
I often run into a situation where I want to have a set of
When you start messing around with Spring's auto-proxy stuff, you often run into this
This is a problem I often run into, and my solution has always to
All too often I've run into a situation in which a view in my
I often run into a situation where I need to come up with a
I run into this problem again and again; Situation there is a (large) dataset
I so often run into this pattern. Can someone please give me a canonical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.