Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Token errors in C++: 2-dimensional arrays, a string tokenizer, insertion sort, and selection sort Hello, I have a code written for this assignment. It runs

Token errors in C++: 2-dimensional arrays, a string tokenizer, insertion sort, and selection sort

Hello, I have a code written for this assignment. It runs fine on my IDE but I recieve token errors when I submit it on my textbook software. I'm really not sure what to do. Any help would be appreciated. Thank you.

Code:

#include #include #include #include #include using namespace std; struct TokenFreq { string token; int freq = 1; }; void matrixInit (vectorint> > &matrix, int numRows, int numCols) { int i, 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 &tfVec) { string token; int i, flag; TokenFreq t; istringstream isStream(istr); while(getline(isStream, token, ' ')) { transform(token.begin(), token.end(), token.begin(), ::tolower); flag = 1; for(unsigned 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 &tokFreqVector){ int i, j, min; TokenFreq token; for (unsigned i = 0; i < tokFreqVector.size() - 1; i++) { min = i; for(unsigned j = i + 1; j < tokFreqVector.size(); j++) { if(tokFreqVector[j].freq < tokFreqVector[min].freq) { token = tokFreqVector[min]; tokFreqVector[min] = tokFreqVector[j]; tokFreqVector[j] = token; } } } } void insertionSort (vector &tokFreqVector) { int i, j; TokenFreq token; for (unsigned i = 1; i < tokFreqVector.size(); i++) { token = tokFreqVector[i]; for(unsigned 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 tfVec; vectorint> > matrix; int i, j; matrixInit(matrix, 3, 4); cout << " size of matrix is: 3x4 "; for (unsigned i = 0; i < 3; i++) { for(unsigned j = 0; j < 4; j++) cout<<" matrix["unsigned i = 0; i < tfVec.size(); i++) cout << " (token = \"" << tfVec[i].token << "\", freq = " << tfVec[i].freq << ")"; selectionSort(tfVec); for(unsigned i = 0; i < tfVec.size(); i++) cout << " (token = \"" << tfVec[i].token << "\", freq = " << tfVec[i].freq << ")"; insertionSort(tfVec); for(unsigned i = 0; i < tfVec.size(); i++) cout << " (token = \"" << tfVec[i].token << "\", freq = " << tfVec[i].freq << ")"; return 0; } 

Errors:

Testing the getTokenFreqVec( const string& istr, vector & tfVec) function: can it handle a string of zero tokens?

The correct number of tokens identified should be: 0; but your function identifies:1

Testing the getTokenFreqVec( const string& istr, vector & tfVec) function: can it handle a string of some nice and funny quotes on C++?

The correct number of tokens identified should be: 22; but your function identifies:23

Testing the getTokenFreqVec( const string& istr, vector & tfVec) function: can it handle the example input in the problem description?

The correct number of tokens identified should be: 46; but your function identifies:47

Testing the getTokenFreqVec( const string& istr, vector & tfVec) function: can it handle a much longer string?

The correct number of tokens identified should be: 226; but your function identifies:227

Testing 1. void selectionSort( vector & tfVec ) in ascending order of freq.

The correct number of tokens identified should be: 22; but your function identifies:23

Testing 2. void selectionSort( vector & tfVec ) in ascending order of freq.

The correct number of tokens identified should be: 226; but your function identifies:227

Edit:

The textbook software is zyBooks.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

ISBN: 0764549634, 9780764549632

More Books

Students also viewed these Databases questions