1

For example, let us say that I am outputting

std::vector<std::vector<double>> A;

by the following:

for (int i = 0 ; i < rows ; i ++){
    for (int j = 0 ; j < cols ; j ++){
         cout << A[i][j] << " ";
         }
     std::cout << std::endl;
}

for a 3 x 3 matrix, the output would look something like:

1 3.7 2.89
3 4.85 12.34
4 5 6

as a contrived example.

I'd like to get it to look like:

1 3.7   2.89
3 4.85 12.34
4 5     6

Aligning the 1's position of the data so as to make it a bit more nicely formatted.

Does anyone have any suggestion for how to go about it? Something hidden in the iomanip library maybe?

I'm currently working on the following implementation:

  • transfer each row to string data
  • check sizes of strings
  • pick largest row (lr)
  • for every prior column element, find the element in that column with max length, compare it to length of lr's column element, and add that many spaces between that output of lr and the prior element
  • do some complex indexing :(
  • Your algorithm doesn't work because the largest row may still have individual numbers which print too narrowly for another number in another row. – john Dec 29 '22 at 10:28
  • 1
    `printf("%5.2f ", val);` would do the job if all numbers are <100. The 5 is the number of decimals of the biggest number + 3 (the dot+2digits). You only have to find the correct number for each column. – mch Dec 29 '22 at 10:39
  • You're right. @john . Like in my example above if `1` were `1.2`. That would require me to also put additional spaces in row 2. I believe it would be one space for every additional length, looking every prior column element of the other rows to determine the max length. – user8393252 Dec 29 '22 at 10:39
  • @mch Is there any point in not having `5` be a larger number (for larger input sizes)? I'm testing it right now, and it seems to work perfectly fine, without no disadvantages. – user8393252 Dec 29 '22 at 10:50
  • For example, `printf("%7.3f ", A[i][j]);` is able to handle numbers < 1000 with 3 decimal digits of precision. Also, as a sidenote, why does this work @mch ? – user8393252 Dec 29 '22 at 10:52
  • The `5` is the number of characters in total for this number, with whitespaces on the left if the number has less digits. e.g., `5.12` will be `" 5.12"` (1 whitespace on the left) `12.48` will not have any whitespace on the left. A larger number will create more whitespaces. I like the output when the numbers are separated by a tab: `std::printf("%5.2f\t", val);`: https://godbolt.org/z/sE8hKTac5 – mch Dec 29 '22 at 11:22
  • Thanks @mch , alternatively, you could left align them to leave no whitespace, this looks much better in my opinion: `printf("%-15.3f ", A[i][j]);` – user8393252 Dec 29 '22 at 20:05
  • That does not align the `.` when one number has more digits than the others. – mch Dec 30 '22 at 07:54
  • You're correct @mch , but I think it looks better. – user8393252 Dec 30 '22 at 08:04
  • @user8393252 I tried to implement it: https://godbolt.org/z/GM5jM4Pn8 It looks for the most digits per column, including negative numbers and aligns the whole column to the largest number. – mch Dec 30 '22 at 08:57
  • You're right, I found that the formula is as such for `%-X.Y`, `X - Y - 1 = amount allowed for '-' and significand`, where the `1` comes from the mandatory `.` – user8393252 Dec 30 '22 at 09:49

0 Answers0