Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function template that will process the elements of a vector and determine the frequency of each element. Each distinct element and its frequency

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Write a function template that will process the elements of a vector and determine the frequency of each element. Each distinct element and its frequency should be stored in an object of the class as specified in Figure 1. Maintain a vector of such objects. For each element in the vector, first determine if an entry for that element exists in the vector. Use the STL algorithm find to facilitate this determination. If an entry has previously been added to the vector, increment its frequency. If the element hasn't previously been added, add a new entry to the back of the vector. A sample main function for testing your functions is shown in Figure 2. Commands to compile, link, and run this assignment are shown in Figure 3. To use the Makefile as distributed in class, add a target of lab28main to targetsisrcfile. #ifndef LAB28_H #define LAB28_H 4 template class Frequency 6 7 public: 8 Frequency (T val) : value(val), frequency (1) 10 11 void increment ( ) 12 { ++frequency; } 13 14 T getValue () const { return value; } 16 17 uint getFrequency () const 18 { return frequency; } 19 20 bool operator==(const T rhs) const; 21 bool operator rhs) const; 22 private: T value; 24 uint frequency; 26 26 27 #endif#include 2 #include #include #include #include #include using namespace atd; 10 atring ltrim(string s) ; 11 string rtrim(string s) ; 12 1.3 template 14 vector> distribution(conat vector& v) ; 15 16 template 17 ostreams operator ># v); #include "lab28.cpp" 30 21 int main(int argo, char **argv) 22 { conat int aCount = 5, bCount = 7, cCount = 7, dCount = 12; 24 int a [aCount] = {5, 5, 5, 5, 5); double b[bCount] = (7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1); char c[Count] = {'r', 'a', 'c', 'e', 'c', 'a', 'r'}; 27 atring d [dCount] = {"Cadillac", "GMC", "GMC", "Lexus", "Lexus", "Dodge", "GMC", "BMW", 20 "BMW" , "GMC", "Dodge", "Lexus"}; 30 ifstream fin; 12 if (argo != 2) { cerr aVector(a, a + sizeof(a) / sizeof(a[0])); 46 cout bVector(b, b + sizeof(b) / sizeof (b[0])); cout cVector (c, c + sizeof(c) / sizeof(c[0])); 52 cout dVector(d, d + sizeof(d) / sizeof(d[0])); vector > dVectorDist; 56 dVectorDist = distribution (dVector) ; cout charVec; 62 vector > charVecDist; char ch; while (fin.get (ch) ) // read the data char-by-char 66 charVec . push_back (ch) ; charVecDist = distribution(charVec); cout wordVec; 77 vector> wordVecDist; atring word; while (fin > > word) // read the data word-by-word word = 1trim(word) ; word = rtrim(word) ; S4 if (word. length () > 0) wordVec. push_back (word) ; wordVecDist = distribution(wordVec) ; cout cd 2336 newuser@caunix * /2336> . /getlab . ksh 28 * Checking to see if a folder exists for Lab 28. . .No * Creating a folder for Lab 28 * Checking to see if Lab 28 has sample input and output files. . .Yes * Copying input and output files for Lab 28 from folder /usr/local/2336/data/28 to folder ./28 Checking to see if /usr/local/2336/arc/lab28main.C exists. . .Yes * Copying file /usr/local/2336/arc/lab28main.C to folder ./28 10 * Checking to see if /usr/local/2336/include/lab28.h exists. . .Yes 11 * Copying file /usr/local/2336/include/lab28.h to folder ./28 12 * Copying file /usr/local/2336/arc/Makefile to folder ./28 * Adding a target of lab28main to targetalarcfile 14 * Touching file ./28/lab28. cpp 15 * Edit file . /28/lab28. opp in Notepad++ newuser@caunix */2336> ed 28 17 newuser caunix */2336/28> 18 01 . dat 01 . out Makefile lab28 . CPP lab28 . h lab28main .C newuserocaunix */2336/28> make lab28main 20 g++ -g -Wall -atd=c++11 -c lab28main.C -I/usr/local/2336/include -I. 21 g++ -0 lab28main lab28main.o -L/usr/local/2336/lib -lm -1bits112 1.55 like -> 1 100 C-1 8 200 158 9 201 After Sorting by Frequency 9 202 that -> 1 38 begin -> 1 161 and with -> 1 Before Sorting by Frequency 205 vowel -> 1 1992 -> 1 206 WHAT -> 1 1993 -> 1 207 ABOUT -> 1 165 1994 -> 1 208 STARTING -> 1 106 This -> 1 209 WITH -> 1 167 is -> 1 210 8 -3 1 a -> 2 211 OR -3 1 NNNNNAP test -> 1 212 ENDING -> 1 170 of -> 1 213 IN -> 1 171 your -> 1 214 8 -3 1 172 word -> 1 215 Mary -> 1 -> 2 173 analysis -> 1 216 said -> 1 -> 2 174 program -> 1 217 I -31 175 How -> 1 218 like -> 1 many -> 1 210 C -31 177 220 HEE words -> 1 1992 -> 1 did -> 1 221 1993 -> 1 179 you -> 1 222 1994 -> 1 -> 2 find -> 1 This -> 1 -> 3 that -> 1 324 is -> 1 130 begin -> 1 225 test -> 1 140 ->3 with -> 1 126 of -> 1 vowel -> 1 327 your -> 1 142 4 WHAT -> 1 word -> 1 A ABOUT -> 1 220 analysis -> 1 187 STARTING -> 1 program -> 1 How -> 1 v 5 WITH -> 1 -> 5 AN -> 2 252 many -> 1 F H -> 5 100 5 -3 1 words -> 1 148 ->5 10 OR -> 1 234 did -> 1 y 140 -> 5 1:01 ENDING -> 1 you -> 1 -> 5 1:01 IN -> 1 256 find -> 1 150 151 - > 104 8 -$1 237 AN -> 2 152 ->6 105 Mary -> 1 2as a -> 2 153 6 106 said -> 1 154 V I->1 240 newuser@caunix "/2336/28> ./lab28main 01 . dat > my . out IFE newuser@caunix */2336/28> diff 01. out my . out 242 newuser@caunix * /2336/28>

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_2

Step: 3

blur-text-image_3

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions