#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
const int MAKS = 20; // максимальный размер массива (с запасом)
void vivod_dvymernogo_masiva(int masiv[MAKS][MAKS], int stroki, int stolbcy);
int main()
{
int masiv[MAKS][MAKS];
int stroka, stolbec;
srand(time(0));
// случайный размер от 2 до 6
int stroki = rand() % 5 + 2;
int stolbcy = rand() % 5 + 2;
// случайное заполнение от -50 до 150
for (stroka = 0; stroka < stroki; stroka++)
{
for (stolbec = 0; stolbec < stolbcy; stolbec++)
{
masiv[stroka][stolbec] = rand() % 201 - 50;
}
}
vivod_dvymernogo_masiva(masiv, stroki, stolbcy);
}
// красивый вывод двумерного массива, всё считается внутри
void vivod_dvymernogo_masiva(int masiv[MAKS][MAKS], int stroki, int stolbcy)
{
int stroka, stolbec, i;
int shirina = 1;
int chislo, kol;
// ищем самое длинное число (минус тоже считается за символ)
for (stroka = 0; stroka < stroki; stroka++)
{
for (stolbec = 0; stolbec < stolbcy; stolbec++)
{
chislo = masiv[stroka][stolbec];
kol = 0;
if (chislo <= 0)
{
kol = 1;
}
while (chislo != 0)
{
chislo = chislo / 10;
kol++;
}
if (kol > shirina)
{
shirina = kol;
}
}
}
// верхняя линия
for (stolbec = 0; stolbec < stolbcy; stolbec++)
{
cout << "+";
for (i = 0; i < shirina + 2; i++)
{
cout << "-";
}
}
cout << "+" << endl;
// строки с числами и линия под каждой строкой
for (stroka = 0; stroka < stroki; stroka++)
{
cout << "|";
for (stolbec = 0; stolbec < stolbcy; stolbec++)
{
cout << " " << setw(shirina) << masiv[stroka][stolbec] << " |";
}
cout << endl;
for (stolbec = 0; stolbec < stolbcy; stolbec++)
{
cout << "+";
for (i = 0; i < shirina + 2; i++)
{
cout << "-";
}
}
cout << "+" << endl;
}
}