#include <iostream>
using namespace std;
int main()
{
const int N = 7, M = 7, K = 3;
int a[N][M];
for (int row = 0; row < N; row++)
{
for (int column = 0; column < M; column++)
{
a[row][column] = rand() % 10;
}
}
cout << endl << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
cout << a[r][c] << " ";
}
cout << endl;
}
cout << endl << " Elements of the main diagonal: " << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
if (r == c)
cout << a[r][c] << " ";
}
cout << endl;
}
cout << endl << " Elements of the main diagonal of square matrix: " << endl;
for (int row = 0; row < N; row++)
{
cout << a[row][row] << " ";
}
cout << endl << " Elements of the main diagonal of any type matrix: " << endl;
// 2nd way
for (int c = 0; c < (M < N ? M : N); c++)
cout << a[c][c] << "\t";
cout << endl << " Elements of the second diagonal: " << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
if (r + c == (M < N ? M : N) - 1)
cout << a[r][c] << " ";
}
cout << endl;
}
cout << endl << " Elements of the second diagonal of square matrix: " << endl;
for (int row = 0; row < (M < N ? M : N); row++)
{
cout << a[row][(M < N ? M : N) - row - 1] << " ";
}
cout << endl << "Element above main diagonal" << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
if (c > r)
cout << a[r][c] << " ";
else
cout << " ";
}
cout << endl;
}
cout << endl << "Element under main diagonal" << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
if (c < r)
cout << a[r][c] << " ";
else
cout << " ";
}
cout << endl;
}
cout << endl << " Elements above the second diagonal: " << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
if (r + c < (M < N ? M : N) - 1)
cout << a[r][c] << " ";
}
cout << endl;
}
cout << endl << " Elements under the second diagonal: " << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
if (r + c > (M < N ? M : N) - 1)
cout << a[r][c] << " ";
else
cout << " ";
}
cout << endl;
}
cout << endl << " Elements as British flag: " << endl;
for (int row = 0; row < N; row++)
{
for (int column = 0; column < M; column++)
{
if (row == column)
cout << a[row][column] << " ";
else
if (row + column == (M < N ? M : N) - 1)
cout << a[row][column] << " ";
else
if (row == N / 2)
cout << a[row][column] << " ";
else
if (column == M / 2)
cout << a[row][column] << " ";
else
cout << " ";
}
cout << endl;
}
// Before exchange
cout << endl << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
cout << a[r][c] << " ";
}
cout << endl;
}
// Exchane the values of the 1st0 and last column and so on
int temp;
for (int column = 0; column < M / 2; column++)
{
for (int row = 0; row < N; row++)
{
temp = a[row][column];
a[row][column] = a[row][M - 1 - column];
a[row][M - 1 - column] = temp;
}
}
cout << endl << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
cout << a[r][c] << " ";
}
cout << endl;
}
// Exchane the values of the 1st0 and last row and so on
for (int row = 0; row < N / 2; row++)
{
for (int column = 0; column < M; column++)
{
temp = a[row][column];
a[row][column] = a[N - 1 - row][column];
a[N - 1 - row][column] = temp;
}
}
cout << endl << endl;
for (int r = 0; r < N; r++)
{
for (int c = 0; c < M; c++)
{
cout << a[r][c] << " ";
}
cout << endl;
}
// Для перехода хранения элементов двумерного массива в виде одномерного используем формулу:
// a[x][y] -> a2[x*M+y]
cout << endl;
int arr2d[N][M];
int a2[N * M];
for (int i = 0; i < N * M; i++)
{
a2[i] = rand() % 100;
}
for (int element = 0; element < N * M; element++)
{
if (element % M == 0)
cout << endl;
cout << a2[element] << "\t";
}
cout << endl << "a2[2][5]";
cout << " = " << a2[M * 2 + 5];
int arr3d[K][N][M];
for (int layer = 0; layer < K; layer++)
{
for (int row = 0; row < N; row++)
{
for (int column = 0; column < M; column++)
{
arr3d[layer][row][column] = rand() % 10;
}
}
}
for (int layer = 0; layer < K; layer++)
{
for (int row = 0; row < N; row++)
{
for (int column = 0; column < M; column++)
{
cout << arr3d[layer][row][column] << " ";
}
cout << endl;
}
cout << endl;
}
}