Pages

Saturday, 21 April 2018

Chapter 8 // Exercise 7, 8 - 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 7



7. Read five names into a vector<string> name, then prompt the user for the ages of the people names and store the ages in a vector<double> age. Then print out the five (name[i],age[i]) pairs. Sort the names (sort(name.begin(), name.end())) and print out the (name[i], age[i]) pairs. The tricky part here is to get the age vector in the correct order to match the sorted name vector. Hint: Before sorting name, take a copy and use that to make a copy of age in the right order after sorting name.


#include "stdafx.h"
#include "std_lib_facilities.h"
//print vectors to the screen
void print(const vector<double>& ages, const vector<string>& names)
{
 for (int i = 0; i < ages.size(); ++i)
 {
  cout << names[i] << ": " << ages[i] << endl;
 }

 cout << "--------------------------------" << endl;
}

//read five names into a vector string
vector<string> getNames(vector<string>& v)
{
 string name;

 for (int i = 0; i < 5; ++i)
 {
  cout << "Name Please: ";
  cin >> name;
  v.push_back(name);
  cout << endl;
 }

 return v;
}

//read 5 ages for names
vector<double> getAges(const vector<string>& name, vector<double>& v_age)
{
 double age;

 for (int i = 0; i < name.size(); ++i)
 {
  cout << "Age for " << name[i] << ": ";
  cin >> age;
  v_age.push_back(age);
  cout << endl;
 }

 return v_age;
}

//compare copy of name vector to new to sort ages correctly
void sortNames(vector<string> name_copy, vector<string>& name, vector<double> age_copy, vector<double>& age)
{
 sort(name.begin(), name.end());  //sort actual names

 //go through each member of sorted name
 for (int i = 0; i < name.size(); ++i)
 {
  //go through each member of original copy name
  for (int j = 0; j < name.size(); ++j)
  {
   //if sorted name matches original
   if (name[i] == name_copy[j])
   {
    //assign original age to new position to match sorted vector
    age[i] = age_copy[j];
   }
  }
 }
}


int main()
{
 //get names
 vector<string> names;
 getNames(names);

 //get ages
 vector<double> ages;
 getAges(names, ages);

 //print original vectors
 print(ages, names);

 //sort the vectors using copies to compare
 sortNames(names, names, ages, ages);

 //print vectors again to see changes
 print(ages, names);

 keep_window_open();

 return 0;
}


Chapter 8 // Exercise 8


8. Then, do that exercise again but allowing an arbitrary number of names.

The only part of this I changed was the function to get the names:

//read five names into a vector string
vector<string> getNames(vector<string>& v)
{
 string name;

 cout << "Press 'q' to stop entering names" << endl;

 while(name != "q")
 {
  cout << "Name Please: ";
  cin >> name;
  if (name == "q")
   break;
  v.push_back(name);
  cout << endl;
 }

 return v;
}

No comments:

Post a Comment