-4

enter image description here

How can I print this code with just enough spaces necessary to make the output organized? I tried /t did not work. I tried to add spaces manually with " ", but since the results change according to the given inputs I could not manage to line it perfectly.

I will appreciate it if you could help thank you! (Code is in c++)

martineau
  • 119,623
  • 25
  • 170
  • 301
Nikola
  • 1
  • 3
  • 2
    Can you think of a rule that tells you how many spaces to use? – Karl Knechtel Nov 03 '20 at 20:47
  • 2
    this'll help https://www.cs.fsu.edu/~myers/c++/notes/formatting.html – Tyler Kelly Nov 03 '20 at 20:50
  • 1
    [std::setw](https://en.cppreference.com/w/cpp/io/manip/setw) will help help if you are using `std::cout` to print. Looking for duplicates. – Yksisarvinen Nov 03 '20 at 20:51
  • What does your current code look like (please edit your question accordingly)? The "numbers" you are printing, are they really numbers, are they strings, ...? – Dominique Nov 03 '20 at 20:52
  • 1
    Hi, welcome to StackOverflow :) We do not write entire programs for you on this site. Instead, you need to attempt it yourself, and ask a *specific* question about some part that you are having difficulty with. Please take the [tour] and see [ask]. Also, you'll get a badge for taking the tour :) – cigien Nov 03 '20 at 21:13

2 Answers2

2

I'm guessing you are using "std::cout" to print the numbers. If yes, you can specify some formating details before printing the number. You have more details here.

In your case, I would try something like this:

#include <iostream>
#include <iomanip>

int main(){
    double l11 = 6, l12 = 403.429, l13 = 1.79146, l14 = 2.44949;
    double l21 = 9, l22 = 8103.08, l23 = 2.19722, l24 = 3;
    double l41 = 15, l42 = 3.26902e+06, l43 = 2.70805, l44 = 3.87298;

    std::cout << std::setfill(' ') << std::setw(3) << l11 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l12 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l13 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l14 << std::endl;

    std::cout << std::setfill(' ') << std::setw(3) << l21 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l22 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l23 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l24 << std::endl;

    std::cout << std::setfill(' ') << std::setw(3) << l41 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l42 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l43 << " ";
    std::cout << std::setfill(' ') << std::setw(15) << l44 << std::endl;
}

that will give you this result:

  6         403.429         1.79146         2.44949
  9         8103.08         2.19722               3
 15     3.26902e+06         2.70805         3.87298
0

How can I print this perfecly aligned in c++?

You can use following algorithm: Iterate over each row and column and calculate the width of each cell. Keep a count of widest cell in each column. Now that you know the width of each column, iterate over the data again, print the values and pad each cell up to that width.

The algorithm is simple but requires a bit of diligence to implement correctly, so test thoroughly.

Your data appears to be numbers so this shouldn't be a problem, but in general: Calculating the width of a string is actually quite tricky if you want to support Unicode. This is not only because a display "character" may consist of multiple graphemes and each grapheme may consist of multiple code points and each code point may consist of multiple code units (code unit being a "character" type in C++)... but also because the width of those characters may be wider than a single column of a fixed width font. The C++ standard has little support for Unicode. POSIX standard has wcswidth just for this purpose.

eerorika
  • 232,697
  • 12
  • 197
  • 326