Question: Convert C++ program spaghetti code to modular code Please add comments Program: #include #include #include #include using namespace std; int main() { string fn; char

Convert C++ program spaghetti code to modular code

Please add comments

Program:

#include  #include  #include  #include  using namespace std; int main() { string fn; char c; 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; // Ask user for filename and open it for reading in binary mode cout<<"Enter filename: "; cin >> fn; ifstream ifs; 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 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 for (int i = 0; i < 256; i++) if (counters[i] > max) max = counters[i]; // 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 << "*"; cout << endl; } cout << " Max count: " << max; cout << " Legend: * = " << round(max / 100.0) << " bytes "; cout << " Total: " << totalbytes << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!