Question: C++ Program: Please explain the 5 best examples of modularity in the program and why these are good examples of modularity. Source Code: #include #include
C++ Program:
Please explain the 5 best examples of modularity in the program and why these are good examples of modularity.
Source Code:
#include#include #include #include using namespace std; // Ask user for filename and open it for reading in binary mode void inputFileDetails(ifstream &ifs) { string fn; cout<<"Enter the exact location of the file & put filename extension (ex. .txt at the end): "; cin >> fn; ifs.open (fn, ios::in | ios::out | ios::binary); if (!ifs.is_open()) { cout << "File could not be opened!"; exit(1); } } // Read each character from file and increment count for that character by 1 void countTotalBytes(ifstream &ifs,int counters[],int& totalbytes) { char c; while (ifs.read(&c, 1)) { counters[(unsigned char) c]++; totalbytes++; } ifs.close(); } // Find the maximum value of all the character counters so that we can scale the distribution plot void findMax(int counters[],int &max) { for (int i = 0; i < 256; i++) if (counters[i] > max) max = counters[i]; } void displayDistribution(int counters[],int max) { // Plot the distribution after applying scaling for (int i = 0; i < 256; i++) { int stars = round (counters[i] * 100.0 / max); cout << setw(3) << i << " "; for (int i = 0; i < stars; i++) cout << "*"; if(stars !=0){//printing the total bytes if it is not 0 cout << stars <<" bytes"; } cout << endl; } } void displayStatistics(int max,int totalbytes) { cout << " Max count: " << max; cout << " Legend: * = " << round(max / 100.0) << " bytes "; cout << " Total: " << totalbytes << endl; } int main() { string fn; int counters[256], max = 0, totalbytes = 0; // keep track of how many of each char (0 to 255) in counters for (int i = 0; i < 256; i++) counters[i] = 0; ifstream ifs; //call functions inputFileDetails(ifs); countTotalBytes(ifs,counters,totalbytes); findMax(counters,max); displayDistribution(counters,max); displayStatistics(max,totalbytes); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
