This is my code in C++, my code is working pretty good but there are some stuff needs ti be changed I included theb below the my code:
include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;
//Structure
struct TokenFreq
{
//Structure variables
string token;
int freq=1;
};
//Method matrixInit()
void matrixInit(vector<vector<int> > &matrix, int numRows, int numCols)
{
//Declare the needed variables
int index1, index2;
//Resize
matrix.resize(numRows, vector<int>(numCols));
//Loop
for(index1 = 0; index1 < numRows; index1++)
{
//Loop
for(index2 = 0; index2 < numCols; index2++)
//Update
matrix[index1][index2] = index1 * index2;
}
}
//Method getTokenFreqVec()
void getTokenFreqVec(const string &istr, vector<TokenFreq> &tfVec)
{
//Declare the needed variables
string value;
int index, marker;
TokenFreq tf;
istringstream isStream(istr);
//Loop
while(getline(isStream, value, ' '))
{
//Function call
transform(value.begin(), value.end(), value.begin(), ::tolower);
//Initialize
marker = 1;
//Loop
for(index = 0; index < tfVec.size(); index++)
{
//Check condition
if(tfVec[index].token == value)
{
//Update
tfVec[index].freq += 1;
marker = 0;
}
}
//Check condition
if(marker)
{
//Update
tf.token = value;
//Function call
tfVec.push_back(tf);
}
}
}
//Method selectionSort()
void selectionSort(vector<TokenFreq> &tokFreqVector)
{
//Declare the needed variables
int index1, index2, minimum;
TokenFreq tf;
//Loop
for(index1 = 0; index1 < tokFreqVector.size() - 1; index1++)
{
//Initialize
minimum = index1;
for(index2 = index1 + 1; index2 < tokFreqVector.size(); index2++)
{
//Check condition
if(tokFreqVector[index2].freq < tokFreqVector[minimum].freq)
{
//Update
tf = tokFreqVector[minimum];
tokFreqVector[minimum] = tokFreqVector[index2];
tokFreqVector[index2] = tf;
}
}
}
}
//Method insertionSort()
void insertionSort(vector<TokenFreq> &tokFreqVector)
{
//Declare the needed variables
int index1, index2;
TokenFreq tf;
//Loop
for(index1 = 1; index1 < tokFreqVector.size(); index1++)
{
//Initialize
tf = tokFreqVector[index1];
//Loop
for(index2 = index1 - 1; index2 >= 0 && tokFreqVector[index2].freq < tf.freq; index2--)
//Update
tokFreqVector[index2 + 1] = tokFreqVector[index2];
tokFreqVector[index2 + 1] = tf;
}
}
//Driver
int main()
{
//Declare and initialize
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<vector<int> > matrix;
vector<TokenFreq> tfVec;
int index1, index2;
//Initialize matrix
matrixInit(matrix, 3, 4);
//Display
cout<<endl<<"Testing matrix."<<endl;
//Loop
for(index1 = 0; index1 < 3; index1++)
{
//Loop
for(index2 = 0; index2 < 4; index2++)
//Display
cout<<endl<<"matrix["<<index1<<"]["<<index2<<"]="<<matrix[index1][index2];
}
//Function call
getTokenFreqVec(istr, tfVec);
//Display
cout<<endl<<endl<<endl<<"Testing Tokenizer"<<endl<<endl;
cout<<"Size: "<<tfVec.size()<<endl;
//Loop
for(index1 = 0; index1 < tfVec.size(); index1++)
//Loop
cout<<endl<<"Token: "<<tfVec[index1].token<<", Freq: "<<tfVec[index1].freq;
//Function call
selectionSort(tfVec);
//Display
cout<<endl<<endl<<endl<<"Testing selection sort."<<endl<<endl;
//Loop
for(index1 = 0; index1 < tfVec.size(); index1++)
//Display
cout<<endl<<"Token: "<<tfVec[index1].token<<", Freq: "<<tfVec[index1].freq;
//Function call
insertionSort(tfVec);
//Display
cout<<endl<<endl<<endl<<"Testing insertion sort."<<endl<<endl;
//Loop
for(index1 = 0; index1 < tfVec.size(); index1++)
//Display
cout<<endl<<"Token: "<<tfVec[index1].token<<", Freq: "<<tfVec[index1].freq;
//Pause
cin.get();cin.get();
//Return
return 0;
}
Here are the Hints, I don't know what to change in my code:
3: Unit testkeyboard_arrow_up
0 / 5
Testing the getTokenFreqVec( const string& istr, vector<TokenFreq> & tfVec) function: can it handle a string of zero tokens?
Test feedback
The input string contains only white spaces. The correct number of tokens identified should be: 0; but your function identifies:1
4: Unit testkeyboard_arrow_up
0 / 7
Testing the getTokenFreqVec( const string& istr, vector<TokenFreq> & tfVec) function: can it handle a string of some nice and funny quotes on C++?
Test feedback
The input string is:Writing in C or C++ is like running a chain saw with all the safety guards removed. In C++, reinvention is its own reward. The correct number of tokens identified should be: 22; but your function identifies:23 For your information, here's the content of your vector: tfVec[0].token=writing tfVec[0].freq=1 tfVec[1].token= tfVec[1].freq=10 tfVec[2].token=in tfVec[2].freq=2 tfVec[3].token=c tfVec[3].freq=1 tfVec[4].token=or tfVec[4].freq=1 tfVec[5].token=c++ tfVec[5].freq=1 tfVec[6].token=is tfVec[6].freq=2 tfVec[7].token=like tfVec[7].freq=1 tfVec[8].token=running tfVec[8].freq=1 tfVec[9].token=a tfVec[9].freq=1 tfVec[10].token=chain tfVec[10].freq=1 tfVec[11].token=saw tfVec[11].freq=1 tfVec[12].token=with tfVec[12].freq=1 tfVec[13].token=all tfVec[13].freq=1 tfVec[14].token=the tfVec[14].freq=1 tfVec[15].token=safety tfVec[15].freq=1 tfVec[16].token=guards tfVec[16].freq=1 tfVec[17].token=removed. tfVec[17].freq=1 tfVec[18].token=c++, tfVec[18].freq=1 tfVec[19].token=reinvention tfVec[19].freq=1 tfVec[20].token=its tfVec[20].freq=1 tfVec[21].token=own tfVec[21].freq=1 tfVec[22].token=reward. tfVec[22].freq=1
5: Unit testkeyboard_arrow_up
0 / 9
Testing the getTokenFreqVec( const string& istr, vector<TokenFreq> & tfVec) function: can it handle the example input in the problem description?
Test feedback
The input string is: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 The correct number of tokens identified should be: 46; but your function identifies:47
6: Unit testkeyboard_arrow_up
0 / 9
Testing the getTokenFreqVec( const string& istr, vector<TokenFreq> & tfVec) function: can it handle a much longer string?
Test feedback
The input string is:No one wants to die. Even people who want to go to heaven don’t want to die to get there. And yet death is the destination we all share. No one has ever escaped it. And that is as it should be, because Death is very likely the single best invention of Life. It is Life’s change agent. It clears out the old to make way for the new. Right now the new is you, but someday not too long from now, you will gradually become the old and be cleared away. Sorry to be so dramatic, but it is quite true. Your time is limited, so don’t waste it living someone else’s life. Don’t be trapped by dogma — which is living with the results of other people’s thinking. Don’t let the noise of others’ opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary. When I was young, there was an amazing publication called The Whole Earth Catalog, which was one of the bibles of my generation. It was created by a fellow named Stewart Brand not far from here in Menlo Park, and he brought it to life with his poetic touch. This was in the late 1960s, before personal computers and desktop publishing, so it was all made with typewriters, scissors and Polaroid cameras. It was sort of like Google in paperback form, 35 years before Google came along: It was idealistic, and overflowing with neat tools and great notions.Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its course, they put out a final issue. It was the mid-1970s, and I was your age. On the back cover of their final issue was a photograph of an early morning country road, the kind you might find yourself hitchhiking on if you were so adventurous. Beneath it were the words: “Stay Hungry. Stay Foolish.” It was their farewell message as they signed off. Stay Hungry. Stay Foolish. And I have always wished that for myself. And now, as you graduate to begin anew, I wish that for you.Stay Hungry. Stay Foolish. The correct number of tokens identified should be: 226; but your function identifies:227
7: Unit testkeyboard_arrow_up
0 / 5
Testing 1. void selectionSort( vector<TokenFreq> & tfVec ) in ascending order of freq.
Test feedback
The input string is:Writing in C or C++ is like running a chain saw with all the safety guards removed. In C++, reinvention is its own reward. The correct number of tokens identified should be: 22; but your function identifies:23
8: Unit testkeyboard_arrow_up
0 / 5
Testing 2. void selectionSort( vector<TokenFreq> & tfVec ) in ascending order of freq.
Test feedback
The input string is:No one wants to die. Even people who want to go to heaven don’t want to die to get there. And yet death is the destination we all share. No one has ever escaped it. And that is as it should be, because Death is very likely the single best invention of Life. It is Life’s change agent. It clears out the old to make way for the new. Right now the new is you, but someday not too long from now, you will gradually become the old and be cleared away. Sorry to be so dramatic, but it is quite true. Your time is limited, so don’t waste it living someone else’s life. Don’t be trapped by dogma — which is living with the results of other people’s thinking. Don’t let the noise of others’ opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary. When I was young, there was an amazing publication called The Whole Earth Catalog, which was one of the bibles of my generation. It was created by a fellow named Stewart Brand not far from here in Menlo Park, and he brought it to life with his poetic touch. This was in the late 1960s, before personal computers and desktop publishing, so it was all made with typewriters, scissors and Polaroid cameras. It was sort of like Google in paperback form, 35 years before Google came along: It was idealistic, and overflowing with neat tools and great notions.Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its course, they put out a final issue. It was the mid-1970s, and I was your age. On the back cover of their final issue was a photograph of an early morning country road, the kind you might find yourself hitchhiking on if you were so adventurous. Beneath it were the words: “Stay Hungry. Stay Foolish.” It was their farewell message as they signed off. Stay Hungry. Stay Foolish. And I have always wished that for myself. And now, as you graduate to begin anew, I wish that for you.Stay Hungry. Stay Foolish. The correct number of tokens identified should be: 226; but your function identifies:227
9: Unit testkeyboard_arrow_up
0 / 5
Test 1. void insertionSort( vector<TokenFreq> & tfVec ) in descending order of freq.
Test feedback
The input string is:Writing in C or C++ is like running a chain saw with all the safety guards removed. In C++, reinvention is its own reward. The correct number of tokens identified should be: 22; but your function identifies:23
10: Unit testkeyboard_arrow_up
0 / 5
Test 2. void insertionSort( vector<TokenFreq> & tfVec ) in descending order of freq.
Test feedback
The input string is:No one wants to die. Even people who want to go to heaven don’t want to die to get there. And yet death is the destination we all share. No one has ever escaped it. And that is as it should be, because Death is very likely the single best invention of Life. It is Life’s change agent. It clears out the old to make way for the new. Right now the new is you, but someday not too long from now, you will gradually become the old and be cleared away. Sorry to be so dramatic, but it is quite true. Your time is limited, so don’t waste it living someone else’s life. Don’t be trapped by dogma — which is living with the results of other people’s thinking. Don’t let the noise of others’ opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary. When I was young, there was an amazing publication called The Whole Earth Catalog, which was one of the bibles of my generation. It was created by a fellow named Stewart Brand not far from here in Menlo Park, and he brought it to life with his poetic touch. This was in the late 1960s, before personal computers and desktop publishing, so it was all made with typewriters, scissors and Polaroid cameras. It was sort of like Google in paperback form, 35 years before Google came along: It was idealistic, and overflowing with neat tools and great notions.Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its course, they put out a final issue. It was the mid-1970s, and I was your age. On the back cover of their final issue was a photograph of an early morning country road, the kind you might find yourself hitchhiking on if you were so adventurous. Beneath it were the words: “Stay Hungry. Stay Foolish.” It was their farewell message as they signed off. Stay Hungry. Stay Foolish. And I have always wished that for myself. And now, as you graduate to begin anew, I wish that for you.Stay Hungry. Stay Foolish. The correct number of tokens identified should be: 226; but your function identifies:227
3: Unit testkeyboard_arrow_up
Here, we are trying to test program for an empty string and ideally there should not be any token if the string is empty or only spaces
However, current program is returning 1.
Updated code:
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;
//Structure
struct TokenFreq
{
//Structure variables
string token;
int freq=1;
};
//Method matrixInit()
void matrixInit(vector<vector<int> > &matrix, int
numRows, int numCols)
{
//Declare the needed variables
int index1, index2;
//Resize
matrix.resize(numRows, vector<int>(numCols));
//Loop
for(index1 = 0; index1 < numRows; index1++)
{
//Loop
for(index2 = 0; index2 < numCols; index2++)
//Update
matrix[index1][index2] = index1 * index2;
}
}
//Method getTokenFreqVec()
void getTokenFreqVec(const string &istr,
vector<TokenFreq> &tfVec)
{
//Declare the needed variables
string value;
int index, marker;
TokenFreq tf;
istringstream isStream(istr);
//Loop
while(getline(isStream, value, ' '))
{
if(value.find_first_not_of(' ') != std::string::npos){ //
[Correction]: Handle empty string
//Function call
transform(value.begin(), value.end(), value.begin(),
::tolower);
//Initialize
marker = 1;
//Loop
for(index = 0; index < tfVec.size(); index++)
{
//Check condition
if(tfVec[index].token == value)
{
//Update
tfVec[index].freq += 1;
marker = 0;
}
}
//Check condition
if(marker)
{
//Update
tf.token = value;
//Function call
tfVec.push_back(tf);
}
}
}
}
//Method selectionSort()
void selectionSort(vector<TokenFreq>
&tokFreqVector)
{
//Declare the needed variables
int index1, index2, minimum;
TokenFreq tf;
//Loop
for(index1 = 0; index1 < tokFreqVector.size() - 1;
index1++)
{
//Initialize
minimum = index1;
for(index2 = index1 + 1; index2 < tokFreqVector.size();
index2++)
{
//Check condition
if(tokFreqVector[index2].freq <
tokFreqVector[minimum].freq)
{
//Update
tf = tokFreqVector[minimum];
tokFreqVector[minimum] = tokFreqVector[index2];
tokFreqVector[index2] = tf;
}
}
}
}
//Method insertionSort()
void insertionSort(vector<TokenFreq>
&tokFreqVector)
{
//Declare the needed variables
int index1, index2;
TokenFreq tf;
//Loop
for(index1 = 1; index1 < tokFreqVector.size(); index1++)
{
//Initialize
tf = tokFreqVector[index1];
//Loop
for(index2 = index1 - 1; index2 >= 0 &&
tokFreqVector[index2].freq < tf.freq; index2--)
//Update
tokFreqVector[index2 + 1] = tokFreqVector[index2];
tokFreqVector[index2 + 1] = tf;
}
}
//Driver
int main()
{
//Declare and initialize
//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";
string istr = " ";
vector<vector<int> > matrix;
vector<TokenFreq> tfVec;
int index1, index2;
//Initialize matrix
matrixInit(matrix, 3, 4);
//Display
cout<<endl<<"Testing matrix."<<endl;
//Loop
for(index1 = 0; index1 < 3; index1++)
{
//Loop
for(index2 = 0; index2 < 4; index2++)
//Display
cout<<endl<<"matrix["<<index1<<"]["<<index2<<"]="<<matrix[index1][index2];
}
//Function call
getTokenFreqVec(istr, tfVec);
//Display
cout<<endl<<endl<<endl<<"Testing
Tokenizer"<<endl<<endl;
cout<<"Size: "<<tfVec.size()<<endl;
//Loop
for(index1 = 0; index1 < tfVec.size(); index1++)
//Loop
cout<<endl<<"Token:
"<<tfVec[index1].token<<", Freq:
"<<tfVec[index1].freq;
if(tfVec.size() != 0){ // [Correction]:Handle empty vector
//Function call
selectionSort(tfVec);
//Display
cout<<endl<<endl<<endl<<"Testing selection
sort."<<endl<<endl;
//Loop
for(index1 = 0; index1 < tfVec.size(); index1++)
//Display
cout<<endl<<"Token:
"<<tfVec[index1].token<<", Freq:
"<<tfVec[index1].freq;
//Function call
insertionSort(tfVec);
//Display
cout<<endl<<endl<<endl<<"Testing insertion
sort."<<endl<<endl;
//Loop
for(index1 = 0; index1 < tfVec.size(); index1++)
//Display
cout<<endl<<"Token:
"<<tfVec[index1].token<<", Freq:
"<<tfVec[index1].freq;
}
//Return
return 0;
}
Note: Refer to comments with [Correction]
prefix
4: Unit testkeyboard_arrow_up
After above fixed, identified token count is 22
5: Unit testkeyboard_arrow_up
After above fixed, identified token count is 46
6: Unit testkeyboard_arrow_up
After above fixed, identified token count is 226
Note: rest all the points will also start showing correct results with fixed code
This is my code in C++, my code is working pretty good but there are some...
I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() { char option; vector<movie> movies; while (true) { printMenu(); cin >> option; cin.ignore(); switch (option) { case 'A': { string nm; int year; string genre; cout << "Movie Name: "; getline(cin, nm); cout << "Year: "; cin >> year; cout << "Genre: "; cin >> genre; //call you addMovie() here addMovie(nm, year, genre, &movies); cout << "Added " << nm << " to the catalog" << endl; break; } case 'R': { string mn; cout << "Movie Name:"; getline(cin, mn); bool found; //call you removeMovie() here found = removeMovie(mn, &movies); if (found == false) cout << "Cannot find " << mn << endl; else cout << "Removed " << mn << " from catalog" << endl; break; } case 'O': { string mn; cout << "Movie Name: "; getline(cin, mn); cout << endl; //call you movieInfo function here movieInfo(mn, movies); break; } case 'C': { cout << "There are " << movies.size() << " movies in the catalog" << endl; // Call the printCatalog function here printCatalog(movies); break; } case 'F': { string inputFile; bool isOpen; cin >> inputFile; cout << "Reading catalog info from " << inputFile << endl; //call you readFromFile() in here isOpen = readFile(inputFile, &movies); if (isOpen == false) cout << "File not found" << endl;...
Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...
Hello, please correct the following C++ CODE it is not working on visual studio: (I WILL RATE, NO INCOMPLETE OR WRONG SOLUTIONS PLEASE) // CPP program to evaluate a given // expression where tokens are // separated by space. #include using namespace std; // Function to find precedence of // operators. int precedence(char op){ if(op == '+'||op == '-') return 1; if(op == '*'||op == '/') return 2; return 0; } // Function to perform arithmetic operations. int applyOp(int a,...
so i have my c++ code and ive been working on this for hours but i cant get it to run im not allowed to use arrays. im not sure how to fix it thank you for the help our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...
Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...
Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...
C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...
In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){ ifile.open(filename.c_str(), ios::in); if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); } } void...