Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

#include #include #include #include const int ARRAY _ SIZE = 1 0 0 0 ; int * createArray ( int length ) { int *

#include
#include
#include
#include
const int ARRAY_SIZE =1000;
int* createArray(int length){
int* array = new int[length];
for (int i =0; i < length; ++i){
array[i]= rand()%1000; // Random numbers between 0 and 999 inclusive
}
return array;
}
void writeBinary(int* values, int length){
std::ofstream outputFile("binary.dat", std::ios::binary);
if (!outputFile){
std::cerr << "Error opening binary file for writing." << std::endl;
return;
}
outputFile.write(reinterpret_cast(&length), sizeof(length));
for (int i =0; i < length; ++i){
outputFile.write(reinterpret_cast(&values[i]), sizeof(values[i]));
}
outputFile.close();
}
int* readBinary(int& length){
std::ifstream inputFile("binary.dat", std::ios::binary);
if (!inputFile){
std::cerr << "Error opening binary file for reading." << std::endl;
return nullptr;
}
inputFile.read(reinterpret_cast(&length), sizeof(length));
int* array = new int[length];
for (int i =0; i < length; ++i){
inputFile.read(reinterpret_cast(&array[i]), sizeof(array[i]));
}
inputFile.close();
return array;
}
int main(){
srand(static_cast(time(nullptr)));
// Module 1
int arrayLength = ARRAY_SIZE;
int* dynamicArray = createArray(arrayLength);
writeBinary(dynamicArray, arrayLength);
delete[] dynamicArray;
dynamicArray = nullptr;
int localLength;
int* localArray = readBinary(localLength);
for (int i =0; i < localLength; ++i){
std::cout << localArray[i]<<"";
}
std::cout << std::endl;
delete[] localArray;
return 0;
}
*Please look at module 1 first before answering module 2 because it is connected the link is attached on top*------------ Module 2 Recommended Implementation Details
This second part of the signature assignment will rework the code to create the binary file. Then,
you will develop classes for reading and analyzing the binary data read from the file.
Write a function named createBinaryFile, which takes the name of a file as its first
parameter and the size of the data as the second parameter and returns nothing. Copy the code
from the createArray function for creating the array of random values into this function.
Modify writeBinary to take an additional string parameter; it is the file name. Add a call to
the existing writeBinary function and pass three parameters. When this code works
correctly, eliminate the createArray function since it is no longer needed.
Write a class called BinaryReader. It should have instance variables of an integer pointer
and an integer. The pointer will point to an array of integers; named values. The integer will
hold the arrays size, named size. Add a constructor that takes a single parameter, a constant
string reference to a file name. Add a private method called readValues with one parameter,
the name of the input file. Copy the code from the readBinary function to open the file, input
the array size into the size instance variable, and create the dynamic array using the values
instance variable. Now, read the values from the file into the array. Eliminate the existing
readBinary function. The constructor should call the readValues method to populate the
array and size instance variables. Add a destructor to delete the integer array. Finally, add
getValues and getSize as public methods that return the instance variables.
Write a second class called Analyzer. This class has two instance variables: an integer pointer
representing an array of integers named values and an integer representing the arrays size
named size. Add a constructor with two parameters: an integer pointer and an integer. Add a
private cloneValues method that creates an integer array, copies the values from the array
parameter into its elements, copies the size, and returns nothing. The constructor should call the
cloneValues method, passing it the two parameter values. Add a destructor to delete the
dynamic array. Add an analyze method that determines the mean (mathematical average),
minimum value, and maximum value from the values in the array. Be certain to return a double
value for the mean value. Return a string that holds this statistical information.
Update the main function to call create binary file, passing the name as binary.dat.
Create a BinaryReader instance and write a loop that iterates over the values from the
instance to ensure it works correctly. After it is working, the loop should be removed. Next,
create an instance of the Analyzer class, passing its constructor the values and size using the
BinaryReaders get methods for its values and size. Call the analyze method and print out
the results.
Use these func

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions