hello,
I have 2 int arrays Mass[n] and Volume[n],
and i need to sort them according to their quotient(M[i]/V[i]) i.e. density.
(oh and I want to use the sort of <algorithm>)
I really HAVE NO IDEA how to do this using the modified sort but a friend told me i could create a struct that would have the two arrays in. I tried so:
struct item{
unsigned int V[];
unsigned int M[];
};
but the problem is that the length of the array is given by the user.(struct must be defined before main but i get n(length) obviously after int main() starts).
i thought of std::sort(rotsa.begin(), rotsa.end(), &rotsa_sorter);
and using the comp:
bool rotsa_sorter(rotsa const& lhs, rotsa const& rhs) {
if (((lhs.M)*(rhs.V)) > ((rhs.M)*(lhs.V)))
//instead of having two quotients i used that if a/b=c/d and b,c!=0 then a*d=c*b
{return lhs>rhs;}
else{return lhs<rhs;}
}
but it didn't work at all. Any ideas? either on another way of doing the sort or maybe you could help me correct my code?
Thanks very very much :)
Sorting the two arrays is a bit tricky, because std::sort expects the items being sorted to be single objects, and each item you have is actually two separate things. You can take an approach like iHutch105 so that each element is an actual object, and you only have a single array, you could make a proxy object that correctly handles assignment and comparison on the underlying arrays - although this is a bit complex, or personally, I would recommend you sort the indices. That is, you leave the original arrays alone, but create a new array of indices into the actual arrays, and sort that.
To sort the indices you should:
1) Create an array with with same number of elements as your mass and volume and set array[i] = i
2) Create a function indexLessThan(int *mass, int *density, int index1, int index2) which returns true if the density at index index1 is less than the density at index2 - you pretty much implemented this in your original post
3) Call sort on the array of indices using tr1::bind(indexLessThan, massArray, densityArray, _1, _2) as the comparator. If you don't have access to bind, this can be done with a struct with operator() - google for functors.
4) You should now be left with an array where the i'th element gives the index in the mass and volume arrays of the i'th sorted element. ie the mass of the third sorted item is mass[sortedIndices[2]].
Also be careful using integers for mass and volume.
Ther eis a much much easier way which requires only for you to make a vector of pairs and a function! The first element of the pair is the mass, and the second is the volume
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <algorithm>
#include <vector>
usingnamespace std;
bool comp(pair<int, int> a, pair<int, int> b)
{
return a.first/a.second<b.first/b.second;
}
int main()
{
vector<pair<int, int> > density;
// yadda yadda yadda
sort(density.begin(), density.end(), comp);
// blah blah blah
}