Can someone help fix this code so any text that is entered it countthe frequency
ID: 3585957 • Letter: C
Question
Can someone help fix this code so any text that is entered it countthe frequency of each word. Thanks
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream> //to use istringstream
#include <algorithm>
using namespace std;
struct TokenFreq {
string token;
int freq = 1;
};
void matrixInit( vector< vector<int> > &matrix, int numRows, int numCols){
int i;
int j;
matrix.resize(numRows, vector<int>(numCols) );
for(i = 0; i< numRows; i++ ){
for(j = 0; j < numCols; j++ )
matrix[i][j] = i*j;
}
}
void getTokenFreqVec(const string &istr, vector<TokenFreq> &tfVec){
string token;
int i;
int flag;
TokenFreq t;
istringstream isStream(istr);
while(getline(isStream, token,' ')){
transform (token.begin(), token.end(), token.begin(), ::tolower);
flag =1;
for(i =0; i <tfVec.size();i++){
if(tfVec[i].token == token){
tfVec[i].freq += 1;
flag =0;
}
}
if(flag){
t.token = token;
tfVec.push_back(t);
}
}
}
void selectionSort( vector<TokenFreq> &tokFreqVector ){
int i;
int j;
int minT;
TokenFreq token;
for(i =0; i<tokFreqVector.size()-1 ;i++){
minT = i;
for (j =i + 1; j <tokFreqVector.size(); j++){
if(tokFreqVector[j].freq < tokFreqVector[minT].freq){
token = tokFreqVector[minT];
tokFreqVector[minT]= tokFreqVector[j];
tokFreqVector[j] = token;
}
}
}
}
void insertionSort( vector<TokenFreq> &tokFreqVector ){
int i;
int j;
TokenFreq token;
for(i = 0; i <tokFreqVector.size();i ++){
token = tokFreqVector[i];
for(j = i -1; j >= 0 && tokFreqVector[j].freq < token.freq; j++)
tokFreqVector[j+1] = tokFreqVector[j];
tokFreqVector[j+1] = token;
}
}
int main(){
string istr = "And no, I'm not a walking C++ dictionary. I do not keep every technical detail in my head at all times. If I did that, I would be a much poorer programmer. I do keep the main points straight in my head most of the time, and I do know where to find the details when I need them. by Bjarne Stroustrup" ;
vector <TokenFreq> tfVec;
vector <vector <int> > matrix;
int i;
int j;
matrixInit(matrix, 3,4);
cout <<" Size of matrix is: 3x4 ";
for(i = 0; i< 3; i++){
for(j = 0; j < 4; j++)
cout<<" matrix["<<i<<"]["<<j<<"]=" <<matrix[i][j];
}
getTokenFreqVec(istr, tfVec);
cout<<" Testing Tokenizer. Size = "<<tfVec.size()<< " {"<<endl;
for(i = 0; i < tfVec.size(); i++)
cout<<tfVec.size()<<" = (token = "<<tfVec[i].token<< ",Freq = "<<tfVec[i].freq << ")"<<endl;
selectionSort(tfVec);
cout<<" Testing selection sort. ";
for(i = 0; i < tfVec.size(); i++)
cout<<" = (Token = "<<tfVec[i].token<<", Freq = "<<tfVec[i].freq << ")"<<endl;
insertionSort(tfVec);
cout<<" Testing insertion sort. ";
for(i = 0; i < tfVec.size(); i++)
cout<<" Token: "<<tfVec[i].token<<", Freq: "<<tfVec[i].freq;
return 0;
}
Explanation / Answer
/*NOTE:
in insertionSort() code 2nd for loop u use j++ instead of decrementing it*/
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream> //to use istringstream
#include <algorithm>
using namespace std;
struct TokenFreq {
string token;
int freq = 1;
};
void matrixInit(vector< vector<int> > &matrix, int numRows, int numCols) {
int i;
int j;
matrix.resize(numRows, vector<int>(numCols));
for (i = 0; i< numRows; i++) {
for (j = 0; j < numCols; j++)
matrix[i][j] = i*j;
}
}
void getTokenFreqVec(const string &istr, vector<TokenFreq> &tfVec) {
string token;
int i;
int flag;
TokenFreq t;
istringstream isStream(istr);
while (getline(isStream, token, ' ')) {
transform(token.begin(), token.end(), token.begin(), ::tolower);
flag = 1;
for (i = 0; i <tfVec.size(); i++) {
if (tfVec[i].token == token) {
tfVec[i].freq += 1;
flag = 0;
}
}
if (flag) {
t.token = token;
tfVec.push_back(t);
}
}
}
void selectionSort(vector<TokenFreq> &tokFreqVector) {
int i;
int j;
int minT;
TokenFreq token;
for (i = 0; i<tokFreqVector.size() - 1; i++) {
minT = i;
for (j = i + 1; j <tokFreqVector.size(); j++) {
if (tokFreqVector[j].freq < tokFreqVector[minT].freq) {
token = tokFreqVector[minT];
tokFreqVector[minT] = tokFreqVector[j];
tokFreqVector[j] = token;
}
}
}
}
void insertionSort(vector<TokenFreq> &tokFreqVector) {
int i;
int j;
TokenFreq token;
for (i = 0; i <tokFreqVector.size(); i++) {
token = tokFreqVector[i];
for (j = i - 1; j >= 0 && tokFreqVector[j].freq < token.freq; j--)//u are running for loop from high to low so use --j instead of ++j
tokFreqVector[j + 1] = tokFreqVector[j];
tokFreqVector[j + 1] = token;
}
}
int main() {
string istr = "And no, I'm not a walking C++ dictionary. I do not keep every technical detail in my head at all times. If I did that, I would be a much poorer programmer. I do keep the main points straight in my head most of the time, and I do know where to find the details when I need them. by Bjarne Stroustrup";
vector <TokenFreq> tfVec;
vector <vector <int> > matrix;
int i;
int j;
matrixInit(matrix, 3, 4);
cout << " Size of matrix is: 3x4 ";
for (i = 0; i< 3; i++) {
for (j = 0; j < 4; j++)
cout << " matrix[" << i << "][" << j << "]=" << matrix[i][j];
}
getTokenFreqVec(istr, tfVec);
cout << " Testing Tokenizer. Size = " << tfVec.size() << " {" << endl;
for (i = 0; i < tfVec.size(); i++)
cout << tfVec.size() << " = (token = " << tfVec[i].token << ",Freq = " << tfVec[i].freq << ")" << endl;
selectionSort(tfVec);
cout << " Testing selection sort. ";
for (i = 0; i < tfVec.size(); i++)
cout << " = (Token = " << tfVec[i].token << ", Freq = " << tfVec[i].freq << ")" << endl;
insertionSort(tfVec);
cout << " Testing insertion sort. ";
for (i = 0; i < tfVec.size(); i++)
cout << " Token: " << tfVec[i].token << ", Freq: " << tfVec[i].freq;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.