Question: DIY (50%) Stats Module Create a module called Stats that can represent a file on the hard disk containing comma-separated numbers. This module should be
DIY (50%) Stats Module
Create a module called Stats that can represent a file on the hard disk containing comma-separated numbers.
This module should be able to load the numbers and perform the following tasks.
- A Stats object should be able to display the numbers in a tabular format on ostream in a specified number of columns, column width and precision.
- A Stats object should be able to display the numbers that fall within a specific range and the number of their occurrence.
- A Stats object should be able to sort the numbers in an ascending or descending order.
- Copying a Stats object should be done safely and doing so it should also copy the data file on the hard disk. The new file name should be the same as the original file with an added "C_" prefix.
- Assigning a Stats object to another should be done safely and the content of the target file should also be overwritten by the source file.
- A Stats object should also make the numbers available to the user program by indexing like an array.
- A Stats object should be able to receive the data file name from the istream and load the data from the file on the hard drive.
Required public functionalities
construction
3 Argument Constructor
Stats(unsigned columnWidth = 15, unsigned noOfColumns = 4, unsigned precision = 2);
- columnWidth: width of each column in characters
- noOfColumns: number of columns to display the numbers in
- precision: number of digits after the decimal point for each number
In this case, the "Stats" object is not tied to any file on the hard drive and is empty.
4 Argument Constructor
Stats(const char* filename, unsigned columnWidth = 15, unsigned noOfColumns = 4, unsigned precision = 2);
Same as the previous constructor, but in this case, if the filename argument is not null and is successfully opened, the numbers in the file will be loaded into the Stats object.
Copy Constructor
Copying a Stats object should be done safely and doing so it should also copy the data file on the hard disk. The new file name should be the same as the original file with an added "C_" prefix.
Operator overloads
Copy assignment
Assigning a Stats object to another should be done safely and the content of the target file should also be overwritten by the content of the source file.
Index operator overloads
double& operator[](unsigned idx);
Returns the reference of the number at idx index. If the index exceeds the size of the array, it should loop back to the beginning. For example, if the array size is 10, index 10 will be the reference of the element at index 0 and index 11 will be the reference of the element at index 1.
If the Stats object is empty, it should return the reference of a dummy double member variable.
double operator[](unsigned idx)const;
Returns the value of the number at idx index. If the index exceeds the size of the array, it should loop back to the beginning. For example, if the array size is 10, index 10 will be the value of the element at index 0 and index 11 will be the value of the element at index 1.
If the Stats object is empty, it should return zero.
Boolean type conversion overload
If Stats is casted to boolean, it should return true only if it is not empty. Otherwise, it should return false.
Methods (Member variables)
sort()
void sort(bool ascending);
Sorts the numbers in ascending or descending order.
size()
unsigned size()const;
returns the number of numbers in the Stats object.
name()
const char* name()const;
returns the name of the file tied to the Stats object.
occurrence()
unsigned occurrence(double min, double max, std::ostream& ostr = std::cout);
displays the numbers that fall within a specific range and the number of their occurrence on ostream.
The format of the printout should be the same as printing all the numbers.
helper implementations
ostream insertion operator<<
Stats should be printable by ostream (cout) using the operator<<.
A Stats object should be able to display the numbers in a tabular format on ostream in a specified number of columns, column width and precision. See the execution sample file.
istream extraction operator>>
Stats object should be able to receive the data file name from ostream (cin) using operator>>. After receiving the name, if the data file is open successfully, the numbers should be loaded into the Stats object.
The tester program:
Modify the tester program to test all the different circumstances/cases of the application if desired and note that the professor's tester may have many more samples than the tester program here.
// Workshop 6-diy: // Version: 0.9 // Date: 2021/02/20 // Author: Fardad Soleimanloo // Description: // This file tests the diy section of your workshop ///////////////////////////////////////////// #include#include #include #include "Stats.h" using namespace sdds; using namespace std; void twentyNumbers(Stats T); void badIndex(const Stats& T); void Copy(const string& dest, const string& source); void Dump(const string& filename); int main() { Copy("numLtl.csv", "numbersLtlOriginal.csv"); Copy("numbers.csv", "numbersOriginal.csv"); Stats L(10, 7, 0); Stats S("numLtl.csv"), Empty, BadFilename("badFilename"); cout << "Enter the data file name: "; cin >> L; cout << L << endl; cout << S << endl; twentyNumbers(L); twentyNumbers(S); cout << "Total of " << L.size() << " numbers in " << L.name() << endl; L[1000] = 11111.11; L.occurrence(-12345.0, 31342.55); L.sort(true); cout << "Sorted Ascending: " << endl; L.occurrence(-12345.0, 31342.55); L.sort(false); cout << "Sorted Descending: " << endl; L.occurrence(-12345.0, 31342.55); L = S; cout << L << endl; cout << "============================================================" << endl; Dump("numbers.csv"); Dump("numLtl.csv"); Dump("C_numbers.csv"); Dump("C_numLtl.csv"); cout << "Empty object tests: " << endl; cout << "*" << Empty << BadFilename << "*" << endl; cout << Empty[10] << endl; badIndex(Empty); Empty.sort(true); Empty.occurrence(1, 2); cout << Empty.size() << endl; return 0; } void twentyNumbers(Stats T) { if (T) { cout << ">>> 20 numbers of : " << T.name() << endl; cout.setf(ios::fixed); cout.precision(1); for (unsigned i = 0; i < 200;i+=10) { cout << "T[" << i << "]: " << T[i] << endl; } cout.unsetf(ios::fixed); } else { cout << "Nothing to print!" << endl; } cout << endl << "-------------------------------------------------------------" << endl; } void Dump(const string& filename) { cout << "DUMP---------------------------------------------------------" << endl; cout << ">>>" << filename << "<<<" << endl ; ifstream fin(filename.c_str()); int i = 0; char ch; while (fin.get(ch)) { if (i++ % 80 == 0) cout << endl; cout << ch; } cout << endl << "-------------------------------------------------------------" << endl; } void Copy(const string& dest, const string& source) { ifstream fin(source.c_str()); ofstream fout(dest.c_str()); char ch; while (fin.get(ch)) fout << ch; } void badIndex(const Stats& T) { cout << T[10] << endl; }
Execution output sample
Output Sample
Reflection
Study your final solutions for each deliverable of the workshop, reread the related parts of the course notes, and make sure that you have understood the concepts covered by this workshop. This should take no less than 30 minutes of your time and the result is suggested to be at least 150 words in length.
Create a file named reflect.txt that contains your detailed description of the topics that you have learned in completing this workshop and mention any issues that caused you difficulty.
You may be asked to talk about your reflection (as a presentation) in class.
DIY Submission (part 2)
Files to submit:
cstirng.cpp cstring.h Stats.cpp Stats.h w6p2_tester.cpp
Upload your source code to your matrix account. Compile and run your code using the g++ compiler as shown above and make sure that everything works properly.
Then, run the following command from your account
- replace profname.proflastname with your professors Seneca user-id
- replace # with the workshop number
- replace ?? with your subject code (200 or 244)
~profname.proflastname/submit 2??/w#/p2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
