Group Activity # 8: Vectors

Introduction

You should start this group activity by introducing each member of the group. One group member should be the scribe, this person is responsible for recording the group's endeavors.

Each person should FIRST work on the problems below individually, then confer as a group to talk about how to do the problems. We'll collect each person's answers for credit.

Grocery Cart

Assume a struct has been declared to hold information about food in a grocery store, the name field might be "Rice Krispies" and the price field would represent the price of a box of Rice Krispies.
   struct Grocery
   {
       string name;
       double price;
   };
Write the function below.
   double CartWorth(const Vector<Grocery> & cart, int num)
   // precondition: num = # of entries in vector cart
   // postcondition: returns the sum of prices of all items in cart































First and Last

A string holds a first and last name, with the names separated by a space; four such strings are shown below.
    "Michael Jordan"
    "Rebecca Lobo"
    "Suzy Hamilton"
    "Robert Kennedy"
Write a function PrintLastFirst that is passed such a string as a parameter and that prints the last name, followed by a comma, followed by the first name. For example, the function call PrintLastFirst("Michael Jordan") should generate the output below
    Jordan, Michael
In writing PrintFirstLast you may find it useful to call the string member function substr that returns a substring of a given string, e.g., s = "rhinoceros"; t = s.substr(0,5); makes the string t == "rhino".
//          string substr(int pos, int len)
//             returns string of len chars starting at pos
//             truncates if len too big, returns empty string
//             if pos too big
Complete the function below.
  void PrintFirstLast(string name)
  // precondition: name consists of a first name followed by
  //               exactly one space, followed by a last name
  //               e.g., "Leonardo daVinci"
  // postcondition: output: lastname comma first name
  //                e.g., daVinci, Leonardo
  // NOTE: if no space in name, then "space cadet" should be printed