[Solved] C Templates Homework Using Template Class Read Data Input File Manipulate Example Input F Q37175727
C++ Templates
In this homework, you will be using a template class to read indata from an input file and manipulate it. Here is an example of aninput file:
aa bb ccdd ee ffgg hh iijj kk ll
In the above example of an input file, there are 4 rows and 3elements per row. Input files will store either strings orintegers. Rows will be on different lines and all elements will beseparated by a space. All strings will be lower case.
Your template class will be called SpecialArray. It willneed to contain the following private members:
- A pointer for a 2D dynamic array. The pointer will be oftemplated type.
- An integer variable for the number of rows in an inputfile.
- An integer variable for the number of columns in an input file(AKA the number of elements per row).
Your template class will also need the following publicmembers:
- A default constructor.
- A constructor which takes two integer parameters for the numberof rows and columns in an input file.
- A destructor.
- A function called readFile to read the elements infrom an input file and store them in your 2D dynamic array. Thisfunction will take the ifstream variable as a parameter(assume the variable already has an associated file open– I haveopen/closed the file in main/unit tests, so you will not need toworry about that.).
- A function called max with empty argument list thatreturns the value of the maximum element in the array.
- A function called min with empty argument list thatreturns the value of minimum element in the array.
- A function called sort with empty argument list whichsorts each row in the array (as in, each individual row will besorted from smallest to largest, with each element staying in itsoriginal row and each row staying in its original position).
- A function called print with empty argument list whichprints/cout’s the contents of the 2D dynamic array to the display.Elements in a row should be separated by spaces, and each rowshould be on a new line (just like in the input file).
- A function called saveToFile with empty argument listwhich outputs the contents of the 2D dynamic array, formatted asabove, to a file called “output.txt”.
In addition to writing the above SpecialArray class, youwill need to modify the main.cpp to achieve thefollowing.
- You will fill in the code for the measureLines andmeasureElementsPerLine functions(which count the number of rows inthe file and the number of elements per row, respectively). You mayassume that all rows will have the same number of elements, andthat the ifstream variable will be associated with an open filealready when it is passed to these two functions.
- Add throw clause in the above two functions. Inparticular, if a file associated with an ifstream variable passedto the above two functions does not exist, they will throw arun_time error which says “File does not exist.” If thesefunctions are passed an ifstream variable for an empty file, theywill throw a run_timeerror which says “File isempty.”
- Add try-catch construct in the main() as indicated. Do notalter main outside of the areas which are commented for you
main.cpp
#include “SpecialArray.h”
#include
#include
#include
using namespace std;
int measureElementsPerLine(ifstream& inFile) {
// Add your code here.
}
int measureLines(ifstream& inFile) {
// Add your code here.
}
int main()
{
int numOfLines, numOfElements;
string fileName, dataType;
cin >> fileName >> dataType;
ifstream inFile(fileName);
// Add try statement below
numOfElements = measureElementsPerLine(inFile);
inFile.close();
inFile.open(fileName);
numOfLines = measureLines(inFile);
inFile.close();
if (dataType == “int”){
SpecialArray specialArray(numOfLines, numOfElements);
inFile.open(fileName);
specialArray.readFile(inFile);
inFile.close();
specialArray.print();
specialArray.sort();
cout << “nSorted outputs: n”;
specialArray.print();
}
else if (dataType == “string”){
SpecialArray specialArray(numOfLines, numOfElements);
inFile.open(fileName);
specialArray.readFile(inFile);
inFile.close();
specialArray.print();
specialArray.sort();
cout << “nSorted outputs: n”;
specialArray.print();
}
// Add catch statement below
return 0;
}
SpecialArray.h
// Add headers here
#ifndef SpecialArray_H
#define SpecialArray_H
// Add your function declarations and definitions here. Withtemplate classes, it is recommended to put them in the samefile.
#endif
input1.txt
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
17 18 19 20
input2.txt
aA bB cC
fF eE dD
gG hH iI
lL kK jJ
mM nN oO
input3.txt(input3.txt is empty)
Expert Answer
Answer to C++ Templates In this homework, you will be using a template class to read in data from an input file and manipulate it…. . . .
OR

