#include <iostream> #include <ctime> using namespace std; const int MAX_S = 10; void create (int Arr[MAX_S][MAX_S], int collumn, int row) { for (int currentRow(0); currentRow < row; currentRow++) { for (int currentCol(0); currentCol < collumn; currentCol++) Arr[currentRow][currentCol] = rand()%100-50; } } void show(int Arr[MAX_S][MAX_S],int collumn,int row) { for (int currentRow(0); currentRow < row; currentRow++) { cout << "(" << currentRow << ")"; for (int currentCol(0); currentCol < collumn; currentCol++) cout << Arr[currentRow][currentCol] << "\t"; cout << endl; } } void swap(int Arr[MAX_S][MAX_S],int collumn,int row) { for (int currentRow(0); currentRow < collumn; currentRow++) { int mirroredCol = collumn - 1; for (int currentCol(0); currentCol < row/2; currentCol++) { if(currentCol % 2 != 0) { swap(Arr[currentRow][currentCol],Arr[currentRow][mirroredCol]); mirroredCol--; } } } } int main() { int row,collumn; cout << "Введіть кількість рядків:\n"; cin >> row; cout << "Введіть кількість стовпців:\n"; cin >> collumn; int Arr[MAX_S][MAX_S]; srand(time(NULL)); create(Arr,collumn,row); show(Arr,collumn,row); swap(Arr,collumn,row); cout << "\n\n\n"; show(Arr,collumn,row); return 0; } 

The swap function reverses the row, but it needs to be redone to turn the column. How to change the function to turn the column?

  • Do you just need to "flip" each column? - Vlad from Moscow

2 answers 2

If you just need to "flip" each column, the function may look like this

 void swap(int Arr[MAX_S][MAX_S],int collumn,int row) { for ( int currentCol = 0; currentCol < collumn; currentCol++ ) { for ( int currentRow = 0; currentRow < row / 2; currentRow++ ) { swap( Arr[currentRow][currentCol], Arr[row - currentRow -1][currentCol]); } } } 

Keep in mind that in the main function, you should check that the values ​​entered for the number of rows and columns do not exceed the MAX_S constant. Otherwise, the program may have an undefined behavior.

  • Oh, I forgot to point out that only non-even columns should roll over, I apologize - darkwoolf
  • But it's easy to do - darkwoolf
  • I just added if (currentCol% 2! = 0) before swap (Arr [currentRow] [currentCol], Arr [row - currentRow -1] [currentCol]); - darkwoolf
  • @darkwoolf And why is that ?! This condition leads to the fact that only odd columns are swapped. - Vlad from Moscow
  • So it was necessary) - darkwoolf

Personally, I am more impressed by the cycle with two variables:

 for(int i = 0, j = row-1; i < j; ++i, --j) { swap(Arr[currentRow][i],Arr[currentRow][j]); }