Question
Introduction and Overview The project requires writing, reading, and handling a large data set. An array of random values is generated and saved to a
Introduction and Overview
The project requires writing, reading, and handling a large data set. An array of random values is generated and saved to a file in binary format. The file is read into memory and sorted. Then the data is analyzed by a series of related classes, and various statistics are output.
The analysis includes the number of random values not generated, the number of values generated multiple times, minimum, maximum, mean, median, and mode values. Finally, a small set of random numbers is generated and searched for using a binary search algorithm.
Part 1 - Writing and Reading Files
In this first part of the signature assignment, develop code to write and read a binary file.
Write function named createArray() that creates a dynamic array of 1000 integers. The calling function must supply a variable for the length received as a pointer and assign the value of 1000 in the createArray() function. It is best to define this value as a constant outside the function. Initialize the array with random numbers between 0 and 999 inclusive. Assign the caller's pointer parameter to the array's length and return a pointer to the dynamically created array.
Write second function named writeBinary() that takes a parameter pointer to an integer array and its length as a second parameter. Open an output file "binary.dat" as binary and write the array's length to it. Then, write each integer in the array to the file and close the file.
Write third function named readBinary() that opens the "binary.dat" file in binary mode. Its only parameter should be a reference to an integer in the main code, which will store the length of the returned array. Read the first value from the file, which is the length of an array to create. Assign this value to the reference parameter so the caller's variable will be updated. Please create a new dynamic array of integers using the length value for its size. In a loop, read in each number from the file and save the value in the array, increasing the index for each. Return a pointer to the array from the function.
Finally, write main function that calls the array creation function, supplying the address of a local integer variable as a parameter representing the array's length. Receive back a pointer to the array with the length variable updated. Call the writeBinary() function, passing a pointer to the array as the first parameter and its length as a second parameter. After it returns, delete the array created in the first function and set the integer pointer to nullptr.
Call the readBinary() function, passing a reference to a local integer variable, which will be the array length. The third function should return a pointer to a new array and update the variable with the length. Write loop to iterate over each value in the array and print out its value separated by a space. Delete the dynamic array and terminate the program.
Use these function headers:
int* createArray(int length)
void writeBinary(int* values, int length)
int* readBinary(int& length)
Example output:
383 886 777 915 793 335 386 492 649 421 362 27 690 59 763 926
...
308 593 278 197 555 672 774 445 0 325 997 283 412 127 382 421
Part 2 - Developing Classes
In this second part of the signature assignment, develop code for classes to read and analyze binary data.
Write function named createBinaryFile(). Place the code for creating the array of random values in it. In it, add a call to the existing writeBinary() function. Modify writeBinary() to take an additional parameter: the file name. Call writeBinary(), passing the name as "binary.dat," a pointer to the array, and the size. A constant named SIZE is recommended to be created and assigned to the value 1000. This simplifies reading the code.
Write class called BinaryReader. It should have instance variables of an integer pointer and an integer. The pointer will point to an array of integers; the integer will hold the array's size. Add a private method called readValues(), which uses the readBinary() code to input the size and the values of an array of integers. Eliminate the existing readBinary() function. Add a destructor to delete the integer array. Then, add two public methods named getValues() and getSize() that return the instance variables. Finally, add a constructor that takes a single parameter, a constant character pointer to a file name. The constructor should call the readValues() method to populate the array.
Write second class called Analyzer. This class has two instance variables, an integer pointer and an integer representing an array of integers and the array's size. Add a constructor that takes two parameters, an integer pointer, and an integer; initialize the instance variables from these. Add a private cloneValues() method that creates an integer array and copies the values from the parameter array. The constructor should call the cloneValues() method. Add a destructor to delete the array. Add an analyze() method. This method should determine the mean (mathematical average), minimum value, and maximum value from the values in the array. Create a string that holds this information and returns it from the function.
Use these function and class headers:
class Analyzer
Analyzer(int* values, int size)
~Analyzer()
int* cloneValues(int*, int)
std::string analyze()
class BinaryReader
BinaryReader(const char* filename)
~BinaryReader()
void readValues(const char*)
int* getValues()
int getSize()
void createBinaryFile(std::string filename)
void writeBinary(std::string filename, int* values, int length)
Example output:
The minimum value is 0
The maximum value is 999
The mean value is 499.495
Part 3 - Implementing an Inheritance Hierarchy
In this third part of the signature assignment, develop code for additional classes to analyze the data.
Modify the Analyzer class to make it a superclass. Change the analyze() method to make a pure virtual function. Although Analyzer has a virtual function, the destructor does not need to be virtual since the Analyzer class owns the integer array; subclasses will not own the array. Change the values and size variables from private to protected.
Create a class called DuplicatesAnalyser that subclasses Analyzer. This class will scan the data and determine which values appear more than once. Add a constructor that takes an integer array and its size. Implement the analyze() method to count duplicated values; use the override keyword appropriately. Return a std::string with explanatory text and the count of duplicates. Only some of the values will be duplicated; others will not exist at all or appear only once.
Create a second class called MissingAnalyzer that subclasses Analyzer. This class will scan the data and determine which values should not appear. Implement the analyze() method to count the missing values; use the override keyword appropriately. Return a std::string with the count.
Create a third class called StatisticsAnalyzer, which performs the same statistical data analysis as the Analyzer class did in Part 2. This includes the minimum, maximum, and mean values. Implement this analysis in the analyze() method; use the override keyword appropriately.
Use these function and class headers:
class Analyzer
Analyzer(int* values, int size)
~Analyzer()
virtual std::string analyze() = 0;
class DuplicatesAnalyzer : public Analyzer
DuplicatesAnalyzer(int* values, int size)
std::string analyze() override
class MissingAnalyzer : public Analyzer
MissingAnalyzer(int* values, int size)
std::string analyze() override
class StatisticsAnalyzer : public Analyzer
StatisticsAnalyzer(int* values, int size)
std::string analyze() override
Example output:
The minimum value is 0
The maximum value is 999
The mean value is 499.495
There were 269 duplicated values
There were 361 missing values
Part 4 - Sorting and Searching Data
In this fourth part of the signature assignment, develop code for classes to read and analyze binary data.
Write function named selection_sort() which takes two parameters, a integer pointer and an integer size. Add code to implement a selection sort algorithm. Add any needed functions to support the selection sort.
Write helper function named binary_search that takes three parameters, the first is an integer array, the second is the searched for value, and the third is the array size. This function calls the recursive function and passes the array, the search value, the starting index, and the last index.
Write recursive function named binary_search_recursive() which takes four parameters from the helper function. The first is an integer pointer representing the array of values, the second is an integer representing the value to be searched for, the third is an integer for the starting index, and the fourth is an integer for the ending index.
Add a new class named SearchAnalyzer which subclasses Analyzer and implements the analyze() method. Use the override keyword appropriately. The constructor should first call the selection_sort() function, passing the integer array and the size. In the analyze() method, generate 100 random integer values from 0 to 999 and use the binary_search() function to see if that value exists in the data. Count the number of found values and return the count in a std::string.
Modify the StatisticsAnalyzer analyze() method to invoke the selection_sort() function. Alter the computation of the minimum and maximum values, considering that the array is sorted. Add functionality to implement the median and mode averages using the sorted data. The median is the exact middle value (if there is an odd number of elements) and the mean of the two middle values (if there is an even number of elements). The mode is the most frequently occurring value; if more than one value occurs with this frequency, pick the first one.
Use these function and class headers:
class SearchAnalyzer : public Analyzer
SearchAnalyzer(int* values, int size)
std::string analyze();
bool binary_search_recursive(int* values, int key, int start, int end)
bool binary_search(int* values, int key, int size)
void selection_sort(int* values, int size)
Example output:
The minimum value is 0
The maximum value is 999
The mean value is 499.495000
The median value is 497
The mode value is 81 which occurred 5 times
There were 269 duplicated values
There were 361 missing values
There were 67 out of 100 random values found
Part 5 - Putting It All Together
Write and debug the above parts, ensuring that each part works, compiles with no errors or warnings, and that the results look like the Part4 output (note: your values will vary, although most of the results should be similar).
Use these function and class headers:
bool binary_search_recursive(int* values, int key, int start, int end)
bool binary_search(int* values, int key, int size)
void createBinaryFile(std::string filename)
void selection_sort(int* values, int size)
void writeBinary(std::string filename, int* values, int length)
class BinaryReader
BinaryReader(std::string filename)
~BinaryReader()
int* getValues()
int getSize()
void readValues(std::string)
class Analyzer
Analyzer(int* values, int size)
~Analyzer()
int* cloneValues(int*, int)
virtual std::string analyze() = 0
class DuplicatesAnalyser : public Analyzer
DuplicatesAnalyser(int* values, int size)
std::string analyze() override
class MissingAnalyser
MissingAnalyser(int* values, int size)
std::string analyze() override
class SearchAnalyzer : public Analyzer
SearchAnalyzer(int* values, int size)
std::string analyze() override
class StatisticsAnalyser
StatisticsAnalyser(int* values, int size)
std::string analyze() override
Design, edit, run, test, and debug your program. Enter the completed C++ code here:
C++ code for the lab project: |
|
Run the code, take a screenshot of the results, and insert the screenshot here:
Screenshot of the results: |
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started