Write a C++ utility object that can be used to generate data files that contain
ID: 3777064 • Letter: W
Question
Write a C++ utility object that can be used to generate data files that contain random numbers. Upon instantiation, your object should contain a method takes two arguments. Create the following interface for a method somewhere within your object:
Overview In this assignment, the student will write a C++ program that implements a utility object that generates data files with a variable number of elements with a standard RNG (random number generator When completing this assignment, the student should demonstrate mastery of the following concepts File I/O Utility objects Pseudo-Random Number Generation Stack Limitations and Dynamic Memory Assignment rite a C++ utility object that can be used to generate data files that contain random numbers Upon instantiation, your object should contain a Create the following interface for a method method takes two argument somewhere within your object bool NumberFileGenerator :generate File(char* filename, int elementCount This method takes two arguments The first char is a character pointer that should contain a reference to a null-terminated string containing the name that will be placed on the data file The second int argument represents how many random numbers will be placed in the data file with the provided name The bool return value should be true if the file was successfully created and populated with data In the event that something went wrong (the file didn't you should have open, you ran out of memory, the file already exists etc...), this method return a value of false Each integer element in the data file should be delimited with a single 'In character to push the cursor to a newline for aesthetic and organizational reasons (this is your file format) In addition to creating the data file and populating it with random numeric data, your object should retain a memory of the last list of numbers placed on the most recently created data file Additionally, a method should be reated that dumps this data on the console upon request from the client code When writing your test driver instantiate a single instance of your object Create file that contain 5000, 10000, 100000, and 500000 elements respectively with arbitrary yet intuitive names After creating each file, show the contents on the screen Please keep in mind that static allocation will work for the first 3 files provided (i.ee. an array of integers locally declared should be able to contain the numbers) However, when you attempt to create a list with 500000 elements, there is a good chance your program will not work because this much information cannot be locally declared on the stack You will need to make a dynamic memory allocation for the purpose of housing the numbers during the display Assessment This assignment will be assessed based on the provided grading rubricExplanation / Answer
/*Write a C++ utility object that can be used to generate data files that contain random numbers.
Upon instantiation, your object should contain a method takes two arguments.
Create the following interface for a method somewhere within your object:*/
#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
#include<time.h> /* time */
using namespace std;
//define class NumberFileGenerator
class NumberFileGenerator
{
char *Filename;
int Lastlist[2];
public:
bool generateFile(char *fileName,int elementCount);
void getNameandLastnumbers(char *s,int *list);
int dumpDataOnconsole();
};
void NumberFileGenerator::getNameandLastnumbers(char *s,int *list)
{
strcpy(s,Filename);
list[0] = Lastlist[0];
list[1] = Lastlist[1];
}
//define function
bool NumberFileGenerator::generateFile(char *fileName,int elementCount)
{
//create out object of type ofstream
ofstream out;
//create element dynamically
int *element;
/* initialize random seed: */
srand (time(NULL));
out.open(fileName);
if(!out)
{
cout<<"Not able to open file... "<<endl;
return false;
}
//create random numbers of elementCount
element = new int[elementCount];
for(int i = 0 ; i < elementCount; i ++)
{
element[i] = rand() ;
out<<element[i]<<endl;
}
//steor file name for object
Filename = new char[strlen(fileName)];
strcpy(Filename,fileName);
//store last two elements on file
Lastlist[0] = element[elementCount-1];
Lastlist[1] = element[elementCount-2];
out.close();
return true;
}
int NumberFileGenerator::dumpDataOnconsole()
{
ifstream in;
in.open(Filename);
if(!in)
{
cout<<"Not able to open file... "<<endl;
return -1;
}
int element, i= 0 ;
while(!in.eof())
{
in>>element;
cout<<"Random number "<< i++ << " = " <<element<<endl;
}
in.close();
return 0;
}
int main()
{
//create 4 objects of type NumberFileGenerator to hold 5000,10000,100000,500000
NumberFileGenerator fileGenerator[4];
char s[4][20]={"FileFiveThosand.txt","FileTenThousand.txt","FileOneLakh.txt","FileFiftyLakh.txt"};
int elementCount[4]={ 5000,10000,100000,5000000};
char ch;
for( int i = 0; i < 4; i++)
{
fileGenerator[i].generateFile(s[i],elementCount[i]);
}
//check for filename and lastelemnts saved on file
char str[20];
int list[2];
for( int i = 0; i < 4; i++)
{
fileGenerator[i].getNameandLastnumbers(str,list);
cout<<"Name of the file : "<<str<<endl;
cout<<"Last element saved on file : "<<list[0]<<" and "<<list[1]<<endl;
}
//want to output to file
for( int i = 0; i < 4; i++)
{
cout<<"Want to print to console y or n : ";
cin>>ch;
if( ch == 'y' )
{
fileGenerator[i].dumpDataOnconsole();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.