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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <fstream>//to read/write from/to files
#include <cstdlib>//to use srand seed and rand
#include <ctime>
#include <iostream>
#include <string>//to use C++ string object (different from C's C-string)
using namespace std;
void makeData(ofstream &of, string fileName,string dataType,int numItems,)
{
int itemsPerLine = 0;//used to organize data readability in text file to print x data per line
int randElem;
const int LOW = 0;
const int HIGH = 2*numItems;
of.open(fileName.c_str() );//creates a new file if file doesn't exist
if ( dataType == "random")
{
for ( int i = 0; i < numItems; i++ )
{
randElem = rand() % (HIGH - LOW + 1) + LOW;
if ( itemsPerLine == 10 )
{
of << "\n";
itemsPerLine = 0;//reset for brand new list of x items in a row
}
of << randElem <<" ";
itemsPerLine+=1;
}
of.close();
}//END RANDOM LIST GENERATOR BLOCK
if ( dataType == "sorted" )
{
for ( int i = 0; i < numItems; i++ )
{
if ( itemsPerLine == 10 )
{
of << "\n";
itemsPerLine = 0;//reset for brand new list of x items in a row
}
of << i <<" ";
itemsPerLine+=1;
}
of.close();
}//END SORTED LIST GENERATOR BLOCK
if ( dataType == "reverse" )
{
for ( int i = numItems; i >= 0; i-- )
{
if ( itemsPerLine == 10 )
{
of << "\n";
itemsPerLine = 0;//reset for brand new list of x items in a row
}
of << i <<" ";
itemsPerLine+=1;
}
of.close();
}//END REVERSED LIST GENERATOR BLOCK
}//END makeData FCN
int main()
{
/*MAKE RANDOM DATA*/
ofstream myFileRand;
makeData(myFileReverse,"randomData1000.txt","random",1000);
/*MAKE REVERSED DATA*/
ofstream myFileReverse;
makeData(myFileReverse,"reversedData1000.txt","reversed",1000);
/*MAKE SORTED DATA*/
ofstream myFileSorted;
makeData(myFileSorted,"sortedData1000.txt","sorted",1000);
return 0;
}
|