Array Column Sort with quality score

Hi I want to implement a code in C++ in which I would like to sort columns in an array according to a quality score, any help is greatly appreciated.

example

A[m][n]=
{1 2 3
4 7 9
1 3 9}

say the column
1
4
1
has a quality score of 1

say the column
2
7
3
has a quality score of 7

say the column
3
9
9
has a quality score of 2


I want to sort the columns in ascending order of quality score 1 2 and 7

so I want my o/p to be

o/p =
{ 1 3 2
4 9 7
1 9 3}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;

//======================================================================

int main()
{
   vector<int> quality = { 1, 7, 2 };
   vector< vector<int> > A = { { 1, 2, 3 },
                               { 4, 7, 9 },
                               { 1, 3, 9 } };

   int rows = A.size();
   int cols = A[0].size();   assert( quality.size() == cols );


   // Pack quality and original columns into a vector of pairs
   vector< pair<int,int> > Q( cols );
   for ( int c = 0; c < cols; c++ )
   {
       Q[c].first  = quality[c];
       Q[c].second = c;
   }


   // Sort on the basis of quality
   sort( Q.begin(), Q.end(), []( const pair<int,int> &a, const pair<int,int> &b ){ return a.first < b.first; } );


   // Permute columns in original quality and array
   vector< vector<int> > temp( rows, vector<int>( cols ) );
   for ( int c = 0; c < cols; c++ )
   {
       quality[c] = Q[c].first;
       int oldcol = Q[c].second;
       for ( int r = 0; r < rows; r++ ) temp[r][c] = A[r][oldcol];
   }
   A = temp;


   // Output
   cout << "Quality:\n";
   for ( int i : quality ) cout << setw( 2 ) << i << " ";
   cout << "\n\n";
   cout << "Data:\n";
   for ( const auto &row : A )
   {
      for ( int i : row ) cout << setw( 2 ) << i << " ";
      cout << '\n';
   }
}

//====================================================================== 

Quality:
 1  2  7 

Data:
 1  3  2 
 4  9  7 
 1  9  3 
Last edited on
Thanks alot @lastchance !!
I am getting this error in the IDE compiler

[Error] in C++98 'quality' must be initialized by constructor, not by '{...}' ?? why is it ??
Try it in C++ shell (use the little gear wheel at the top right of the code sample).

{}-based initialisation for vectors came in with C++11. I've only used it here as a quick way to initialise my array. You may be intending to initialise it in a different manner anyway. If it's not c++11-compliant then I'm surprised that it's not squawking about range-based for loops, lambda functions and the use of "auto" as well.

I've just edited it to take it back to C++11 compliant ... but I don't feel like downgrading it any further.

Either:
(1) Upgrade your compiler
(2) Set a compiler option (e.g. -std=c++11) in your IDE if it will let you (any reasonably recent one should)
(3) (Last resort) Set the components of A manually, or with nested loops, or by reading from file.

I can't advise you on IDEs since I don't use one, so you will have to READ their documentation.

If I hadn't upgraded g++ for a long time then I would have to type
g++ -std=c++11 myfile.cpp
at the command line. However, a minimum of c++11 (and probably later) is assumed by default for later versions.
Last edited on
Ok thanks!! -std=c++ worked fine
Last edited on
Topic archived. No new replies allowed.