Question

(PLEASE USE C++) (PLEASE USE THE EXAMPLE GIVEN) Question 1: [10 pointes] the input file inFile.txt...

(PLEASE USE C++)

(PLEASE USE THE EXAMPLE GIVEN)

Question 1: [10 pointes] the input file inFile.txt stores a number of words with possible replication. Each word is stored on a separate line. The number of words within inFile.txt is stored as the first line in that file.

Write and test a program that reads the words from inFile.txt and prints them in outFile.txt  without replication as well as the number of replication for each word.

Hint: to implement the above program, one must use a table of pointers to C-Strings as well as an array of counters; each counter keeps track of the count of a given word among all words within the input file.

Input File (inFile.txt)

5

smith

sara

john

john

smith

Output File (outFile.txt)

smith     2

john      2

sara      1

Question 2 [10 pointes] repeat Question 1 using an array of object strings as well as an array of counters and file IO.

Homework Answers

Answer #1

C++ CODE:

#include <bits/stdc++.h>     
#include <string>
#include <fstream>      //for dat file handling
using namespace std;

int main()
{
   fstream file1("input.txt");              // reading input from file
   string str;
   int str_len = 0;
   bool check_string_found = 0;
   string *arr;
   int *counters;

   int size;

   file1 >> size;

   arr = new string[size];
   counters = new int[size];

   while (file1 >> str)
   {
       if(str.length()!=0)
       {
            check_string_found = 0;             // bool type variable to store whether the string is equal or not

            for (int i = 0;i < str_len;i++)
            {
                if (str == arr[i])              //checking both strings are same or not
                {
                    check_string_found = 1;
                    counters[i]++;
                }
            }
            if (!check_string_found)              //  checking string is found or not
            {
                counters[str_len] = 1;
                arr[str_len++] = str;
            }
       }
   }

   fstream file12("output.txt");            // writing output into file

   for (int i = 0;i < str_len;i++)          //printing frequency of each string
   {
       file12 << arr[i] << ": " << counters[i] << endl;
   }
   file1.close();                           //closing file
   file12.close();                          //closing file
   delete[] arr;                            //dealloacting memory
   delete[] counters;                       //dealloacting memory
   return 0;
}

Input:

Output:

If you have any doubt feel free to ask and if you like the answer please upvote it .

Thanks

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please write a program that reads the file you specify and calculates how many times each...
Please write a program that reads the file you specify and calculates how many times each word stored in the file appears. However, ignore non-alphabetic words and convert uppercase letters to lowercase letters. For example, all's, Alls, alls are considered to be the same words. What is the output of the Python program when the input file is specified as "proverbs.txt"? That is, in your answer, include the source codes of your word counter program and its output. <proverbs.txt> All's...
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...
I have a list of things for review in C programming, could you please give me...
I have a list of things for review in C programming, could you please give me an example and a brief explanation for each question... Thank you very much 5. Create functions that can return pointers or functions that can return structs 6. Analyze code that uses predefined strings such as  strlen, strcat, strncat, and  strtok 7. Create syntax that can perform file input and output 8. Distinguish between array and pointer notation and use both in syntax
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
PLEASE WRITE A BASH SCRIPT FOR USE IN JUYPTER NOTEBOOK. Initialize a variable INPUT_FILE that stores...
PLEASE WRITE A BASH SCRIPT FOR USE IN JUYPTER NOTEBOOK. Initialize a variable INPUT_FILE that stores the name of the file 'testdata.csv'. Write an if-else statement in a shell that checks if the input file exists. If the file exists, then display a message "testdata.csv exists," otherwise display a message "testdata.csv does not exist." Initialize an array variable named 'ARR_WORDS' as below: ARR_WORDS=("naresh" "sam" " david" ) Initialize an array variable named 'ARR_COUNTS' of size equal to the length of...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...