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

C++ user defined functions and arrays. I need to write a function to give me the

ID: 3774544 • Letter: C

Question

C++ user defined functions and arrays.

I need to write a function to give me the character stats of an input file and I don't know how to go about doing it. I have "hit the wall" with this part. I also cannot figure how to format the output. I hope I have provided all the information needed for assistance. THANK YOU!

What I need:

- Count/sum of alpha character occurrences. (not alphanumeric, nor special characters, only a-z)

OUTPUT EXAMPLE:  Number of chars = 269

- A list of the top five most used alpha characters (only a-z)

OUTPUT EXAMPLE:

Five Most Used Characters:
'E' 33 counted
'N' 26 counted
'T' 25 counted
'A' 24 counted
'R' 21 counted

- A list of the Characters not used.

OUTPUT EXAMPLE:

Characters Not Used:   J, Q, Z

- My function voidwriteTotal  should output to the file in 3 collumns, but I can only manged to output in one single column. I need it to look like this:

Character Occurence
- - - - - - - - - - - -
A count = 24    B count =   2     C count =   5
D count =   8    E count = 33   F count =   7
G count =   4   H count = 10    I count = 19
J count =   0   K count =   2    L count = 18
M count =   6   N count = 26   O count = 18
P count =   5   Q count =   0    R count = 21
S count = 11 T count = 25   U count = 13
V count =   1   W count =   2    X count =   2
Y count =   7 Z count =   0

---------------MY C++ CODE FOLLOWS (code runs without bugs. It will ask you for your input file and the name of your output file)-----------

#include
#include
#include
#include
#include

using namespace std;


void initialize(int& lc, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount(char ch, int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);


int main()
{
///Variable declaration

    ifstream inFile;    //input file stream variable
    ofstream outFile;   //output file stream variable
    char inputFile[51]; //variable to store the name of the input file
    char outputFile[51];    //variable to store the name of the output file

    //Character Occurence
    int lineCount; //stores the line count
    int letterCount[26]; //store the letter count A-Z...0-26
    char ch; //store the characters

///Open the I/O files

    cout << "Enter the input file name: ";
    cin >> inputFile;
    cout << endl;
    inFile.open(inputFile);
    if (!inFile)
    {
        cout << "Cannot open the input file." << endl;
        return 1;
    }

    cout << "Enter the output file name: ";
    cin >> outputFile;
    cout << endl;
    outFile.open(outputFile);


    initialize(lineCount, letterCount);             //initialize lineCount & letterCount


    inFile.get(ch);                                 //read the first character
    while (inFile)
    {
        copyText(inFile, outFile, ch, letterCount); //process the lines
        lineCount++;                                //increment the line count
        inFile.get(ch);                             //read the next character
    }

    writeTotal(outFile, lineCount, letterCount);    //output # of lines and Char occurrence


    inFile.close();
    outFile.close();

    return 0;
}

void initialize(int& lc, int list[]) //initialize lineCount & letterCount to 0
{
    int j;
    lc = 0;

    for (j = 0; j < 26; j++)
        list[j] = 0;
}

void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]) //outputs Original message contents
{

    while (ch != ' ')      //process the entire line
    {
        outtext << ch;      //output the character

        characterCount(ch, list);   //call the function character count
        intext.get(ch);     //read the next character
    }
    outtext << ch;          //output the newline character
}

void characterCount(char ch, int list[]) //counting character occurrence
{
    int index;

    ch = toupper(ch);                       //convert letter to uppercase

    index = static_cast(ch)
            - static_cast('A');        //Find the index array for this letter

    if (0 <= index && index < 26)           //IF the index is valid, add it to the appropriate count
        list[index]++;
}

void writeTotal(ofstream& outtext, int lc, int list[]) //outputs to output file line & letter count
{
    int index;

    outtext << endl << endl;
    outtext << "Number of lines = " << lc << endl;

    outtext << endl << endl;
    outtext << "____________________________________________"<< endl;
    outtext << "Character Occurrence"<< endl;
    outtext << "-------------------------"<< endl;
    for (index = 0; index < 26; index++)
        outtext << setw(10)
        << static_cast(index + static_cast('A'))
        << " count = " << list[index] << endl;
        outtext << "____________________________________________"<< endl;
}

------------------------------------END OF C++ CODE-------------------------------------------------

Example of the input.txt is bellow this line .NOTE: The file must have a line return after the last character, in order for our example code to run properly. If you don’t have that blank line at the end of the input file, the program will not perform as intended and behave unpredictably.

---------------Input.txt begins--------------------------------

9 8 7 6 5 You must have a line return after the
last character in your message, in order
for our example code to run properly. Note
that there are 8 lines of text in this file
including an additional mandatory "blank line"...otherwise
your system may hang up and create huge output files
the intentional line return and final blank line follows.

-----------------END OF INPUT.TXT -------------------------

Explanation / Answer

Just change writeTotal as below and you will get required result..

void writeTotal(ofstream& outtext, int lc, int list[]) //outputs to output file line & letter count
{
   for( int i = 0 ; i < 26 ; i = i + 3)
   {
       if( i == 24 )
           outtext<<(char)(list[i]+'A')<<" count = "<< list[i]<<" "<<(char)(list[i+1] + 'A' )<< " count = "<<list[i+1]<<endl;
       else
       outtext<<(char)(list[i]+'A')<<" count = "<< list[i]<<" "<<(char)(list[i+1] + 'A') << " count = "<<list[i+1]<<" "<<(char)(list[i+2] + 'A') << " count = "<<list[i+2]<<endl;
   }
}

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