Align a matrix with decimals and negative numbers.

May 18, 2015 at 3:17pm
Hi, I hope you can help me. I need a piece of program that with a given 2 dimensional matrix, it aligns it, so that it is shown justified in the console.

I already have one part, that aligns negative and positive numbers, but then I put some decimals numbers and it doesn't work; it doesn't align when there are positive, negative and decimals.

What makes it easier is that it is only with 3x3 matrix. So, I built a 3x3 matrix and then I add the independent terms. I am solving systems of equations 3x3, so what I want is a display like this. (Sorry for my english :( ).

** Don't pay attention to the points, I put them to show spaces. I couldn't make cplusplus to display them without nothing separating the numbers.

...................Independent terms
-2...1....9...|...2.5
1..2.5..-1...|..-3.2
4..-1.....4...|...-5
Last edited on May 18, 2015 at 3:25pm
May 18, 2015 at 3:32pm
May 18, 2015 at 3:48pm
Thanks for the answer, but as far as I know, setw() leaves a number of given spaces after the number. Which means that setw(3) and then "2.5" would leave 3 spaces after the number and setw(3) and then "3" would leave 3 spaces after that one. Tell me if I'm wrong, please...
May 18, 2015 at 3:53pm
how about converting your numbers to string, then you can count how long they are and insert spaces depending on the biggest string?
May 18, 2015 at 3:56pm
Yes, you're right. I'll try to do it that way and maybe after I'll let you know how it went.
May 18, 2015 at 4:56pm
Can anybody help me with a specific code? I can't figure out how to align them all. My algorithm functions with some cases, but then something comes out. I fix that, but that just dealigns others.

** A function for example, that takes as an argument one value. Then, I loop through the matrix, giving as argument each value of it and it shows them one by one.

** It doesn't matter what you choose, I just want that general alignment, no matter the typ of numbers (positive, negative, decimals).
May 18, 2015 at 5:24pm
Don't bother. I've just figured out how to do it. It turns out, I just needed to focus on the independent terms. The alignment of the other numbers was already done.
Topic archived. No new replies allowed.