Advertisement
PIBogdanov

Transpose

Jan 23rd, 2024
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. size_t inputRowsCount(size_t&);
  4.  
  5. size_t inputColumnsCount(size_t&);
  6.  
  7. void inputElementsIntoTheArray(double**, size_t, size_t);
  8.  
  9. void transposeTheSquaredArray(double**, size_t);
  10.  
  11. void transposeTheNonSquaredArray(double**&, size_t&, size_t&);
  12.  
  13. void swapTheRowsAndTheColumns(size_t&, size_t&);
  14.  
  15. void printTheArray(double**, size_t, size_t);
  16.  
  17. void printArrayAsTransposedOne(double**, size_t, size_t);
  18.  
  19. void disposeTheArray(double**, size_t);
  20.  
  21. int main()
  22. {
  23.     size_t rowsCount = 0;
  24.  
  25.     inputRowsCount(rowsCount);
  26.  
  27.     size_t columnsCount = 0;
  28.  
  29.     inputColumnsCount(columnsCount);
  30.  
  31.     system("cls");
  32.  
  33.     std::cout << "Rows count: " << rowsCount << "\nColumns count: " << columnsCount << "\n\n\nThe array is a " << ( (rowsCount == columnsCount) ? "square.\n\n\n" : "rectangle.\n\n\n" );
  34.    
  35.     double** numbers = new double* [rowsCount];
  36.  
  37.     for (size_t i = 0; i < rowsCount; i++)
  38.     {
  39.         numbers[i] = new double[columnsCount];
  40.     }
  41.  
  42.     std::cout << "Input elements into the array:\n\n";
  43.  
  44.     inputElementsIntoTheArray(numbers, rowsCount, columnsCount);
  45.  
  46.     std::cout << "\n\nPrinting the array:\n\n";
  47.  
  48.     printTheArray(numbers, rowsCount, columnsCount);
  49.  
  50.     std::cout << "\n\n\nPrinting the array as a transposed one:\n\n";
  51.  
  52.     printArrayAsTransposedOne(numbers, rowsCount, columnsCount);
  53.  
  54.     std::cout << "\n\n\nTransposing the array ....\n\n";
  55.  
  56.     if (rowsCount == columnsCount)
  57.     {
  58.         transposeTheSquaredArray(numbers, rowsCount);
  59.     }
  60.  
  61.     else
  62.     {
  63.         transposeTheNonSquaredArray(numbers, rowsCount, columnsCount);
  64.     }
  65.  
  66.     std::cout << "\nPrinting the array:\n\n";
  67.  
  68.     printTheArray(numbers, rowsCount, columnsCount);
  69.  
  70.     disposeTheArray(numbers, rowsCount);
  71.  
  72.     return 0;
  73. }
  74.  
  75. size_t inputRowsCount(size_t& rowsCount)
  76. {
  77.     system("cls");
  78.  
  79.     std::cout << "Input the desired rows: ";
  80.     std::cin >> rowsCount;
  81.  
  82.     if (rowsCount < 1)
  83.     {
  84.         inputRowsCount(rowsCount);
  85.     }
  86.  
  87.     return rowsCount;
  88. }
  89.  
  90. size_t inputColumnsCount(size_t& columnsCount)
  91. {
  92.     system("cls");
  93.  
  94.     std::cout << "Input the desired columns: ";
  95.     std::cin >> columnsCount;
  96.  
  97.     if (columnsCount < 1)
  98.     {
  99.         inputColumnsCount(columnsCount);
  100.     }
  101.  
  102.     return columnsCount;
  103. }
  104.  
  105. void inputElementsIntoTheArray(double** numbers, size_t rowsCount, size_t columnsCount)
  106. {
  107.     for (size_t i = 0; i < rowsCount; i++)
  108.     {
  109.         for (size_t j = 0; j < columnsCount; j++)
  110.         {
  111.             std::cin >> numbers[i][j];
  112.         }
  113.  
  114.         if (i < (rowsCount - 1))
  115.         {
  116.             std::cout << "\n";
  117.         }
  118.     }
  119. }
  120.  
  121. void transposeTheSquaredArray(double** numbers, size_t rowsCount)
  122. {
  123.     for (size_t i = 0; i < rowsCount; i++)
  124.     {
  125.         for (size_t j = i + 1; j < rowsCount; j++)
  126.         {
  127.             numbers[i][j] += numbers[j][i];
  128.  
  129.             numbers[j][i] = numbers[i][j] - numbers[j][i];
  130.  
  131.             numbers[i][j] -= numbers[j][i];
  132.         }
  133.     }
  134. }
  135.  
  136. void transposeTheNonSquaredArray(double**& numbers, size_t& rowsCount, size_t& columnsCount)
  137. {
  138.     double** numbersCopy = new double * [columnsCount];
  139.  
  140.     for (size_t i = 0; i < columnsCount; i++)
  141.     {
  142.         numbersCopy[i] = new double[rowsCount];
  143.     }
  144.  
  145.     for (size_t i = 0; i < columnsCount; i++)
  146.     {
  147.         for (size_t j = 0; j < rowsCount; j++)
  148.         {
  149.             numbersCopy[i][j] = numbers[j][i];
  150.         }
  151.     }
  152.  
  153.     disposeTheArray(numbers, rowsCount);
  154.  
  155.     swapTheRowsAndTheColumns(rowsCount, columnsCount);
  156.  
  157.     numbers = new double * [rowsCount];
  158.  
  159.     for (size_t i = 0; i < rowsCount; i++)
  160.     {
  161.         numbers[i] = new double[columnsCount];
  162.     }
  163.  
  164.     for (size_t i = 0; i < rowsCount; i++)
  165.     {
  166.         for (size_t j = 0; j < columnsCount; j++)
  167.         {
  168.             numbers[i][j] = numbersCopy[i][j];
  169.         }
  170.     }
  171.  
  172.     disposeTheArray(numbersCopy, rowsCount);
  173. }
  174.  
  175. void swapTheRowsAndTheColumns(size_t& rowsCount, size_t& columnsCount)
  176. {
  177.     rowsCount += columnsCount;
  178.  
  179.     columnsCount = rowsCount - columnsCount;
  180.  
  181.     rowsCount -= columnsCount;
  182. }
  183.  
  184. void printTheArray(double** numbers, size_t rowsCount, size_t columnsCount)
  185. {
  186.     for (size_t i = 0; i < rowsCount; i++)
  187.     {
  188.         for (size_t j = 0; j < columnsCount; j++)
  189.         {
  190.             std::cout << numbers[i][j];
  191.  
  192.             if (j < (columnsCount - 1))
  193.             {
  194.                 std::cout << " ";
  195.             }
  196.         }
  197.  
  198.         if (i < (rowsCount - 1))
  199.         {
  200.             std::cout << "\n";
  201.         }
  202.     }
  203. }
  204.  
  205. void printArrayAsTransposedOne(double** numbers, size_t rowsCount, size_t columnsCount)
  206. {
  207.     for (size_t j = 0; j < columnsCount; j++)
  208.     {
  209.         for (size_t i = 0; i < rowsCount; i++)
  210.         {
  211.             std::cout << numbers[i][j];
  212.  
  213.             if (i < (rowsCount - 1))
  214.             {
  215.                 std::cout << " ";
  216.             }
  217.         }
  218.  
  219.         if (j < (columnsCount - 1))
  220.         {
  221.             std::cout << "\n";
  222.         }
  223.     }
  224. }
  225.  
  226. void disposeTheArray(double** array, size_t rowsCount)
  227. {
  228.     for (size_t i = 0; i < rowsCount; i++)
  229.     {
  230.         delete[] array[i];
  231.     }
  232.  
  233.     delete[] array;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement