Question

some people think cars they own is extension of their personality somehow the idea that cars...

some people think cars they own is extension of their personality

somehow the idea that cars define people is very materialistic for some

for some people cars are just a way of transportation from point a to b

I like cars

some people like to collect old cars as a hobby

I guess one needs to be rich to have such a hobby

for some people cars are bad because gasoline pollutes environment

do you like cars

Read this document and create frequency map in C++. Take the frequency map and display histogram for each word in the document.

Draw histogram in horizontal direction.

For example-

This (6) **********

my (4) ******

Truck (6) *********

##other similar answers u find on chegg not working.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
using namespace std;

// Creates a map to store word and its frequency
map <string, int> wordFrequencyMap;

// Function to add a word to map
void addWord(string word)
{
// Creates an iterator for the map to search word
map<string, int> ::iterator iterate = wordFrequencyMap.find(word);

// Checks if iterator having the word
if(iterate != wordFrequencyMap.end())
// Increase the second by one
iterate->second = iterate->second + 1;
// Otherwise adds the word to map
else
wordFrequencyMap.insert(make_pair(word, 1));
}// End of function

// Function to read the file and calls the function to add the word to map
// Return true for success otherwise returns false
bool createMap()
{
// Opens the file for reading
ifstream readF("FrequencyWordUsingMap.txt");
// To sore the current line
string currentLine;

// Checks if file is opened
if(readF.is_open())
{
// Loops till end of the file
while(getline(readF, currentLine))
{

size_t previousPosition = 0, position;
// Loops till current line punctuation character and stores the position
while((position = currentLine.find_first_of("~`=!@#$%^&*)/\?-_|[,. }] (_-+{;':""></",
previousPosition)) != string::npos)
{
// Checks if current position is greater than earlier position
if(position > previousPosition)
// Calls the function to add the word subtracted from current list
addWord(currentLine.substr(previousPosition, position - previousPosition));
// Update the previous position by adding current position by one
previousPosition = position + 1;
}// End of inner while loop

// Otherwise checks if previous position is less then current line length
if(previousPosition < currentLine.length())
// Calls the function to add the word subtracted from current list
addWord(currentLine.substr(previousPosition, string::npos));
}// End of outer while loop

// Close the file
readF.close();
}// End of if condition

// Otherwise file cannot be opened display error message and return false
else
{
cout<<"\nERROR: Unable to open file for reading."<<endl;
return false;
}// End of else

// At the end return true for success
return true;
}// End of function

// Function to display each word, its frequency and bar chart
void showData()
{
// Loops till end of the map
for(map<string, int> :: iterator itrator = wordFrequencyMap.begin();
itrator != wordFrequencyMap.end(); ++itrator)
{
cout<<"\n "<< setw(30) <<itrator->first<<setw(3)<<"("<<itrator->second<<")";

// Loops till size of the second element in map to display '*'
for(int c = 0; c < itrator->second; c++)
cout<<"*";
}// End of for loop
}// End of function

int main()
{
// Checks if map is created
if(createMap())
{
// Displays map size and its elements
cout<<"\n The map has "<< wordFrequencyMap.size() << " elements";
// Calls the function to display each word, its frequency and bar chart
showData();
}// End of if condition
return 0;
}// End of main function

Sample Output:

FrequencyWordUsingMap.txt file contents

some people think cars they own is extension of their personality
somehow the idea that cars define people is very materialistic for some
for some people cars are just a way of transportation from point a to b
I like cars
some people like to collect old cars as a hobby
I guess one needs to be rich to have such a hobby
for some people cars are bad because gasoline pollutes environment
do you like cars

Add a comment
Know the answer?
Add Answer to:
some people think cars they own is extension of their personality somehow the idea that cars...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below...

    First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below Include each of the following in your answer (if applicable – explain in a paragraph) Research problem: what do you want to solve using Delphi? Sample: who will participate and why? (answer in 5 -10 sentences) Round one questionnaire: include 5 hypothetical questions you would like to ask Discuss: what are possible outcomes of the findings from your study? Hint: this is the conclusion....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT