(NOTE: THE PROGRAM SHOULD BE WRITTEN IN C++) Important, please read! Make sure y
ID: 3587442 • Letter: #
Question
(NOTE: THE PROGRAM SHOULD BE WRITTEN IN C++)
Important, please read!
Make sure you use the specified struct name and function prototypes, as they will be referred to as such in the unit tests.
Please feel free to introduce additional subroutines (i.e., functions) to help you implement the required functions, although they will not be tested.
A change was made to the first argument of the following function:
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 for grading. You are required to develop your program on a local IDE first. Then submit it for grading. You can submit your solution for grading up to 25 times.
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>
using namespace std;
int main()
{
int i,j,n,temp,a[40];
cout<<"Enter the elements:";
cin>>n;
cout<<" Enter the elements ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp; //insert element in proper place
}
}
cout<<" Sorted list is as follows ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;// end of the program
}
}
cout<<" Sorted list is as follows ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;// end of the program
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.