http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
My version is spelt differently so adjust the code accordingly if copying and pasting.
Chapter 4 Exercise // 4.17
Write a program that finds the min, max and mode of a sequence of strings.
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
int main()
{
vector<string> words;
vector<string> smWords;
vector<string> lgWords;
vector<int> wordSizes;
cout << "Enter a set of whitespaced separated words (press enter then ctrl+z when finished): \n";
string w;
while (cin >> w)
{
words.push_back(w);
}
sort(words);
string word;
string mode;
string mostOccurring;
int highCount = 0;
int count = 0;
//print the words entered in order
for (int i = 0; i < words.size(); ++i)
{
cout << words[i] << " ";
}
//calculate mode
for (int i = 0; i < words.size(); ++i)
{
//get the ball rolling
if (i == 0)
{
word = words[i];
++count;
}
else
{
if (word == words[i])
{
++count;
mode = words[i];
}
else
{
if (highCount == 0)
{
highCount = count;
mostOccurring = mode;
}
else if (count > highCount)
{
highCount = count;
mostOccurring = mode;
}
word = words[i];
count = 1;
}
}
}
//calculate min & max
for (int x = 0; x < words.size(); ++x)
{
wordSizes.push_back(words[x].size());
}
sort(wordSizes);
int smallestWS = wordSizes[0];
int largestWS = wordSizes[wordSizes.size() - 1];
for (int x = 0; x < words.size(); ++x)
{
if (words[x].size() == smallestWS)
smWords.push_back(words[x]);
if (words[x].size() == largestWS)
lgWords.push_back(words[x]);
}
//print results
if (highCount == 1)
cout << "\nThere is no mode in this set of words.";
else
{
cout << "\nThe mode is: " << mostOccurring;
cout << "\nAppearing " << highCount << " time(s)";
}
cout << "\nThe largest word(s): ";
for (int x = 0; x < lgWords.size(); ++x)
if(x == 0 || lgWords[x] != lgWords[x-1])
cout << lgWords[x] << " ";
cout << "\nThe smallest words(s): ";
for (int x = 0; x < smWords.size(); ++x)
if (x == 0 || smWords[x] != smWords[x - 1])
cout << smWords[x] << " ";
cout << '\n';
keep_window_open();
return 0;
}
This one took me a while but only because towards the end I started to get a bit pedantic over what exactly should be printed. For example, what if someone entered words that were the same size? What would be the largest and smallest then? Also, what if there was no mode?
Originally testing for the min and max was in the same part as the mode but it just wouldn't work so I devised a new test for them. Basically in that section, it creates a new vector that contains the word sizes of <words>. These are then sorted (to get the smallest and largest) and assigned to variables. Another loop is created that checks to see if there are any words within <words> that match these sizes, if so they are pushed back into their respective new vectors. I did this because when you sort a vector of strings, it sorts it alphabetically, so searching for the largest and smallest by checking on the previous number won't work.
No comments:
Post a Comment