ПЕРЕСТАНОВКИ СТРОК ИЛИ СТОЛБЦОВ 1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include<iostream>

#include<ctime>
using namespace std;

const int maxM = 10;


const int maxN = 10;

void randomizeMatrix(int matrix[maxM][maxN], int row, int col, int min, int max)
{
for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
matrix[i][j] = min + rand() % (max-min+1);
}

void printMatrix(int matrix[maxM][maxN], int row, int col)


{
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
cout << matrix[i][j] << '\t';
cout << '\n';
}
cout << "\n\n";
}

void swapRows(int matrix[maxM][maxN], int fRow, int sRow, int m, int n)


{
for (int i=0;i<n;i++)
{
int tmp = matrix[fRow][i];
matrix[fRow][i] = matrix[sRow][i];
matrix[sRow][i] = tmp;
}

int lastMinRow(int matrix[maxM][maxN], int m, int n)


{
int min = UINT16_MAX;
int rowMin = 0;
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
if (matrix[i][j]<=min)
{
min = matrix[i][j];
rowMin = i;
}
}
}
return rowMin;
}

int main()
{
int m,n;
int maxValue,minValue;
srand(time(0));
cout << "Enter size of matrix(max 10x10)" << endl;
cin >> m >> n;

cout << "Enter maxValue and minValue" << endl;


cin >> maxValue >> minValue;

int myMatrix[maxM][maxN];
randomizeMatrix(myMatrix,m,n,minValue,maxValue);
printMatrix(myMatrix,m,n);

int lastRow = m-1;


int minRow = lastMinRow(myMatrix,m,n);

swapRows(myMatrix,lastRow,minRow,m,n);
cout << "last min number row " << minRow << endl;
cout << "number of last row " << lastRow << endl;

cout << "matrix after swapinig rows" << endl;


printMatrix(myMatrix,m,n);

return 0;
}

#include<iostream>
#include<ctime>
using namespace std;

const int maxM = 20;


const int maxN = 10;

void randomizeMatrix(int matrix[maxM][maxN], int row, int col, int min, int max)
{
for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
matrix[i][j] = min + rand() % (max-min+1);
}

void printMatrix(int matrix[maxM][maxN], int row, int col)


{
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
cout << matrix[i][j] << '\t';
cout << '\n';
}
cout << "\n\n";
}

void swapColumns(int matrix[maxM][maxN], int fCol, int sCol, int m, int n)


{
for (int row=0;row<m;row++)
{
int tmp = matrix[row][fCol];
matrix[row][fCol] = matrix[row][sCol];
matrix[row][sCol] = tmp;
}

void multiSwap(int matrix[maxM][maxN], int from, int to, int m, int n)


{
for (int i=0; i<=(to-from)/2; i++)
{
swapColumns(matrix,from+i,to-i,m,n);
}
}

int main()
{
int m;
int maxValue,minValue;
int myMatrix[maxM][maxN];
srand(time(0));

cout << "Enter number of rows" << endl;


cin >> m;

cout << "Enter maxValue and minValue" << endl;


cin >> maxValue >> minValue;

randomizeMatrix(myMatrix,m,maxN,minValue,maxValue);
printMatrix(myMatrix,m,maxN);

int k,s;
cout << "Enter k and s (k < s < 10)" << endl;
cin >> k >> s;
multiSwap(myMatrix,k,s,m,maxN);

cout << "Matrix after multis swap" << endl;


printMatrix(myMatrix,m,maxN);

return 0;
}

You might also like