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

1. Implement the following function to create a matrix of dimensionality numRows

ID: 3585751 • Letter: 1

Question

1. Implement the following function to create a matrix of dimensionality numRows x numCols, where matrix starts with an initial size of 0. Furthermore, initialize the value at matrix[i][j] to the product of i and j.

For example, if numRows is 3, numCols is 4, you are going to allocate space to matrix so that it contains three row vectors, where the size of each row vector will be 4. At the end of this function call, the content of matrix will be:

2. Given a string variable string s; , implement a tokenizer to identify the unique tokens contained in this string, identify all the unique tokens and their frequencies, and store such information into a vector. Tokens are sequences of contiguous characters separated by any of the specified delimiters (e.g., white spaces). In this lab, only white spaces will be considered as delimiters. For instance, the string "Hello, what's that thing? " contains four tokens: "Hello", "what's", "that" and "thing?". The frequency of a token is the number of times this token appears in this string. In this example, each token has a frequency of 1. Note that in this lab, these tokens are case insensitive. For example, "Hello" and "hello" are considered to be the same token.

Specifically, you are required to

declare a struct TokenFreq that consists of two data members: (1) string token; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object word stores the token "dream" and its frequency 100:

implement the following function, where istr is the input string, and tfVec will be used to store the list of unique and case insensitive tokens and their corresponding frequencies identified within istr. You might find it's very convenient to use stringstream objects to tokenize a string.

Assume that the value of istr is

After calling the above function, tfVec is expected to contain the following values (where order of appearances doesn't matter):

Implement the selection sort algorithm to sort a vector in ascending order of token frequency. The pseudo code of the selection algorithm can be found at this webpage. You can also watch an animation of the sorting process at http://visualgo.net/sorting -->under "select". This function has the following prototype:

Implement the insertion sort algorithm to sort a vector in descending order of token frequency. The pseudo code of the selection algorithm can be found at [http://www.algolist.net/Algorithms/Sorting/Insertionsort}(http://www.algolist.net/Algorithms/Sorting/Insertionsort). Use the same link above to watch an animation of this algorithm. This function has the following prototype:

Remember to include a main() function to test the above functions.

Explanation / Answer

#include <iostream>

#include <fstream>

#include <vector>

#include <sstream> //to use istringstream

#include <algorithm>

using namespace std;

/* below is the struct TokenFreq which

will store token and it's corresponding frequency count*/

struct TokenFreq {

string token;

int freq = 1;

};

/* below function will create a matrix of size numRows x numCols

and also initialize with i x j*/

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;

}

}

/* below main function will test the correctness of our above defined function*/

int main() {

cout << "Please enter the string you want to tokenise : ";

string istr;

//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";

getline(cin, istr);

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 << " [" << i << "]"<< " = (token = " << tfVec[i].token << ",Freq = " << tfVec[i].freq << ")" << endl;

selectionSort(tfVec);

cout << " Testing selection sort. ";

for (i = 0; i < tfVec.size(); i++)

cout << " [" << i << "]" << " = (token = " << tfVec[i].token << ",Freq = " << tfVec[i].freq << ")" << endl;

insertionSort(tfVec);

cout << " Testing insertion sort. ";

for (i = 0; i < tfVec.size(); i++)

cout << " [" << i << "]" << " = (token = " << tfVec[i].token << ",Freq = " << tfVec[i].freq << ")" << endl;

return 0;

}