Could you please solve the problem with dynamically allocated memory with c++ Wr
ID: 3839139 • Letter: C
Question
Could you please solve the problem with dynamically allocated memory with c++
Write a program that reads in a text file one word at a time. Store a word into a dynamically created array when it is first encountered. Create a parallel integer array to hold a count of the number of times that each particular word appears in the text file. If the word appears in the text file multiple times, do not add it into your dynamic array, but make sure to increment the corresponding word frequency counter in the parallel integer array. Remove any trailing punctuation from all words before doing any comparisons. Create and use the following text file containing a quote from Bill Cosby to test your program. I don't know the key to success, but the key to failure is trying to please everybody. At the end of your program, generate a report that prints the contents of your two arrays in a format similar to the following: Word Frequency AnalysisExplanation / Answer
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
char buffer[1025];
int wordCount[100] = {''};
std::ifstream file("Newfile.txt");
std::string line;
std::string partial;
std::vector<std::string> tokens;
int num = 0;
while(std::getline(file, line)) { // ' ' is the default delimiter
//std::cout << " " << line ;
std::istringstream iss(line);
std::string token;
while(std::getline(iss, token, ' ')){ // but we can specify a different one
std::vector<string>::iterator it;
it = find (tokens.begin(), tokens.end(), token);
if (it != tokens.end()){
int i = 0;
for(; i < tokens.size(); i++){
if(tokens[i] == token){
break;
}
}
wordCount[i] = wordCount[i] + 1;
}else{
tokens.push_back(token);
wordCount[num] = 1;
num = num + 1;
}
}
}
cout << "Word" << " " << "Frequency" << " ";
for(int i = 0; i < tokens.size(); i++)
{
cout << tokens[i] << " " << wordCount[i] << " ";
}
cout << " " << endl;
std::cout << ' ' ;
}
output
-------
i 1
don't 1
know 1
the 2
key 2
to 3
success, 1
but 1
failure 1
is 1
trying 1
please 1
everybody. 1
description :
1. As given in query, i have taken dynamica array ( vector) for storing words and
2. for count , int array has been taken.
Please let me know if you face any difficulties to make understanding on above code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.