Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is my code in C++, my code is working pretty good but there are some stuff

ID: 3587448 • Letter: T

Question

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

Explanation / Answer

#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;

//Structure
struct TokenFreq
{
//Structure variables
string token;
int freq;
TokenFreq()
{
freq = 1;
}

};//End of structure

//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";
string istr = "Writing in C or C++ is like running a chain saw with all the safety guards removed. In C++, reinvention is its own reward. ";
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;
}

Sample Run 1:


Testing matrix.

matrix[0][0]=0
matrix[0][1]=0
matrix[0][2]=0
matrix[0][3]=0
matrix[1][0]=0
matrix[1][1]=1
matrix[1][2]=2
matrix[1][3]=3
matrix[2][0]=0
matrix[2][1]=2
matrix[2][2]=4
matrix[2][3]=6


Testing Tokenizer

Size: 22

Token: writing, Freq: 1
Token: in, Freq: 2
Token: c, Freq: 1
Token: or, Freq: 1
Token: c++, Freq: 1
Token: is, Freq: 2
Token: like, Freq: 1
Token: running, Freq: 1
Token: a, Freq: 1
Token: chain, Freq: 1
Token: saw, Freq: 1
Token: with, Freq: 1
Token: all, Freq: 1
Token: the, Freq: 1
Token: safety, Freq: 1
Token: guards, Freq: 1
Token: removed., Freq: 1
Token: c++,, Freq: 1
Token: reinvention, Freq: 1
Token: its, Freq: 1
Token: own, Freq: 1
Token: reward., Freq: 1


Testing selection sort.


Token: writing, Freq: 1
Token: c, Freq: 1
Token: or, Freq: 1
Token: c++, Freq: 1
Token: like, Freq: 1
Token: running, Freq: 1
Token: a, Freq: 1
Token: chain, Freq: 1
Token: saw, Freq: 1
Token: with, Freq: 1
Token: all, Freq: 1
Token: the, Freq: 1
Token: safety, Freq: 1
Token: guards, Freq: 1
Token: removed., Freq: 1
Token: c++,, Freq: 1
Token: reinvention, Freq: 1
Token: its, Freq: 1
Token: own, Freq: 1
Token: reward., Freq: 1
Token: in, Freq: 2
Token: is, Freq: 2


Testing insertion sort.


Token: in, Freq: 2
Token: is, Freq: 2
Token: writing, Freq: 1
Token: c, Freq: 1
Token: or, Freq: 1
Token: c++, Freq: 1
Token: like, Freq: 1
Token: running, Freq: 1
Token: a, Freq: 1
Token: chain, Freq: 1
Token: saw, Freq: 1
Token: with, Freq: 1
Token: all, Freq: 1
Token: the, Freq: 1
Token: safety, Freq: 1
Token: guards, Freq: 1
Token: removed., Freq: 1
Token: c++,, Freq: 1
Token: reinvention, Freq: 1
Token: its, Freq: 1
Token: own, Freq: 1
Token: reward., Freq: 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote