It is necessary to rearrange the columns in the matrix so that they go in descending order of the sum of the columns. The whole program is not necessary, you only need a sorting algorithm.
Closed due to the fact that the issue is too general for the participants Grundy , aleksandr barakin , Vladimir Martyanov , D-side , user194374 Apr 2 '16 at 19:27 .
Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .
- oneIs this your lab asked? - OlegUP
- The solution depends very much on how you store the matrix. - Harry
1 answer
No one will solve the task for you here, this is not according to the rules of the site, there are different sorting algorithms, google or here is the link
There are a lot of options, for example, to create a structure that binds the amount of a column to its index, you can use std::pair
instead. If you need to sort descendingly, you can sort it using the <
typedef struct { unsigned int index_; double sum_; // или long int или long double, завиит от того какая матрица } SumIndexBunch ; bool operator < ( SumIndexBunch& a, SumIndexBunch& b ) { return a.sum_ < b.sum_; }
1) use an array and apply to it the desired sorting algorithm implemented by itself
2) If you were assigned a sorting laboratory at the institute, it probably does not work: use std::vector
such structures of dimension N (N = number of columns), sort by the sum
_ field, vector
has a sort
method, it sorts the elements in descending order or return, depending on the template parameter, type comparer= ( std::less
or std::greater)
(by default, less
so that you don’t need to define a comparer). for int and its derivatives <, >
operators are already defined, but for your structure you will have to override them.
3) there is an option with std::set
, which stores the elements in order, but the snag is that a quick search in it will be only by the value of sum_ since comparison operators need to be defined for them, and index search will have to be written manually.