fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <time.h>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. int main(void) {
  9.  
  10. // random number for rand()
  11. srand(time(0));
  12.  
  13. int m = 5; // row count
  14. int n = 5; // column count
  15.  
  16. // declaration of a dynamic array of pointers
  17. auto arr = new double*[n];
  18.  
  19. // filling the array with pointers
  20. for (int i = 0; i < n; i++)
  21. arr[i] = new double[m];
  22.  
  23. // array initialization with random numbers
  24. for (int i = 0; i < n; i++)
  25. for (int j = 0; j < m; j++)
  26. arr[i][j] = (double) (rand() % 400 - 199) / 2.0; // (-100.0; 100.0)
  27.  
  28. // matrix output
  29. cout << "\n\033[92mOriginal array:\033[94m" << endl;
  30. for (int i = 0; i < m; i++) {
  31. for (int j = 0; j < n; j++)
  32. //printf("%5.1f ", arr[i][j]);
  33. cout<<setw(6)<<fixed<<setprecision(1)<<arr[i][j];
  34. cout << endl;
  35. }
  36.  
  37.  
  38. // array for the sums of modules of the row elements
  39. auto sumOfAbsolutes = new double[m];
  40.  
  41. // Initializing the array with zeros
  42. for (int i = 0; i < m; i++)
  43. sumOfAbsolutes[i] = 0;
  44.  
  45. // filling the array with the sums of element modules
  46. for (int i = 0; i < m; i++)
  47. for (int j = 0; j < n; j++)
  48. sumOfAbsolutes[i] += abs(arr[i][j]);
  49.  
  50. // output
  51. cout << "\n\033[92mSums of modules of array row elements:" << endl;
  52. for (int i = 0; i < m; i++)
  53. cout << "\033[92m" << i << ": \033[94m"<< sumOfAbsolutes[i] << " ";
  54. cout << "\n\n";
  55.  
  56.  
  57. // sorting
  58. for (int i = 0; i < (m - 1); i++)
  59. for (int j = i; j < m; j++)
  60. if (sumOfAbsolutes[i] > sumOfAbsolutes[j]) {
  61. double tmp = sumOfAbsolutes[i];
  62. sumOfAbsolutes[i] = sumOfAbsolutes[j];
  63. sumOfAbsolutes[j] = tmp;
  64.  
  65. double *tmp2 = arr[i];
  66. arr[i] = arr[j];
  67. arr[j] = tmp2;
  68. }
  69.  
  70. // matrix output
  71. cout << "\033[92mSorted array:\033[94m" << endl;
  72. for (int i = 0; i < m; i++) {
  73. for (int j = 0; j < n; j++)
  74. //printf("%5.1f ", arr[i][j]);
  75. cout<<setw(6)<<fixed<<setprecision(1)<<arr[i][j];
  76. cout << endl;
  77. }
  78.  
  79.  
  80. int columnWithMaxNegNum = 0; // the column with the maximal negative element
  81. int minNumber = 0; // the column with the minimum element
  82.  
  83. // search for the maximal negative element
  84. double maxNegNum = -100.0; // tbd
  85. for (int i = 0; i < m; i++)
  86. for (int j = 0; j < n; j++)
  87. if (arr[i][j] < 0 && arr[i][j] > maxNegNum) {
  88. columnWithMaxNegNum = j;
  89. maxNegNum = arr[i][j];
  90. }
  91. // minimum element search
  92. double minN=100.0;
  93. for (int i = 0; i < m; i++)
  94. for (int j = 0; j < n; j++)
  95. if (arr[i][j] < minN) {
  96. minNumber = j;
  97. minN = arr[i][j];
  98. }
  99.  
  100. cout << "\n\033[92mThe column with the maximum negative element: \033[94m" << columnWithMaxNegNum << endl;
  101. cout << "\033[92mThe column with the minimum element: \033[94m" << minNumber << endl;
  102.  
  103. // rearrangement of columns
  104. for (int i = 0; i < m; i++) {
  105. double temp = arr[i][columnWithMaxNegNum];
  106. arr[i][columnWithMaxNegNum] = arr[i][minNumber];
  107. arr[i][minNumber] = temp;
  108. }
  109.  
  110. cout << "\n\033[92mRearrangement of columns:" << endl;
  111. for (int i = 0; i < m; i++) {
  112. for (int j = 0; j < n; j++)
  113. printf("\033[94m%5.1f ", arr[i][j]);
  114. cout << "\n\033[0m";
  115. }
  116.  
  117. // memory cleanup
  118. delete[]sumOfAbsolutes;
  119. for (int i = 0; i < n; i++)
  120. delete[]arr[i];
  121. delete[]arr;
  122. }
  123.  
Success #stdin #stdout 0.01s 5428KB
stdin
Standard input is empty
stdout
Original array:
  33.5  -0.5  16.5 -39.0  64.5
  80.5  -1.0 -46.5 -23.0 -59.5
  49.5  98.5 -27.0  96.5 -53.5
  44.0  91.5 -43.0 -94.0 -71.5
 -84.5  75.5 -60.0  54.5  32.5

Sums of modules of array row elements:
0: 154.0   1: 210.5   2: 325.0   3: 344.0   4: 307.0   

Sorted array:
  33.5  -0.5  16.5 -39.0  64.5
  80.5  -1.0 -46.5 -23.0 -59.5
 -84.5  75.5 -60.0  54.5  32.5
  49.5  98.5 -27.0  96.5 -53.5
  44.0  91.5 -43.0 -94.0 -71.5

The column with the maximum negative element: 1
The column with the minimum element: 3

Rearrangement of columns:
 33.5 -39.0  16.5  -0.5  64.5 
 80.5 -23.0 -46.5  -1.0 -59.5 
-84.5  54.5 -60.0  75.5  32.5 
 49.5  96.5 -27.0  98.5 -53.5 
 44.0 -94.0 -43.0  91.5 -71.5