 # The main objectives of this lab include: Set up a 2d array (or m...

## Question

The main objectives of this lab include:

Set up a 2d array (or matrix) with proper initial values using vector of vectors given a string, implement a tokenizer to identify all the unique tokens contained within this string and the number of times (i.e., frequency) a given token appears in this string.
Create a new datatype using struct to hold a token and its frequency; further store all the tokens and their frequencies into a vector implement the insertion sort algorithm to sort the list of tokens in increasing order of frequency implement the selection sort algorithm to sort the list of tokens in decreasing order of frequency.

Submit a single .cpp program. You are required to develop your program on a local IDE first.

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.

void matrixInit( vector< vector<int> >& matrix, int numRows, int numCols);
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:

size of matrix is: 3x4
matrix=0
matrix=0
matrix=0
matrix=0
matrix=0
matrix=1
matrix=2
matrix=3
matrix=0
matrix=2
matrix=4
matrix=6

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:
TokenFreq word;
word.value="dream";
word.freq=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.
void getTokenFreqVec( const string& istr, vector<TokenFreq> & tfVec)
Assume that the value of istr 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.

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

size=46 {
 = (token = "and", freq = 2)
 = (token = "no,", freq = 1)
 = (token = "i'm", freq = 1)
 = (token = "not", freq = 2)
 = (token = "a", freq = 2)
 = (token = "walking", freq = 1)
 = (token = "c++", freq = 1)
 = (token = "dictionary.", freq = 1)
 = (token = "i", freq = 6)
 = (token = "do", freq = 3)
 = (token = "keep", freq = 2)
 = (token = "every", freq = 1)
 = (token = "technical", freq = 1)
 = (token = "detail", freq = 1)
 = (token = "in", freq = 2)
 = (token = "my", freq = 2)
 = (token = "head", freq = 2)
 = (token = "at", freq = 1)
 = (token = "all", freq = 1)
 = (token = "times.", freq = 1)
 = (token = "if", freq = 1)
 = (token = "did", freq = 1)
 = (token = "that,", freq = 1)
 = (token = "would", freq = 1)
 = (token = "be", freq = 1)
 = (token = "much", freq = 1)
 = (token = "poorer", freq = 1)
 = (token = "programmer.", freq = 1)
 = (token = "the", freq = 3)
 = (token = "main", freq = 1)
 = (token = "points", freq = 1)
 = (token = "straight", freq = 1)
 = (token = "most", freq = 1)
 = (token = "of", freq = 1)
 = (token = "time,", freq = 1)
 = (token = "know", freq = 1)
 = (token = "where", freq = 1)
 = (token = "to", freq = 1)
 = (token = "find", freq = 1)
 = (token = "details", freq = 1)
 = (token = "when", freq = 1)
 = (token = "need", freq = 1)
 = (token = "them.", freq = 1)
 = (token = "by", freq = 1)
 = (token = "bjarne", freq = 1)
 = (token = "stroustrup", freq = 1)

Implement the selection sort algorithm to sort a vector in ascending order of token frequency. This function has the following prototype:
void selectionSort( vector<TokenFreq> & tokFreqVector ); //This function receives a vector of TokenFreq objects by reference and applies the selections sort algorithm to sort this vector in increasing order of token frequencies.

Implement the insertion sort algorithm to sort a vector in descending order of token frequency. This function has the following prototype:
void insertionSort( vector<TokenFreq> & tokFreqVector );
Remember to include a main() function to test the above functions.

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

int main(int argc, char** argv) {

// test matrixInit
cout << "matrixInit\n";
vector<vector<int> > matrix;
matrixInit(matrix, 3, 4);
cout << "size of matrix is: 3x4\n";
int i, j;
i = 0;
for (auto elem : matrix) {
j = 0;
for (auto e : elem) {
cout << "matrix[" << i << "][" << j << "] = " << e << "\n";
j++;
}
i++;
}

cout << "\n\n";

// test getTokenFreqVec
cout << "test getTokenFreqVec\n";
string t = "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;
getTokenFreqVec(t, tfVec);

cout << "size = " << tfVec.size() << "\n";
i = 0;
for (TokenFreq elem : tfVec) {
cout << "[" << i << "] = { token = \""
<< elem.token << "\", freq = "
<< elem.freq << ")\n";
i++;
}...

By purchasing this solution you'll be able to access the following files:
Solution.cpp.

\$45.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.