Absolute C++ (Walter Savitch) – Chapter 16 & 19. Lecture slides.
shuffle
for shuffling the elements of an array.
Use
the array to be shuffled as the first parameter and the size of the
array as the second parameter. Shuffling an array implies reorganizing
the elements in a random order. int a[] = {1,2,3,4,5,6,7};
shuffle(a, 7);
double b[] = {1.2, 2.2, 3.2, 4.2};
shuffle(b, 4);
string c[] = {"one", "two", "three", "four"};
shuffle(c, 4);
b) Write a template
function called maximum
that takes two values of the
same type as arguments and returns the highest value.
Person
with member variables
for last and first name. Include the constructors, member functions and
overloaded operators that you find useful. list<Person>
variable for persons and write a function for inserting Person objects
in sorted order (based on the alphabetical order of names).void insertOrdered(list<Person> &p,
Person person);
HINT! strings can be compared with operators such as < or
>.PhonebookEntry
with member variables
for a person's name and phone numbers. A single person may have many
different phonenumbers and each phonenumber should be associated with a
unique label such as "home", "mobile", "work". By "unique" we mean that
there should be only one number with the label "mobile" for the same
person. It is up to you how you want to represent the name of the
person (as separate values for first and last name, or a single string
with both).Phonebook
that is used to store and
update PhonebookEntries. Include a member function called add
that takes a PhonebookEntry as argument. If the person name in the
argument entry does not exist in the phonebook, the argument entry is
simply added to the phonebook. If the person's name already exists in
the phonebook, the argument entry is used to update the list of
phonenumbers for this person: if a label exists the number is replaced,
if a label does not exists the number is added, existing numbers that
are not updated should be kept).
a) Extend the Phonebook class in the previous part with a
function called find
for searching the phonebook.
bool Phonebook::find(string name, string
label, PhoneBookEntry& result);
The find
function should be quite flexible and
support searching by substrings. The first parameter is the name (or
substring of a name), the second parameter is the label associated with
a phone number (or substring of the label). The third argument is used
to return the name and numbers found. If the label argument is an empty
string (""), the returned entry should include all the phonenumbers
for this person, if the label argument is a non-empty string, the
numbers included should be only the one(s) that match the string in the
label argument. A boolean return
value is used to indicate if a matching entry was found or not. NB! The
function returns the first matching entry.
Example:
PhonebookEntry result;
if (pb.find("Aalb", "mob", result){
cout << result << endl; //assuming that
the << operator is overloaded
}
Result:
Trond Aalberg
mobile: 97855667
if (pb.find("Aalb", "", result){
cout << result << endl; //assuming that
the << operator is overloaded
}
Result
Trond Aalberg
mobile: 97855667
home: 76334455
work: 73597952