Pages

Wednesday, 9 May 2018

Chapter 8 // Exercise 13 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 8 // Exercise 13



13. Write a function that takes a vector<string> argument and returns a vector<int> containing the number of characters in each string. Also find the longest and the shortest string and the lexicographically first and last string. How many separate functions would you use for these tasks? Why?


#include "stdafx.h"
#include "std_lib_facilities.h"

void print(const vector<int>& vInt, const vector<string>& vStr1, const vector<string>& vStr2)
{
 cout << "Chars in each string: ";
 for (int i = 0; i < vInt.size(); ++i)
  cout << vInt[i] << " ";

 cout << "\n\nLongest String: " << vStr1[0] << endl;
 cout << "\nSmallest String: " << vStr1[1] << endl;

 cout << "\nAlphabetically First Word: " << vStr2[0] << endl;
 cout << "\nAlphabetically Last Word: " << vStr2[1] << '\n' << endl;
}

//number of characters in each string
vector<int> findNumChars(const vector<string>& v)
{
 vector<int> numChar;

 for (int i = 0; i < v.size(); ++i)
 {
  //push back size of each string
  numChar.push_back(v[i].size());
 }

 return numChar;
}

//find longest&shortest string in vector
vector<string> findMinMax(const vector<string>& v)
{
 vector<string> minMax;

 int largest = v[0].size();
 int smallest = v[0].size();
 int iterator1 = 0;
 int iterator2 = 0;  //to pushback correct items from vector

 for (int i = 0; i < v.size(); ++i)
 {
  //if largest is smaller than value, make that new largest
  if (largest < v[i].size())
  {
   largest = v[i].size();
   iterator1 = i;
  }

  //if smallest is bigger than value, make that new smallest
  if (smallest > v[i].size())
  {
   smallest = v[i].size();
   iterator2 = i;
  }
 }

 minMax.push_back(v[iterator1]); //push back largest string
 minMax.push_back(v[iterator2]); //push back smallest string

 return minMax;
}

//find alphabetically first and last string
vector<string> findAlphaB(vector<string> v_copy)
{
 vector<string> alpha;

 //sort the copy to be in alphabetical order
 sort(v_copy.begin(), v_copy.end());

 alpha.push_back(v_copy[0]);
 alpha.push_back(v_copy[v_copy.size() - 1]);

 return alpha;
}

int main()
{
 vector<string> words = { "keyboard", "cat", "nyan", "cat", "tank", "cat", "hipster", "kitty", "grumpy", "cat" };

 //find number of character in each string
 vector<int> numChar = findNumChars(words);

 //find largest and shortest strings
 vector<string> minMax = findMinMax(words);

 //find alphabetically first and last string
 vector<string> alphaB = findAlphaB(words);

 //print results
 print(numChar, minMax, alphaB);

 keep_window_open();

 return 0;
}

You could do separate functions for every task, for example, a function that only finds the longest string and a function that only finds the shortest string however, to save on code and efficiency, I decided to return these values as vectors. That way we know that the vectors (apart from number of chars in a string) will only ever have 2 values in them; the largest followed by the smallest and the first alphabetical word followed by the last. There should technically be separate print functions however we know exactly what the size of 2 vectors will be, so I consider it OK to directly access the items in them.

No comments:

Post a Comment