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

A Huffman code can be used to compress a file. It replaces fixed-size, one-byte

ID: 3667033 • Letter: A

Question

A Huffman code can be used to compress a file. It replaces fixed-size, one-byte characters by variablelength codes (strings of bits). The codes for the most frequently used characters will be shorter than 8 bits, those for unusual characters will be longer. No codes are generated for characters that are not used. The input file is read twice, once to generate the code, then again to encode the file. In this assignments, you will analyze the frequencies of the characters that appear in a text file that will, eventually, be compressed. 1.Create a simple class named Tally. Data members should include an array of 256 integer counters, for tallying occurrences of the 256 possible ASCII characters, a counter for the number of input characters, and an ifstream& for the input file. Define a constructor, a print function, and a “worker” function named doTally. 2. Write a main program that calls banner() at the beginning. Open the input file and use the open stream as a parameter to instantiate the Tally class. Call doTally(). Print the results of the tally. Call bye() and end i want it as a C++ program with simple and understandable programing.

Explanation / Answer

As per problem statement written the c++ code ..see the below code will read the data from the file and count
the number of character and will store the result into counter[256].

Code:

#include<iostream>
#include<ifstream>
#include<fstream>
using namespace std;

class Tally
{
   int counters[256];
   int counter;
   ifstream infile;
public:
   Tally()
   {
       //counter = 0;
   }
   void print()
   {
       cout<<"Print the Tally Class data members";
   }
  
   void doTally(string fileName)
   {
       infile.open(fileName);
       int i = 0;
       char line;
       cout << "Reading from the file" << endl;
       while(!infile.eof())
       {
           infile >> line;
           counters[i] = line;
           counter++;
           i++;
       }
   }
  
};

void banner()
{
   cout<<"Welcome to Tally class"<<endl;
}
void bye()
{
   cout<<"Good bye"<<endl;
}
int main()
{
   banner()
   Tally obj;
   obj.doTally("afile.dat");
   obj.print();  
   bye()
   return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote