2. homework 2c (19.09) - Syerikgul/Homeworks GitHub Wiki

#include

int main() { int rows, cols; do { std::cout << "Rows: "; std::cin >> rows; } while (rows < 1); do { std::cout << "Colums: "; std::cin >> cols; } while (cols < 1);

int** M;
//
M = new int*[rows];
for (int i = 0; i < rows; ++i)
    M[i] = new int[cols];

//
for (int i = 0; i < rows; ++i)
    for (int j = 0; j < cols; ++j)
        M[i][j] = rand();

int* widths = new int[cols];
for (int j = 0; j < cols; ++j) {
    widths[j] = 0;
    for (int i = 0; i < rows; ++i) {
        //
        int digits_count = 0;
        int n = M[i][j];
        do {
            ++digits_count;
            n /= 10;
        } while (n > 0);

        //
        if (widths[j] < digits_count)
            widths[j] = digits_count;
    }
}

//
for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {

        //
        int digits_count = 0;
        int n = M[i][j];
        do {
            ++digits_count;
            n /= 10;
        } while (n > 0);

        
        for (int space = digits_count; space < widths[j]; ++space)
            std::cout << ' ';
        std::cout << M[i][j];
        std::cout << ' ';
    }
    std::cout << std::endl;
}

//
for (int i = 0; i < rows; ++i)
    delete[] M[i];
delete[] M;

return 0;

}

⚠️ **GitHub.com Fallback** ⚠️