How to create file

Hello, I need to created text file with 100,000 reverse sorted out elements. I generated my random 100,000 elements from website, how do I get the reverse sorted out version???

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <ctime>
#include <cstdlib>
using namespace std;

int WIDTH = 3;


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


template <typename T> ostream &operator << ( ostream &strm, vector<T> &M )     // Output a vector<T>
{
   for ( auto e : M ) strm << setw( WIDTH ) << e << ' ';
   return strm;
}


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


vector<int> getNumbers( unsigned int N, int a, int b )     // Return N random numbers between a and b (inclusive)
{                          
   vector<int> result;
   int d = b - a + 1;
   while ( N-- ) result.push_back( a + rand() % d );
   return result;
}


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


int main()
{
   srand( time( 0 ) );

   vector<int> V = getNumbers( 10, 0, 99 );                // Get some numbers
   cout << "Original:       " << V << '\n';

   sort( V.begin(), V.end(), greater<int>() );             // Sort by predicate
   cout << "Reverse sorted: " << V << '\n';

   // Write to file (one per line)
   ofstream out( "output.txt" );
   for ( int e : V ) out << e << '\n';
}


Original:        70  46  83  51  49  52  67  72  72  32 
Reverse sorted:  83  72  72  70  67  52  51  49  46  32 
Last edited on
Topic archived. No new replies allowed.