Schedule‎ > ‎

## Objectives

• Code vectors to store data.
• Implement common vector algorithms.
• Write functions to manipulate vectors.
• Explore code reuse with classes
• Write member functions for classes that read data from the console.
• Write member functions for classes that write data to the console.
• Code vectors to work with objects

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:
• Most importantly, you undermine your own self confidence when you cheat, because you know you didn't accomplish the assignment without cheating.
• You may not give/show/post a copy of your code to anyone else.
• You may not look at another student's code until you complete and submit this assignment.
• You may get help from other people but only if they do not show or tell you the code to type.
• Remember that the instructor performs similarity tests on programming project submissions, and copied or plagiarized code is usually very easy to detect.

## Grading Criteria (30pts + Extra Credit 3pts)

For each part: 10 pts: File Header 1pt, Function Headers 1pt, Compiles 2pts, Proper Formatting 1pt, Works as Specified 10pts

1. vectorwork.cpp (15pts)
2. contacts.cpp (15pts)
3. Extra Credit: xcvectorwork.cpp (3pts)

Total Possible: 30 pts + Extra Credit 3pts

Project Specifications

Your solutions to these projects must use only techniques we have covered so far.

Programming Style

For all programs, remember to follow all the style rules we have covered, as well as the newer rules, including:

1. Class naming conventions
2. Indentation in classes and placement of curly braces
3. Every nontrivial function declaration (prototype) in the class must have a function comment block
4. Every file has a file comment block
5. No magic numbers

## Project 1: Vector Function Worksheet

Vectors are a convenient way to store and process a list of data. They work much like arrays but can grow and shrink to accommodate the number of elements needed.

Use this worksheet to improve your understanding of vectors and functions. Refer to lesson 10 for more information on vectors.

Project Specifications

Keep the same filename and add to the existing code to complete the project. Leave the existing code unchanged, except for comments as instructed.

2. Add your name and the date to the file comment block at the top of the file, replacing the words Your Name with your first and last name and Date Here with the current date.
3. No user input is required for this project and do not add any.
4. Write the required functions as described by the function signature and comment block.

Do NOT change any of the function signatures.                                                                                                                                                                                                                                                                                                                                        Image source: Ed Parrish

5. Write the function definitions below `main()`, and their prototypes above `main()`.
6. Compile and run the code when finished with each function to verify correctness.

Uncomment the function calls from `main()` as you progress. Do NOT change any code in `main()` beyond removing comments to run the code.

7. Submit this project with the rest of the assignment.

## Project 2: Contact List

A contact list is a collection of names and other information about people. To find information about a person, and to contact them, the user selects a name from a list. In this assignment, we take our `Person` class from Assignment 9 to develop a contact list.

If you weren't able to complete person.cpp use this:  person.cpp

We are using a vector to make our list of contacts. Visualize the vector as a series of entries in a list. Each element of the vector represents a contact. The entries are identified by a number as shown below.

`contacts = `
 `string name` `int age` `double income`
`[0]`
 `string name` `int age` `double income`
`[1]`
 `string name` `int age` `double income`
`[2]`

The vector holds each `Person` object, one object to each slot (element) as shown. We can add or delete contacts in the list by adding or removing `Person` objects to and from the vector. Each object listed above holds three pieces of data. To access the data inside an object we use member functions like `read()``print()``getName()``setName()` and so on.

Project Specifications

1. Using your `Person` class from Assignment 9, develop a contact list application.  If you weren't able to complete it:  use this person.cpp
2. You must name the source code file `contacts.cpp` and include all your code in this single file
3. Create a vector to store your `Person` objects and add at least three of the persons from the previous assignment to the vector.

Thus, your contacts list will have at least three contacts included automatically when your program starts.

4. Add a `read()` member function to your `Person` class. The function has no parameters and returns nothing. The `read()` function reads data from `cin` and stores it into the member variables of the `Person `object in the following order:
1. `name`
2. `age`
3. `income`

The `read()` function must operate like the following:

```Enter the name of the person: Joe Schmoe
Enter the age for Joe Schmoe: 24
Enter the income for Joe Schmoe: 9876.54
```

In the above example, the user entered the values shown in italics (for emphasis). The user must be able to enter spaces in the person's name as shown.  See the getline() sample in Key Sample Code.

5. Develop a menu to manage the contacts list that is redisplayed after each option is completed and looks like the following:
```Please choose one of the following operations:
0. Exit program
1. List contacts
2. Add a contact
3. Delete a contact
4. Change contact income
5. List by age range
Choice (0-5): _
```

The user enters the number of the operation on the line labeled "Choice" where the underbar "`_`" is shown. The underbar "_" is shown for information only and is not part of the program display. Entering a `0` exits the program and that is the only way to exit the program. Do NOT change the number of a menu operation.

Hint: Call a function to perform each menu option.

6. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. Data entry is shown in italics for information only and is not part of the program display. For the input shown you must get the same output. However, the output must change properly if the inputs are different.

1. List the contacts in the contacts list, which includes the information from the `print()` function with a heading and item number starting at one as shown below.
```Please choose one of the following operations:
0. Exit program
1. List contacts
2. Add a contact
3. Delete a contact
4. Change contact income
5. List by age range
Choice (0-5): 1

Contact list:
# Name                 Age     Income
1 Sophie Engineer       42  102280.00
2 Emma Hacker           24   71916.00
3 John Q Public         37   55775.37
```
2. Add a contact to the contacts list when selecting menu item 2 by calling the `read()` member function of the `Person` class and inserting the new entry at the end of the list.
```Please choose one of the following operations:
0. Exit program
1. List contacts
2. Add a contact
3. Delete a contact
4. Change contact income
5. List by age range
Choice (0-5): 2

Adding a new contact:
Enter the name of the person: Joe Schmoe
Enter the age for Joe Schmoe: 27
Enter the income for Joe Schmoe: 9876.54
```

Notice that the user can enter spaces within the contact name.

3. Delete a contact from the list when selecting menu item 3 and entering the number of the contact on the list. This operation removes the `Person` object from the vector.
```Please choose one of the following operations:
0. Exit program
1. List contacts
2. Add a contact
3. Delete a contact
4. Change contact income
5. List by age range
Choice (0-5): 3

Deleting a contact:
Contact list:
# Name                 Age     Income
1 Sophie Engineer       42  102280.00
2 Emma Hacker           24   71916.00
3 John Q Public         37   55775.37
4 Joe Schmoe            27    9876.54
Enter the number of the contact: 1
```

Notice that the program inserted the contact from the previous step into the system at the requested location.

4. Changing the income for a contact.
```Please choose one of the following operations:
0. Exit program
1. List contacts
2. Add a contact
3. Delete a contact
4. Change contact income
5. List by age range
Choice (0-5): 4

Change income for a contact:
Contact list:
# Name                 Age     Income
1 Emma Hacker           24   71916.00
2 John Q Public         37   55775.37
3 Joe Schmoe            27    9876.54
Enter the number of the contact: 2
Enter the new income: 54321.42
```

Which updates the income for the contact.

5. Listing the contacts by age range.
```Please choose one of the following operations:
0. Exit program
1. List contacts
2. Add a contact
3. Delete a contact
4. Change contact income
5. List by age range
Choice (0-5): 5

Searching by Age
Enter the minimum age: 20
Enter the maximum age: 30
Name                 Age     Income
Emma Hacker           24   71916.00
Joe Schmoe            27    9876.54
```

Which lists only those contacts within the age range.

7. In addition to the `main()` and the `Person` class member functions, define between three and eight non-member functions and call all the non-member functions at least once to perform an action.

A non-member function does not have a prototype declared inside the class.

8. Structure your code such to declare non-member function prototypes before `main()` and non-member function definitions after `main()` for all your functions.
9. Submit this project with the rest of the assignment.
Hints:
1. For the menu, use a sentinel controlled loop with conditional statements to select the operations. See Exercise 10.4 step 8.
2. To set the three initial objects in a vector, see the Exercise 10.4.  Like this:
`myStore[0] = Product("Milk", 3.95);`
3. Another way is to use the `push_back()` function on an empty vector like:
```vector<Product> list;
Product milk("Milk", 3.95);
list.push_back(milk); ```
4. When adding a contact name, remember how `getline()` and `cin >> ws;` work together. See Key Sample Code.
5. To update the income:
1. Ask the user for the position of the person in the vector:
`cin >> position;`

Adjust the position entered as needed to match the vector index.

2. Ask for the income and use the `setIncome()` function to update the income:
```list[position].setIncome(input);
```

## Extra Credit (3 pts)

The following are worth extra credit points if the main program works well:

1. Create your own interesting vector function problem for the vector-function worksheet. (1 point for completing, 1 point for robust test cases, and 1 point for interest and creativity)
1. Submit the extra function and its `main()` function in a file named `xcvectorwork.cpp`.
2. The function must have a vector parameter.
3. Do not have any user input in the extra credit file.
4. Label the tests in `main()` with a `cout` statement with the words "Testing Extra Credit" followed by the function name, like:
```*** Testing Extra Credit myFabFun ***
```
5. Following the label, include at least 3 test cases calling the extra function in `main()`, testing different aspects like the test cases in the standard project.

Solution Code:

Part 1:  vectorwork.cpp

`/**`
`    CS-11 Asn 10`
`    vectorwork.cpp`
`    Purpose: a worksheet to improve your skills with vectors.`

`    @author Sharon Strelitz`
`    @version 1.0 12/10/2017`
` */`
`#include <iostream>`
`#include <vector>`
`using namespace std;`

`/**`
`    Returns true if 10 appears as either the first or last element in the`
`    vector AND the vector size >= 1. Examples:`
`    first10Last({1, 2, 10}) ? true`
`    first10Last({10, 1, 2, 3}) ? true`
`    first10Last({13, 10, 1, 2, 3}) ? false`

`    @param data The vector to test.`
`    @param return true if 10 is the first or last element; otherwise false.`
`*/`
`bool first10Last(const vector<int>& data);`

`/**`
`    Returns true if the first element and the last element are equal`
`    AND the vector size >= 1. Examples:`
`    equalFirstLast({1, 2, 3}) ? false`
`    equalFirstLast({1, 2, 3, 1}) ? true`
`    equalFirstLast({1, 2, 1}) ? true`

`    @param data The vector to test.`
`    @param return true if 10 is the first or last element; otherwise false.`
`*/`
`bool equalFirstLast(const vector<int>& data);`

`/**`
`    Prints all the elements in a vector to the console on one line.`
`    SDo NOT print commas between elements. Examples:`
`    printVector({1,2,3}) -> 1 2 3`
`    printVector({10, 30, 50, 79, 85}) -> 10 30 50 79 85`
`    printVector({5}) -> 5`

`    @param data The vector to print.`
`*/`
`void printVector(const vector<int>& data);`

`/**`
`    Adds 2 to each element in the vector. Examples:`
`    plus2({1,2,3}) -> {3, 4, 5}`
`    plus2({10, 30, 50, 79, 85}) -> {12, 32, 52, 81, 87}`
`    plus2({5}) -> {7}`

`    @param data The vector to modify.`
`*/`
`void plus2(vector<int>& data);`

`/**`
`    Multiplies each element in the vector by itself. Examples:`
`    square({1,2,3}) -> {1, 4, 9}`
`    square({3, 5, 6, 8, 9}) -> {9, 25, 36, 64, 81}`
`    square({5}) -> {25}`

`    @param data The vector to modify.`
`*/`
`void square(vector<int>& data);`

`int main()`
`{`

`    bool answer;`
`    cout << boolalpha;;`
`    cout << "***Testing first10Last***" << endl;`
`    const int A3 = 3, A10 = 10, A13 = 13;`
`    vector<int> data1 = {1, 2, A10};`
`    answer = first10Last(data1);`
`    cout << "first10Last1 should be true: " << answer << endl;`
`    vector<int> data2 = {A10, 1, 2, A3};`
`    answer = first10Last(data2);`
`    cout << "first10Last2 should be true: " << answer << endl;`
`    vector<int> data3 = {A13, A10, 1, 2, A3};`
`    answer = first10Last(data3);`
`    cout << "first10Last3 should be false: " << answer << endl;`

`    cout << "\n***Testing equalFirstLast***"<< endl;`
`    vector<int> data4 = {1, 2, A3};`
`    answer = equalFirstLast(data4);`
`    cout << "equalFirstLast1 should be false: " << answer << endl;`
`    const int A20 = 20, A50 = 50, A60 = 60, A80 = 80, A90 = 90;`
`    vector<int> data5 = {A10, A20, A50, A60, A80, A90, A10};`
`    answer = equalFirstLast(data5);`
`    cout << "equalFirstLast2 should be true: " << answer << endl;`
`    vector<int> data6 = {1};`
`    answer = equalFirstLast(data6);`
`    cout << "equalFirstLast3 should be true: " << answer << endl;`

`    cout << "\n***Testing printVector***" << endl;`
`    vector<int> data7 = {1, 2, A3};`
`    cout << "printVector1 should be 1 2 3: ";`
`    printVector(data7);`
`    const int A30 = 30, A79 = 79, A85 = 85;`
`    vector<int> data8 = {A10, A30, A50, A79, A85};`
`    cout << "printVector2 should be 10 30 50 79 85: ";`
`    printVector(data8);`
`    const int A5 = 5;`
`    vector<int> data9 = {A5};`
`    cout << "printVector3 should be 5: ";`
`    printVector(data9);`

`    cout << "\n***Testing plus2***" << endl;`
`    plus2(data7);`
`    cout << "plus2a should be 3 4 5: ";`
`    printVector(data7);`
`    plus2(data8);`
`    cout << "plus2b should be 12 32 52 81 87: ";`
`    printVector(data8);`
`    plus2(data9);`
`    cout << "plus2c should be 7: ";`
`    printVector(data9);`

`    cout << "\n***Testing squareArray***" << endl;`
`    vector<int> data10 = {1, 2, A3};`
`    square(data10);`
`    cout << "square1 should be 1 4 9: ";`
`    printVector(data10);`
`    const int A6 = 6, A8 = 8, A9 = 9;`
`    vector<int> data11 = {A3, A5, A6, A8, A9};`
`    square(data11);`
`    cout << "square2 should be 9 25 36 64 81: ";`
`    printVector(data11);`
`    vector<int> data12 = {A5};`
`    square(data12);`
`    cout << "square3 should be 25: ";`
`    printVector(data12);`

`    cout << "***End of Tests***" << endl;`

`    return 0;`

`}`

`bool first10Last(const vector<int>& data){`
`    if (data.size() >= 1)  {`
`        if (data[0] == 10 || data[data.size() - 1] == 10)  {`
`            return true;`
`        }`
`    }`
`    return false;`
`}`

`bool equalFirstLast(const vector<int>& data)  {`
`        if (data.size() >= 1)  {`
`        if (data[0] == data[data.size() - 1])  {`
`            return true;`
`        }`
`    }`
`    return false;`
`}`

`void printVector(const vector<int>& data){`
`    for (int i = 0; i < (int) data.size(); i++)  {`
`        cout << data[i] << " ";`
`    }`
`    cout << endl;`
`}`

`void plus2(vector<int>& data) {`
`    for (int i = 0; i < (int) data.size(); i++)  {`
`        data[i] += 2;`
`    }`
`}`

`void square(vector<int>& data) {`
`    for (int i = 0; i < (int) data.size(); i++)  {`
`        data[i] *= data[i];`
`    }`
`}`

Part 2:  contacts.cpp

`/**`
`    CS-11 Asn 10, person.cpp`
`    Purpose: Store info about a person`

`    @author Sharon Strelitz`
`    @version 1.0  02/20/2018`
` */`
`#include <iostream>`
`#include <iomanip>`
`using namespace std;`
`const int NAME_WIDTH = 18;`
`const int AGE_WIDTH = 6;`
`const int INCOME_WIDTH = 11;`

`class Person {`
`  public:`
`    Person();  //default Constructor`
`    Person(string newName, int newAge, double newIncome);`
`    string getName() const;`
`    int getAge() const;`
`    double getIncome() const;`
`    void setName(string newName);`
`    void setAge(int newAge);`
`    void setIncome(double newIncome);`
`    void print() const;`
`    void read() const;`
`  private:`
`    string name;`
`    int age; `
`    double income;`
`};`

`Person::Person()  {`
`  name = "";`
`  age = 0;`
`  income = 0;`
`}`

`Person::Person(string newName, int newAge, double newIncome){`
`  name = newName;`
`  age = newAge;`
`  income = newIncome;`
`}`
` `
`string Person::getName() const {`
`        return name;`
`}`

`int Person::getAge() const {`
`        return age;`
`}`
`double Person::getIncome() const {`
`        return income;`
`}`
`void Person::setName(string newName) {`
`        name = newName;`
`}`

`void Person::setAge(int newAge) {`
`        age = newAge;`
`}`
`void Person::setIncome(double newIncome) {`
`        income = newIncome;`
`}`
`/**`
`    print:  print the information about a person`

`    @param  no input parameters`
`    @return void`
`*/`
`void Person::print() const {`
`    cout << fixed << setprecision(2);`
`    cout << setw(NAME_WIDTH) << left << getName();`
`    cout << setw(AGE_WIDTH) << right << getAge();`
`    cout << setw(INCOME_WIDTH) << right << getIncome() << endl;  `
`}`

`void Person::read() const {`
`    cout << "Enter the name of the person: ";`
`    string name;`
`    cin >> ws; /* clear whitespace from buffer, avoids`
`                  problems when you mix cin and getline*/`
`    getline(cin, name);`
`    cout << "Enter the age for " << name << ": ";`
`    int age;`
`    cin >> age;`
`    cout << "Enter the income for " << income << ": ";`
`    int income;`
`    cin >> income;`
`    cout << "Your age: " << age `
`         << "full name: " << name `
`         << "income: "  << income << endl;`
`}`

`int main() {`
`  vector<Person> personDB();`
`  Person person1;`
`  person1.setName("Jose Engineer");`
`  person1.setAge(30);`
`  person1.setIncome(100000);`
`  Person person2("Maria Engineer", 22, 100000);`
`  Person person3("John Driver", 30, 12000);`
`  personDB[0] = person1;`
`  personDB[1] = person2;`
`  personDB[2] = person3;`
`  cout << setw(NAME_WIDTH) << left << "Name";`
`  cout << setw(AGE_WIDTH) << right << "Age";`
`  cout << setw(INCOME_WIDTH) << right << "Income" << endl;  `
`  person1.print();`
`  person2.print();`
`  person3.print();`

`}`

Subpages (1):