Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include #include #include \"DataStore.hpp\" /** * Construct an empty DataStore.

ID: 3746629 • Letter: #

Question

#include
#include

#include "DataStore.hpp"

/**
* Construct an empty DataStore.
*/
DataStore::DataStore() {
// TODO: Handle any default initialization you need here.
  
}

/**
* Construct a DataStore that was saved to a file.
* @param filename - name of file containing data to load.
*/
DataStore::DataStore(std::string filename) {
// TODO: Read your output file into your program.
// This cannot be done until you decide how you want to
// format your file.
  
}

/**
* Destructor: cleanup any allocated memory.
*/
DataStore::~DataStore() {
// TODO: Complete the destructor.
// Note: you may remove this entirely (here and in .hpp) if your data is
// stored entirely in automatic variables.
}

Just the Constructor of both data store and the Destructor i need help with

Explanation / Answer

#include "DataStore.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;

// Default constructor definition
DataStore::DataStore()
{
// Sets the maximum size to 10
maximum = 20;
// Dynamically allocate memory
data = new int[maximum];
// Sets the current position to 0
current = 0;
}// End of default constructor

// Parameterize constructor to read file and store data
DataStore::DataStore(string fileName)
{
// Counter for number of records. Initializes to zero
int counter = 0;
int no;
// Initializes current position to 0
current = 0;
// Declare ifstream objects
ifstream inF;

// Open product file for reading
inF.open(fileName.c_str());

// Checks whether file can be opened or not
if(inF.fail())
{
cout << " ERROR: Unable to open the file "<<fileName<<" for reading." << endl;
exit(0);
}// End of if condition

// Loops till not end of file
while (!inF.eof())
{
// Reads number
inF>>no;
// Increase the record counter by one
counter++;
}// End of while loop
// Close the file
inF.close();
// Sets the maximum size to number of records in file
maximum = counter;
// Dynamically allocates memory of size maximum
data = new int[maximum];

// Re - open product file for reading
inF.open(fileName.c_str());

// Loops till not end of file
while (!inF.eof())
{
// Reads number
inF>>no;
// Check if the number is between -1000 and +1000
if(no >= -1000 && no <= 1000)
{
// Calls the function get the frequency of the parameter number
// Checks if the frequency of the number is greater than 100000 then display error
if(get_count(no) > 100000)
cout<<"ERROR: Number cannot be duplicate more than 100000 times.";
// Otherwise valid
else
{
// Adds the number at current position
data[current] = no;
// Increase the record counter by one
current++;
}// End of else
}// End of if condition
// Otherwise invalid number
else
cout<<" ERROR: Number must be between -1000 and +1000. Your number is: "<<no;
}// End of while loop
}// End of parameterized constructor

// Defines destructor
DataStore::~DataStore()
{
delete data;
}// End of destructor

// Function to add a number in list
void DataStore::add_value(const int& no)
{
// Checks if the current value is equals to maximum then display memory full
if(current == maximum)
cout<<" ERROR: Memory full.";
// Otherwise check if the number is between -1000 and +1000
else if(no >= -1000 && no <= 1000)
{
// Calls the function get the frequency of the parameter number
// Checks if the frequency of the number is greater than 100000 then display error
if(get_count(no) > 100000)
cout<<"ERROR: Number cannot be duplicate more than 100000 times.";
// Otherwise valid
else
{
// Adds the number at current position
data[current] = no;
// Increase the record counter by one
current++;
}// End of else
}// End of else if
// Otherwise invalid number
else
cout<<" ERROR: Number must be between -1000 and +1000. Your number is: "<<no;
}// End of function

// Function to remove a number from list
bool DataStore::remove_value(const int &no)
{
// Sets the found status to -1
int found = -1;
// Checks if current counter is zero then display error message
if(current == 0)
cout<<" ERROR: List is empty.";
// Otherwise list contains data
else
{
// Loops till end of the list
for(int x = 0; x < current;)
{
// Checks if the current number is equals to the parameter number
if(data[x] == no)
{
// Sets the found status to x index position value
found = x;
// Loops from found position to the end of the list
for(int y = found; y <= current; y++)
// Shift data to previous
data[y] = data[y+1];
// Decrease the counter by one
current--;
}// End of if condition
// Otherwise number not matched
else
// Increase the loop counter by one
x++;
}// End of for loop
}// End of else

// Checks if found value is -1 return false
if(found == -1)
return false;
// Otherwise return true
else
return true;
}// End of function

// Function to return number frequency
int DataStore::get_count(const int &no)
{
// Initialize the frequency counter to 0
int counter = 0;
// Checks if the current value is zero then return 0
if(current == 0)
return counter;
// Otherwise loops till end of the list
for(int x = 0; x < current; x++)
// Checks if the current index position number is equals to parameter number
if(data[x] == no)
// Increase the counter by one
counter++;
// Returns the counter
return counter;
}// End of function

// Function to return the concatenates number string format
string DataStore::to_string()
{
string result = "";
// Loops till end of the list
for(int x = 0; x < current; x++)
{
// Declaring output string stream
ostringstream temp;
// Sending a number as a stream into output string
temp << data[x];
// the str() coverts number into string
string conv = temp.str();
// Concatenates the string format of the number with comma
result = result + conv + ", ";
}// End of for loop
// Returns the result string
return result;
}// End of function

// Function to write number to file
void DataStore::write_out(string fileName)
{
// Declare ofstream objects
ofstream outF;

// Open product file for writing
outF.open(fileName.c_str());

// Checks whether file can be opened or not
if(outF.fail())
{
cout << " ERROR: Unable to open the file "<<fileName<<" for writing." << endl;
exit(0);
}// End of if condition

// Loop till current counter
for(int x = 0; x < current; x++)
// Checks for last record
if(x == current-1)
// Writes data without new line
outF<<data[x];
// Otherwise writes data with new line
else
outF<<data[x]<<endl;
}// End of function

// main function definition
int main()
{
// Using parameterized constructor read file contents and store data
DataStore storage("numbers.txt");
// Calls the function displays data
cout<<storage.to_string();
cout<<" Count 2: "<<storage.get_count(2)<<endl;
storage.remove_value(2);
cout<<" After removing 2: "<<storage.to_string();
cout<<" Count 2: "<<storage.get_count(2);
cout<<" Before adding 10";
cout<<" Count 10: "<<storage.get_count(10);
cout<<" Adding 10";
storage.add_value(10);
cout<<" Adding 10";
storage.add_value(10);
cout<<" Adding 10";
storage.add_value(10);
cout<<" Count 10: "<<storage.get_count(10);
storage.remove_value(-15);
cout<<" After removing -15: "<<storage.to_string();
cout<<" Adding -10001";
storage.add_value(-10001);
cout<<" Adding 10001";
storage.add_value(10001);
cout<<" Adding 90";
storage.add_value(90);
cout<<" Adding 90";
storage.add_value(90);
storage.write_out("Frequency.txt");
}// End of main function

Sample Output:

5, 10, -15, 5, 5, 3, 3, 2, 2, 2, 10,
Count 2: 3

After removing 2: 5, 10, -15, 5, 5, 3, 3, 10,
Count 2: 0
Before adding 10
Count 10: 2
Adding 10
Adding 10
Adding 10
Count 10: 5
After removing -15: 5, 10, 5, 5, 3, 3, 10, 10, 10, 10,
Adding -10001
ERROR: Number must be between -1000 and +1000.
Your number is: -10001
Adding 10001
ERROR: Number must be between -1000 and +1000.
Your number is: 10001
Adding 90
Adding 90
ERROR: Memory full.

File numbers.txt contents

5
10
-15
5
5
3
3
2
2
2
10

File Frequency.txt contents

5
10
5
5
3
3
10
10
10
10
90