fork(5) download
  1. //(c)Terminator
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. void transponse(int* arr, int& n, int& m);
  8. void print_mat(ostream& _o, const int* arr,
  9. const int n, const int m);
  10.  
  11.  
  12. int main(void){
  13. const int M = 5;
  14. const int N = 6;
  15. int arr[M*N];
  16.  
  17. int n = N;
  18. int m = M;
  19.  
  20. // заполнить матрицу для примера
  21. int* e = &arr[M*N];
  22. for(int* i = &arr[0]; i != e;)
  23. *i++ = rand() % 10;
  24.  
  25. print_mat(cout, arr, n, m);
  26.  
  27. transponse(arr, n, m);
  28. print_mat(cout, arr, n, m);
  29.  
  30. transponse(arr, n, m);
  31. print_mat(cout, arr, n, m);
  32. return 0;
  33. }
  34.  
  35.  
  36. //транспонирование
  37. void transponse(int* arr, int& n, int& m){
  38. if(n == m){
  39. for(int r = 0; r < n; ++r){
  40. for(int c = r; c < m; ++c)
  41. swap(arr[r*m + c], arr[c*m + r]);
  42. }
  43. } else {
  44. int* tmp = new int[n * m];
  45. for(int r = 0; r < n; ++r){
  46. for(int c = 0; c < m; ++c)
  47. tmp[c*n + r] = arr[r*m + c];
  48. }
  49. memcpy(arr, tmp, (size_t)(n * m) * sizeof(int));
  50. swap(n, m);
  51. delete[] tmp;
  52. }
  53. }
  54.  
  55.  
  56. //печать матрицы
  57. void print_mat(ostream& _o, const int* arr,
  58. const int n, const int m){
  59. for(int i = 0; i < n; ++i){
  60. for(int j = 0; j < m; ++j)
  61. _o << arr[i*m + j] << ' ';
  62. _o << endl;
  63. }
  64. _o << endl;
  65. }
  66.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
3 6 7 5 3 
5 6 2 9 1 
2 7 0 9 3 
6 0 6 2 6 
1 8 7 9 2 
0 2 3 7 5 

3 5 2 6 1 0 
6 6 7 0 8 2 
7 2 0 6 7 3 
5 9 9 2 9 7 
3 1 3 6 2 5 

3 6 7 5 3 
5 6 2 9 1 
2 7 0 9 3 
6 0 6 2 6 
1 8 7 9 2 
0 2 3 7 5