This assignment is a c++ assignment
P4 Words COMP2711/8801 Computer Programming 2 Flinders University Practical 4: Words Task Your task is to write a program, words, that reports information about the number of words read from standard input. For example, if the file qbf.txt contains the text the quick brown fox jumps over the lazy dog then running the program with its input redirected to come from that file will report 9 words ./words < qbf.txt="" total:="" 9="" you="" may="" find="" it="" useful="" to="" create="" your="" own="" text="" file="" to="" use="" during="" development="" rather="" than="" repeatedly="" typing="" in="" a="" series="" of="" words.="" the="" quickest="" way="" to="" pass="" this="" to="" your="" program="" from="" within="" clion="" is="" to="" open="" the="" built-in="" terminal="" and="" type="" in="" the="" above="" command="" (you="" will="" likely="" need="" to="" replace="" ./words="" with="" words.exe="" on="" windows).="" this="" assumes="" the="" text="" file="" is="" located="" in="" your="" project="" directory,="" of="" course.="" svn="" check="" out="" before="" you="" begin,="" you="" will="" need="" to="" check="" out="" the="" practical="" directory="" from="" the="" topic="" repository.="" this="" can="" be="" achieved="" within="" clion="" by="" selecting="" get="" from="" version="" control="" on="" the="" welcome="" screen.="" in="" the="" window="" that="" appears,="" select="" subversion="" from="" the="" dropdown="" list.="" if="" you="" have="" not="" previously="" connected="" clion="" to="" the="" repository,="" click="" the="" add="" button="" (+)="" and="" paste="" the="" following="" url="" into="" the="" text="" field="" (replacing="" fan="" with="" your="" fan):="" https://topicsvn.flinders.edu.au/svn-repos/comp2711/fan="" expand="" the="" repository="" listing="" and="" select="" the="" words="" directory.="" click="" check="" out="" then="" select="" a="" location="" to="" save="" your="" project="" (preferably="" in="" a="" practicals="" directory="" you="" have="" created="" for="" your="" cp2="" pracs)="" and="" click="" open.="" from="" the="" destination="" list,="" choose="" the="" second="" option="" to="" create="" a="" project="" directory="" named="" words="" and="" then="" click="" ok="" to="" complete="" the="" check="" out.="" a="" dialogue="" may="" appear="" confirming="" the="" subversion="" working="" format;="" if="" so,="" 1.8="" is="" fine.="" finally,="" when="" prompted="" if="" you="" would="" like="" to="" open="" the="" project,="" click="" yes.="" automated="" marking="" this="" practical="" is="" available="" for="" automatic="" marking="" via="" the="" flo="" quiz="" practical="" 4,="" which="" is="" located="" in="" the="" assessment="" hub="" module.="" to="" assess="" your="" solution,="" first="" commit="" your="" changes="" to="" the="" topic="" repository.="" this="" can="" be="" completed="" within="" clion="" via="" the="" commit="" option="" under="" the="" vcs="" menu="" (or="" alternatively="" with="" ⌘+k="" on="" macos="" or="" ctrl+k="" on="" windows).="" you="" should="" adhere="" to="" good="" development="" habits="" and="" enter="" a="" commit="" message="" that="" describes="" what="" you="" have="" changed="" since="" the="" last="" commit.="" when="" ready,="" click="" commit="" to="" upload="" your="" changes="" and="" receive="" a="" revision="" number.="" enter="" this="" revision="" number="" into="" the="" answer="" box="" for="" the="" relevant="" question="" (level).="" there="" are="" no="" penalties="" for="" incorrect="" solutions,="" so="" if="" you="" do="" not="" pass="" all="" test="" cases,="" check="" the="" report="" output,="" modify="" your="" solution,="" commit,="" and="" try="" again.="" remember="" to="" finish="" and="" submit="" the="" quiz="" when="" you="" are="" ready="" to="" hand="" in.="" you="" may="" complete="" the="" quiz="" as="" many="" times="" as="" you="" like—your="" final="" mark="" for="" the="" practical="" will="" be="" the="" highest="" quiz="" mark="" achieved.="" background="" the="" following="" skeleton="" code="" for="" the="" program="" is="" provided="" in="" words.cpp,="" which="" will="" be="" located="" inside="" your="" working="" copy="" directory="" following="" the="" check="" out="" process="" described="" above.="" int="" main="" (int="" argc,="" char**="" argv)="" {="" enum="" {="" total,="" unique="" }="" mode="total;" flinders="" university="" college="" of="" science="" and="" engineering="" 1="" comp2711/8801="" computer="" programming="" 2="" flinders="" university="" for="" (int="" c;="" (c="getopt(argc," argv,="" "tu"))="" !="-1;)" {="" switch(c)="" {="" case="" 't':="" mode="total;" break;="" case="" 'u':="" mode="unique;" break;="" }="" }="" argc="" -="optind;" argv="" +="optind;" string="" word;="" int="" count="0;" while="" (cin="">> word) { count += 1; } switch (mode) { case total: cout < "total:="" "="">< count="">< endl;="" break;="" case="" unique:="" cout="">< "unique:="" "="">< "**="" missing="" **"="">< endl;="" break;="" }="" return="" 0;="" }="" the="" getopt="" function="" (#include="">
) provides a standard way of handling option values in command line arguments to programs. It analyses the command line parameters argc and argv looking for arguments that begin with '-'. It then examines all such arguments for specified option letters, returning individual letters on successive calls and adjusting the variable optind to indicate which arguments it has processed. Consult getopt documentation for details. In this case, the option processing code is used to optionally modify a variable that determines what output the program should produce. By default, mode is set to total indicating that it should display the total number of words read. The getopt code looks for the t and u options, which would be specified on the command line as -t or -u, and overwrites the mode variable accordingly. When there are no more options indicated by getopt returning -1, argc and argv are adjusted to remove the option arguments that getopt has processed. Level 1: Unique words Extend the program so that if run with the u option, specified by the command line argument -u, it displays the number of unique words. To count unique words, use the STL vector class to keep track of which words you have already seen. When each word is read, check to see if it is already in the vector; if it is not, insert it. The number of unique words will then be the size of the vector. Level 2: Your own vector Modify your code so that it does not use the STL vector class. You will need to write your own class to keep track of the list of words. At this level, you can assume that there will be no more that 1,000 unique words, so you can use a statically allocated array to store the items. A minimal STL-compliant class, which will minimise the changes you need to make to your main program, would have an interface like this: template class MyVector { public: typedef T* iterator; // creates an alias of the T* type called iterator Flinders University / College of Science and Engineering 2 COMP2711/8801 Computer Programming 2 Flinders University MyVector(); iterator begin(); iterator end(); int size(); iterator insert (iterator position, const T& item); private: T items[1000]; int used; }; For this practical, you may wish to implement your class as a template class, which is defined entirely in the header file without an accompanying .cpp file. In this approach, the complete method is written within the class scope as a single unit. Given the class is likely to be small, you could also opt to place the class at the top of your words.cpp file. If you do elect to create a separate class file, ensure you add it to the repository. CLion should prompt you to do this when creating the class file(s), so just click Add. Level 3: Individual word counts Extend the program so that if run with the i option it displays the counts of individual words in alphabetical order. For example, the command ./words -i < qbf.txt should result in the output shown. brown: 1 dog: 1 fox: 1 jumps: 1 lazy: 1 over: 1 quick: 1 the: 2 your program will need to store two pieces of information for each word. define a struct called wordinfo that contains a string, named text, for the word's text, and an int, named count, for the number of times it has been seen. then create a vector of wordinfo instead of a vector of string. when you add a new word, give it a count of 1. when you see a word that is already in the vector, increment its count. the simplest way to display the result in alphabetic order is to keep the vector in sorted order as words are added, then simply iterate through the vector at the end. the insertion sort algorithm is a suitable way to find where a new word should be inserted. iterate through the list until you find a word that is alphabetically after the new word, then insert at that position. level 4: large data sets make sure that your program works correctly (and efficiently!) even if it is run with large data sets. since you do not know how large the collection of words might become, you will need to make your vector grow dynamically. a suitable strategy is to allocate space for a small number of items initially and then check at each insert whether or not there is still enough space. when the space runs out, allocate a new block that is twice as large, copy all of the old values into the new space, and delete the old block. flinders university / college of science and engineering 3 qbf.txt="" should="" result="" in="" the="" output="" shown.="" brown:="" 1="" dog:="" 1="" fox:="" 1="" jumps:="" 1="" lazy:="" 1="" over:="" 1="" quick:="" 1="" the:="" 2="" your="" program="" will="" need="" to="" store="" two="" pieces="" of="" information="" for="" each="" word.="" define="" a="" struct="" called="" wordinfo="" that="" contains="" a="" string,="" named="" text,="" for="" the="" word's="" text,="" and="" an="" int,="" named="" count,="" for="" the="" number="" of="" times="" it="" has="" been="" seen.="" then="" create="" a="" vector="" of="" wordinfo="" instead="" of="" a="" vector="" of="" string.="" when="" you="" add="" a="" new="" word,="" give="" it="" a="" count="" of="" 1.="" when="" you="" see="" a="" word="" that="" is="" already="" in="" the="" vector,="" increment="" its="" count.="" the="" simplest="" way="" to="" display="" the="" result="" in="" alphabetic="" order="" is="" to="" keep="" the="" vector="" in="" sorted="" order="" as="" words="" are="" added,="" then="" simply="" iterate="" through="" the="" vector="" at="" the="" end.="" the="" insertion="" sort="" algorithm="" is="" a="" suitable="" way="" to="" find="" where="" a="" new="" word="" should="" be="" inserted.="" iterate="" through="" the="" list="" until="" you="" find="" a="" word="" that="" is="" alphabetically="" after="" the="" new="" word,="" then="" insert="" at="" that="" position.="" level="" 4:="" large="" data="" sets="" make="" sure="" that="" your="" program="" works="" correctly="" (and="" efficiently!)="" even="" if="" it="" is="" run="" with="" large="" data="" sets.="" since="" you="" do="" not="" know="" how="" large="" the="" collection="" of="" words="" might="" become,="" you="" will="" need="" to="" make="" your="" vector="" grow="" dynamically.="" a="" suitable="" strategy="" is="" to="" allocate="" space="" for="" a="" small="" number="" of="" items="" initially="" and="" then="" check="" at="" each="" insert="" whether="" or="" not="" there="" is="" still="" enough="" space.="" when="" the="" space="" runs="" out,="" allocate="" a="" new="" block="" that="" is="" twice="" as="" large,="" copy="" all="" of="" the="" old="" values="" into="" the="" new="" space,="" and="" delete="" the="" old="" block.="" flinders="" university="" college="" of="" science="" and="" engineering=""> qbf.txt should result in the output shown. brown: 1 dog: 1 fox: 1 jumps: 1 lazy: 1 over: 1 quick: 1 the: 2 your program will need to store two pieces of information for each word. define a struct called wordinfo that contains a string, named text, for the word's text, and an int, named count, for the number of times it has been seen. then create a vector of wordinfo instead of a vector of string. when you add a new word, give it a count of 1. when you see a word that is already in the vector, increment its count. the simplest way to display the result in alphabetic order is to keep the vector in sorted order as words are added, then simply iterate through the vector at the end. the insertion sort algorithm is a suitable way to find where a new word should be inserted. iterate through the list until you find a word that is alphabetically after the new word, then insert at that position. level 4: large data sets make sure that your program works correctly (and efficiently!) even if it is run with large data sets. since you do not know how large the collection of words might become, you will need to make your vector grow dynamically. a suitable strategy is to allocate space for a small number of items initially and then check at each insert whether or not there is still enough space. when the space runs out, allocate a new block that is twice as large, copy all of the old values into the new space, and delete the old block. flinders university / college of science and engineering 3>