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

How to solve this problem using c++ 4. [40 points] File ilo Write a complete pro

ID: 3904440 • Letter: H

Question

How to solve this problem using c++

4. [40 points] File ilo Write a complete program starting from #include. A file called input.txt contains words separated by spaces. We will assume everything is lowercase and there are no punctuations. Ask the user for a file name for an output file. Write to the output file the list of words occurring in the input.txt file and how many times each word occurred. Each word should only appear once in the list. Format the output so that the width of the first column is 10 and the width of the second column is 4. Sample output: aardvark 5 bruin 9 hello 2 You may write any functions and classes you wish. You may use any vectors or any algorithms from the algorithm library. Next two pages are left blank to give you plenty of room It is a good idea to pseudo code your idea here on this page. Even if you make some mistakes you can get partial credit if your pseudo code is reasonable.

Explanation / Answer

#include<iostream>                      //Header files

#include<fstream>

#include<iomanip>

using namespace std;

struct data             //Structure declared to save what words have occured

{

    string word;            //For the word

    int freq;               //For its frequency

};

int main()              //main() begins

{

    string output, temp;            //Variable declaration

    int l = 0, i;

    cout<<"Enter the output file name (including .txt): ";

    cin>>output;                    //Input the output file name

    ofstream ofile;                 //ofstream object

    ofile.open(output.c_str());     //Open the output file with name in the string output

    ifstream ifile;                 //ifstream object

    ifile.open("input.txt");        //Open the input file with name "input.txt"

    while(!ifile.eof())             //Loop to count the total number of words

    {                               //This will help to create an array of structure data of length l

        ifile>>temp;

        l++;

    }

    ifile.seekg(0, ios::beg);       //Move the pointer at the beginning of the file

    data dat[l];                    //Array of data with length l

    for(i = 0; i < l; i++)          //Loop for making the values of word as "" and freq as 0

    {

        dat[i].word = "";

        dat[i].freq = 0;

    }

    while(!ifile.eof())             //Loop for counting the frequency of each word

    {

        ifile>>temp;                //Read a word from file

        for(i = 0; i < l && dat[i].word != ""; i++)          //Loop to check if the word is already in the dat array

            if(temp == dat[i].word)

                break;

        //After the loop, value of ith word in the dat array will be either "" which means we

        //can add new word at this position or equal to temp which means both the words are same, and we can change its frequency

        dat[i].word = temp;

        dat[i].freq++;

    }

    for(i = 0; i < l && dat[i].word != ""; i++)        //Loop to save the words and frequency in the output file

        ofile<<setw(10)<<dat[i].word<<setw(4)<<dat[i].freq<<endl;           //setw(w) sets the width as w characters

    ifile.close();                  //Close input file

    ofile.close();                  //Close output file

    return(0);

}                                   //main() ends

Some sample test runs are also shown below.

Test Run 1:

input.txt:

abcdef ghi xyz xyz xyz abcdef ghi xyz kkk abcdef ghi ghi abcdef

Console: (User input is in bold)

Enter the output file name (including .txt): result.txt

result.txt

    abcdef   4

       ghi   4

       xyz   4

       kkk   1

Test Run 2:

input.txt:

abc def

Console: (User input is in bold)

Enter the output file name (including .txt): result.txt

result.txt

       abc   1

       def   1

The width of columns are maintained as mentioned (10 and 4). It is not visible here because of different font.

Kindly give a thumbs up, if found useful. Comment for queries. :)

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