Friday 1 June 2018

Chapter 9 // Exercise 2 - 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 9 // Exercise 1


Design and implement a Name_pairs class holding (name, age) pairs where name is a string and age is a double. Represent that as a vector<string>  (called name) and a vector<double> (called age) member. Provide an input operation read_names() that reads a series of names. Provide a read_ages() operation that prompts the user for an age for each name. Provide a print() operation that prints out the (name[i], age[i]) pairs (one per line) in the order determined by the name vector. Provide a sort() operation that sorts the name vector in alphabetical order and reorganises the age vector to match. Implement all "operations" as member functions.

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

class Name_pairs
{
private:
 vector<string> name;
 vector<double> age;
public:
 void read_names(int iterator); //read in a series of names
 void read_ages();  //read in age for name
 void print();   //print out vectors
 void sortNP();   //sort the vectors alphabetically by name
};

void Name_pairs::read_names(int iterator)
{
 string names;
 for (int i = 0; i < iterator; ++i)
 {
  cout << "Name: ";
  cin >> names;
  name.push_back(names);
  cout << endl;
 }
}

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

void Name_pairs::print()
{
 cout << endl;
 for (int i = 0; i < name.size(); ++i)
  cout << "Name: " << name[i] << "     Age: " << age[i] << endl;
}

void Name_pairs::sortNP()
{
 vector<string> name_copy = name;
 vector<double> age_copy = 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()
{
 cout << "How many names to read in? > ";
 int howMany;
 cin >> howMany;

 Name_pairs namePair;

 //read in names
 namePair.read_names(howMany);

 //read in ages
 namePair.read_ages();

 //print
 namePair.print();

 //sort namePair alphabetically
 namePair.sortNP();

 //print
 namePair.print();

 keep_window_open();

 return 0;
}

This was basically chapter 8 exercise 7 but in a class form, so I just reused some of the functions from that and stuck it into a class.

8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. i used this to sort and print->
    struct sorted
    {
    string name;
    double age;
    };
    void Name_pairs::sort_name()
    {
    cout<<"Sorted: "< ss;
    sorted obj;
    for(int i=0;i<name.size();++i)
    {
    obj.name=name[i]; obj.age=age[i];
    ss.emplace_back(obj);
    }
    sort(name);
    for(auto i:name)
    {
    for(auto x:ss)
    {
    if(i==x.name)
    cout<<x.name<<" "<<x.age<<endl;

    }
    }
    }

    yours code is much simpler . thanks

    ReplyDelete
  3. there seems to be some problem while writing comments, on 8th line ss is vector of type sorted.

    ReplyDelete
    Replies
    1. Yeah code doesn't format very well in Blogger comments. There is a problem with my original code above which I corrected in the github version; basically if you have duplicate names, the ages will be messed up. This is solved by doing what you've done by having a struct of a name and age, then sorting by names and applying those values back to the vectors.

      Delete
  4. here is the design i created (exercise 9) , its unfinished -> https://pastebin.com/PFya2SEs
    problem is in rent(). if i re-rent a book, it doesnot show book already rented out .
    a.set_checkedout(b) is not changing the flag in original rack whereas i have passed the book and library objects as reference. any help will be appreciated.

    ReplyDelete
    Replies
    1. Your function to return the rack in class Library returns a copy. Therefore, the l.ret_rack() in rent() returns a copy with a updating the local copied version. Change line 72 to:
      vector<Book>& ret_rack() { return rack; }

      Delete
    2. yeah i figured it out , thanks for the help

      Delete