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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void populate();
void display();
void writeFile(ofstream& ,int[],int);
int main()
{
int choice;
cout << "Please Enter (1) if you wish to create a new set of randomly generated numbers \n";
cout << "Enter (2) if you wish to display the numbers that were generated \n";
cout << "Enter (3) if you wish to see the randomly generated numbers in reverse order/n";
cin >> choice;
if(choice == 1)
{
populate();
}
else
display();
return 0;
}
void populate()
{int values[]={5, 10, 15, 20, 25, 30, 35, 95, 100};
int elements,seed,i;
int *array;
char filename[30];
ofstream out;
cout<<"Enter number of elements: ";
cin>>elements;
cout<<"Enter random number generator seed: ";
cin>>seed;
srand(seed);
array=new int[elements];
for(i=0;i<elements;i++)
array[i]=values[rand()%7];
cout<<"Enter file name: ";
cin>>filename;
out.open(filename);
writeFile(out,array,elements);
cout<<"Operation successfully completed\n ";
}
void display()
{
char filename[50];
ifstream my;
cin.getline(filename, 50);
cin >> filename;
my.open(filename);
if(!my.is_open()){
exit(EXIT_FAILURE);
}
while(my.good())
{
int num;
my >> num;
cout << num << " ";
}
}
void writeFile(ofstream& out ,int a[],int m)
{int i;
for(i=0;i<m;i++)
out<<a[i]<<endl;
}
|