Question

I need to make a few changes to this C++ program,first of all it should read...

I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good.

#include <iostream>
#include <map>
#include <string>
#include <cctype>
#include <fstream>
#include <iomanip>

using namespace std;
void addWord(map<std::string,int> &words,string s);
void readFile(string infile,map<std::string,int> &words);
void display(map<std::string,int> words);
  
int main() {
// Declaring variables
std::map<std::string,int> words;
  
  
//defines an input stream for the data file
ifstream dataIn;

  
string infile;
cout<<"Please enter a File Name :";
cin>>infile;
readFile(infile,words);
  
display(words);

  
return 0;
}
void readFile(string infile,map<std::string,int> &words)
{
string s;
//defines an input stream for the data file
ifstream dataIn;
//Opening the input file
dataIn.open(infile.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"'"<<infile<<"' File Not Found **";
exit(0);
}
else
{
//counting how many words in the file
while(dataIn>>s)
{
addWord(words,s);
}
dataIn.close();
}
}
void addWord(map<std::string,int> &words,string s)
{
char c;
string str="";
for(unsigned int i=0;i<s.length();i++)
{
c=s[i];
if(isalpha(c))
str.push_back(std::toupper(c));
}
++words[str];
}
void display(map<std::string,int> words)
{

cout<<"---------------------"<<endl;
cout<<"Word Counter Summary "<<endl;
cout<<"---------------------"<<endl;
cout<<"\nWord\t\tCount"<<endl;
cout<<"------\t\t-----"<<endl;
  
for(std::map<std::string,int>::iterator iter = words.begin(); iter!=words.end(); ++iter)
{
cout<<setw(16)<<left<<iter->first<<setw(5)<<right<<iter->second<<std::endl;
}
  
}

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

Here is the solution to above problem in C++. Please read the code comments for more information

Please give a thumbs up!!!!

Explanation

1) fixed infile name to MichaelJordan.dat

2) scan the map and check for maximum frequency in display method and save the word

3) for the second and third most frequent word do same as for first word but also check if second if not equal to first and third is not equal to both first and second while checking in the map

C++ Code

#include <iostream>
#include <map>
#include <string>
#include <cctype>
#include <fstream>
#include <iomanip>

using namespace std;
void addWord(map<std::string,int> &words,string s);
void readFile(string infile,map<std::string,int> &words);
void display(map<std::string,int> words);
  
int main() {
// Declaring variables
std::map<std::string,int> words;
  
  
//defines an input stream for the data file
ifstream dataIn;

//fixing the fileName
string infile="MichaelJordan.dat";
readFile(infile,words);
  
display(words);

  
return 0;
}
void readFile(string infile,map<std::string,int> &words)
{
string s;
//defines an input stream for the data file
ifstream dataIn;
//Opening the input file
dataIn.open(infile.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"'"<<infile<<"' File Not Found **";
exit(0);
}
else
{
//counting how many words in the file
while(dataIn>>s)
{
addWord(words,s);
}
dataIn.close();
}
}
void addWord(map<std::string,int> &words,string s)
{
char c;
string str="";
for(unsigned int i=0;i<s.length();i++)
{
c=s[i];
if(isalpha(c))
str.push_back(std::toupper(c));
}
++words[str];
}
void display(map<std::string,int> words)
{

cout<<"---------------------"<<endl;
cout<<"Word Counter Summary "<<endl;
cout<<"---------------------"<<endl;
cout<<"\nWord\t\tCount"<<endl;
cout<<"------\t\t-----"<<endl;
  
for(std::map<std::string,int>::iterator iter = words.begin(); iter!=words.end(); ++iter)
{
cout<<setw(16)<<left<<iter->first<<setw(5)<<right<<iter->second<<std::endl;
}

//3 word which occur with most frequency
//first word
int freq=0;
string firstword="";
for(std::map<std::string,int>::iterator iter = words.begin(); iter!=words.end(); ++iter)
{
   if(iter->second>freq)
   {
       firstword=iter->first;
       freq=iter->second;
   }
}
//print the first word
cout<<"1st MOST FREQUENT WORD: "<<firstword<<endl;
//second word
string secondword="";
freq=0;
for(std::map<std::string,int>::iterator iter = words.begin(); iter!=words.end(); ++iter)
{
   //check if not equal to first word
   if(iter->second>freq &&(iter->first.compare(firstword)!=0))
   {
       secondword=iter->first;
       freq=iter->second;
   }
}
//print the first word
cout<<"2nd MOST FREQUENT WORD: "<<secondword<<endl;
//third word
string thirdword="";
freq=0;
for(std::map<std::string,int>::iterator iter = words.begin(); iter!=words.end(); ++iter)
{
   //check if not equal to first word and second word
   if(iter->second>freq &&(iter->first.compare(firstword)!=0) &&(iter->first.compare(secondword)!=0))
   {
       thirdword=iter->first;
       freq=iter->second;
   }
}
//print the first word
cout<<"3rd MOST FREQUENT WORD: "<<thirdword<<endl;

}

Add a comment
Know the answer?
Add Answer to:
I need to make a few changes to this C++ program,first of all it should read...
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
  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • The following program is in c++ ; I am trying to read in games from a...

    The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After...

    Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:  implement member functions  convert a member function into a standalone function  convert a standalone function into a member function  call member functions  implement constructors  use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...

  • The 4th deliverable is to create the program the makes the buy recommendation. It uses the...

    The 4th deliverable is to create the program the makes the buy recommendation. It uses the class Stock to store and retrieve the stock information. I need to make this program compatible with my stocks class. Program I need to change: #include <iostream> #include <cmath> #include <fstream> #include <iomanip> #include <string> #include <stdlib.h> using namespace std; // read the data file, store in arrays int openDatafile(ifstream&,string [], float[], float[], int[]); // opens the data file void readData(ifstream &, string[], float[],...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

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