C++ column output via cout

There is such a task: generate a matrix NxN with random numbers, find the maximum in each column of the resulting matrix, and find the minimum from a sample of maxima. At first, such a seemingly simple solution came up:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>

#define N 6
#define RAND() rand() % 100 // [0;99]
#define SIZE setw(4)

using namespace std;

ofstream fout("result.txt");

void print(const char* str)
{
    cout << str;
    fout << str;
}

void print(int number)
{
    cout << SIZE << number;
    fout << SIZE << number;
}

int main(int argc, char** argv)
{
    srand(time(NULL));
    setlocale(LC_ALL, "Russian");
    int d2arr[N+1][N]; // доп строка для максимумов столбцов
    int min = 100;
    for (int i = 0; i < N; i++)
        d2arr[N][i] = -1;

    print("Вывод сгененированной матрицы:\n");

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            d2arr[i][j] = RAND();
            if (d2arr[i][j] > d2arr[N][j])
                d2arr[N][j] = d2arr[i][j];
            print(d2arr[i][j]);
        }
        print("\n");
    }
    print("\nВывод максимумов для столбцов:\n");
    for (int i = 0; i < N; i++)
    {
        if (d2arr[N][i] < min)
            min = d2arr[N][i];
        print(d2arr[N][i]);
    }
    print("\n\nМинимум из выборки:");
    print(min);
    fout.close();

    _getch();
    return 0;
}

However, I would like to optimize the program and get rid of the second cycle, the idea is (pseudo):

int min = 100, max=-1;
int d2arr[N][N];
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            d2arr[j][i] = RAND(); // генерируем по столбцам
            if (d2arr[j][i] > max)
                max = d2arr[j][i];
            print(d2arr[j][i]);
        }
        if (max < min) min = max;
        print("\n");
    }

The actual problem: how can I output such data immediately? Let's say I output the first column by wrapping the row after each element. How do I go back to the beginning of the console? Preferably without the console handle and SetConsoleCursorPosition, and using something simpler (a simple univer lab, just interesting to do differently).
P.S.: the program uses fstream, and the same thing should be output there.
Windows OS, Visual studio

Author: SelfishCrawler, 2019-11-08

1 answers

Regarding the output: native means of returning to the line back can not be achieved... But, there are libraries that allow you to organize such output in the console. I'm talking about ncurses (it also compiles under windows, but this may be a problem, so in this case, use the pdcurses-api the same, except for rare moments)

 1
Author: Andrej Levkovitch, 2019-11-09 02:10:41