Advertisement
janac

Sort Columns In A Matrix

Nov 3rd, 2021 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <iostream>    
  2. using namespace std;
  3.  
  4. // Sort the columns by their sum, in ascending order.
  5. // The two-dimensional array (matrix).
  6. // 8 1 9 4
  7. // 3 2 1 2
  8. // 7 2 5 3
  9.  
  10. const int ROWS = 3; // Length of each row and column.
  11. const int COLUMNS = 4; // Length of each row and column.
  12.  
  13. int matrix[ROWS][COLUMNS] = { {8, 1, 9, 4}, {3, 2, 1, 2}, {7, 2, 5, 3} };
  14.  
  15. // Get the sum of the numbers in one column.
  16. int get_column_sum(int arr[ROWS][COLUMNS], int column_index)
  17. {
  18.     int sum = 0; // Start with a sum of zero.
  19.  
  20.     for (int i = 0; i < ROWS; ++i) // For each row,
  21.     {
  22.         // Add the number in the column indicated.
  23.         sum += arr[i][column_index];
  24.     }
  25.  
  26.     return sum;
  27. }
  28.  
  29. // Swap two columns.
  30. void swap_columns(int arr[ROWS][COLUMNS], int columnA, int columnB)
  31. {
  32.     int temp = 0; // This is temporary storage for the swap.
  33.     for (int i = 0; i < ROWS; ++i) // For each row,
  34.     {
  35.         temp = arr[i][columnA]; // Store value in column1 in temp.
  36.  
  37.         // Move value in column2 to column1.
  38.         arr[i][columnA] = arr[i][columnB];
  39.         arr[i][columnB] = temp; // Move value in temp to column 2.
  40.     }
  41.  
  42. }
  43.  
  44. // Display the matrix.
  45. void display_matrix(int arr[ROWS][COLUMNS])
  46. {
  47.     for (int i = 0; i < ROWS; ++i) // For each row,
  48.     {
  49.         // For each item in the row,
  50.         for (int j = 0; j < COLUMNS; ++j)
  51.         {
  52.             // Print the item.
  53.             cout << arr[i][j] << " ";
  54.         }
  55.         cout << endl; // Go to the next line.
  56.     }
  57. }
  58.  
  59. int main()
  60. {
  61.     // Display the unsorted matrix.
  62.     display_matrix(matrix);
  63.  
  64.     // Store the lowest column in the unsorted part.
  65.     int lowest_column = 0;
  66.  
  67.     // Mark the beginning of the current unsorted part.
  68.     for (int i = 0; i <= COLUMNS - 2; ++i)
  69.     {
  70.         // Store the column index at the beginning of the
  71.         // unsorted part as the lowest column.
  72.         lowest_column = i;
  73.  
  74.         // For each of the columns that follow within the
  75.         // unsorted part,
  76.         for (int j = i + 1; j <= COLUMNS - 1; ++j)
  77.         {
  78.             // Compare the lowest column to it.
  79.             // If the column sum is less,
  80.             if (get_column_sum(matrix, j) // column that follows
  81.                 < get_column_sum(matrix, lowest_column)) // current lowest column
  82.             {
  83.                 // Mark that column is less.
  84.                 lowest_column = j;
  85.             }
  86.         }
  87.  
  88.         // Swap the column with the first column in
  89.         // the unsorted part, if the lowest column is different.
  90.         if (i != lowest_column)
  91.         {
  92.             swap_columns(matrix, i, lowest_column);
  93.         }
  94.     }
  95.  
  96.     cout << endl; // Blank line.
  97.     display_matrix(matrix); // Display the sorted matrix.
  98.  
  99.     // End the program.
  100.     return 0;
  101. }
  102.  
  103.  
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement