Feb 12, 2012 at 9:10am UTC
I have written this sort algorithm for a number of given integers from a file.
The problem is that it sorts the numbers in an ascending order. I want the vice-versa... How could I do that ?
----
Sorry For Bad English
----
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
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
int vect[1001];
int i,j,n,p=0;
ifstream inFisier("Code_in.txt" );
ofstream outFisier("Code_out.txt" );
if (!inFisier.is_open() || !outFisier.is_open())
{
cout << "Error 404: File not found!\n" ;
return 1;
}
else
{
inFisier >> n;
for (i=0; i<n; ++i)
{
inFisier >> vect[i];
p++;
}
sort (vect, vect + p);
for (j=0; j<p; ++j)
{
outFisier << vect[j] << ' ' ;
}
}
return 0;
}
Last edited on Feb 12, 2012 at 9:11am UTC
Feb 12, 2012 at 10:05am UTC
H, just put the function
bool compare( int a , int b ) { return a > b; } ;
and
use sort as sort (vect, vect + p , compare );
Feb 12, 2012 at 10:17am UTC
Thanks a lot blue :) It's great!