Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. char** ChangeMatrixsElements(char** matrix, int rows, int cols);
  6. void DeleteCharMatrix(char** matrix, int rows);
  7.  
  8. int main() {
  9. const int rows = 3;
  10. const int cols = 3;
  11.  
  12. char** inputMatrix = new char* [rows];
  13.  
  14. for (int i = 0; i < rows; i++)
  15. {
  16. inputMatrix[i] = new char[cols];
  17. for (int j = 0; j < cols; j++)
  18. {
  19. cin >> inputMatrix[i][j];
  20. }
  21. cout << "\r\n";
  22. }
  23.  
  24. char** result = ChangeMatrixsElements(inputMatrix, rows, cols);
  25.  
  26. for (int row = 0; row < rows; row++)
  27. {
  28. for (int col = 0; col < cols; col++)
  29. {
  30. cout << result[row][col];
  31. }
  32.  
  33. cout << "\r\n";
  34. }
  35.  
  36. DeleteCharMatrix(inputMatrix, rows);
  37. DeleteCharMatrix(result, rows);
  38. }
  39.  
  40. char** ChangeMatrixsElements(char** matrix, int rows, int cols) {
  41. char** tempMatrix = new char* [rows];
  42.  
  43. for (int i = 0; i < rows; i++)
  44. {
  45. tempMatrix[i] = new char[cols];
  46. }
  47.  
  48. for (int row = 0; row < rows; row++)
  49. {
  50. for (int col = 0; col < cols; col++)
  51. {
  52. int position = col;
  53. if (row % 2 == 0 && col % 2 == 0) {
  54.  
  55. position = (row + col) % cols;
  56. }
  57. else if (row % 2 != 0 && col % 2 != 0) {
  58. position = (row - col) % cols;
  59.  
  60. if (position < 0) {
  61. position *= -1;
  62. }
  63. }
  64.  
  65. tempMatrix[row][col] = matrix[row][position];
  66. }
  67. }
  68.  
  69. return tempMatrix;
  70. }
  71.  
  72. void DeleteCharMatrix(char** matrix, int rows) {
  73. for (int i = 0; i < rows; i++)
  74. {
  75. delete[] matrix[i];
  76. }
  77.  
  78. delete[] matrix;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement