PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 15, 2022

[FIXED] How to format output like this

 August 15, 2022     c++, c++11, formatting, matrix, output     No comments   

Issue

My code is like this so far :

void matrix::print(int colWidth) const
{
    cout << getRows() << " x " << getCols() << endl;
    cout << "-";
    for (unsigned int d = 0; d < getCols(); d++) {
        cout << "--------";
    }

    cout << endl;
    for (unsigned x = 0; x < getRows(); x++) {
        cout << "|";
        for (unsigned y = 0; y < getCols(); y++) {
            cout << setw(colWidth) << at(x, y) << " |";
        }
        cout << endl;
    }
    cout << "-";
    for (unsigned int d = 0; d < getCols(); d++) {
        cout << "--------";
    }

    cout << endl;
}

But the output depends on the colWidth which will be the space between each number printed. So how can I adjust my dashes to be printed like the following no matter the colWidth it should align.

One output should look like this:

enter image description here

Second output is like this:

enter image description here


Solution

If the column width is a parameter, you're almost done with your code. Just turn the cout<<"--------" into:

std::cout << std::string(getCols()*(colWidth + 2) + 1, '-');

That code prints a string of dashes, which width is: number of matrix columns, times column width plus 2, plus 1:

  • Plus 2 because you are appending a " |" to each column.
  • Plus 1 because you are adding a '|' at the beginning of each row.

You may want to check for empty matrices at the beginning of your print method.

[Demo]

#include <initializer_list>
#include <iomanip>  // setw
#include <iostream>  // cout
#include <vector>

class matrix
{
public:
    matrix(std::initializer_list<std::vector<int>> l) : v{l} {}
    
    size_t getRows() const { return v.size(); }
    size_t getCols() const { if (v.size()) { return v[0].size(); } return 0; }
    int at(size_t x, size_t y) const { return v.at(x).at(y); }

    void print(int colWidth) const
    {
        std::cout << "Matrix: " << getRows() << " x " << getCols() << "\n";

        // +2 due to " |", +1 due to initial '|'
        std::cout << std::string(getCols()*(colWidth + 2) + 1, '-') << "\n";
        
        for (unsigned x = 0; x < getRows(); x++) {
            std::cout << "|";
            for (unsigned y = 0; y < getCols(); y++) {
                std::cout << std::setw(colWidth) << at(x, y) << " |";
            }
            std::cout << "\n";
        }

        std::cout << std::string(getCols()*(colWidth + 2) + 1, '-') << "\n";
    }

private:
    std::vector<std::vector<int>> v{};
};

int main()
{
    matrix m{{1, 2}, {-8'000, 100'000}, {400, 500}};
    m.print(10);
}

// Outputs
//
//   Matrix: 3 x 2
//   -------------------------
//   |         1 |         2 |
//   |     -8000 |    100000 |
//   |       400 |       500 |
//   -------------------------


Answered By - rturrado
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing