A11-Keeping Lists

Table of Contents


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

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are encouraged to work with one other student of this class following the rules of Pair Programming for Homework Assignments. If you choose to pair program, there is a bonus applied.
  • You may not give a copy of your code to your designated pair-programming partner if you did not develop the code together.
  • You may not look at another person's code until you complete and submit this assignment, except for code you develop code together with your pair-programming partner.
  • You may get help from people other than your pair-programming partner if you get stuck, but only if they do not show or tell you the code to type.

Preparation

  1. Make sure you have completed the exercises from lesson 10 and lesson 11.
  2. Complete the Review Exercises in CodeLab 10 and 11. These exercises will help prepare you for the problem-solving programs and should be completed first.

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 (See: Class Names)
  2. Indentation in classes and placement of curly braces
  3. Every function declaration (prototype) in the class, including constructor functions, have a function comment block
  4. Every file has a file comment block
  5. No magic numbers (See: No Magic Numbers)

Partial screen shot of program output
Image source: Ed Parrish

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
  1. Start by downloading the worksheet: vectorwork.cpp

    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, one per problem, as described by the function signature and comment block.

    Do NOT change any of the function headers and do NOT make any changes to the main() function besides uncommenting code.

  5. Write the function definitions below main(), and their prototypes above main().
  6. Do not turn in code with cout in any function besides printVector() and the cout statements already coded in main().

    You may add cout to debug code but please remove it before turning in your code.

  7. Compile and run the code when finished with each function to verify correctness. Check the test results and make any alterations to your function definitions as necessary. Do NOT change any code in main() beyond removing comments to call the functions and do not change the function headers.

    When you first start you will see warnings when compiling. Remember that code with warnings compiles but the warning is giving you hints about problems with the code.

  8. Example Run: The outputs of the program must look exactly like the following for full credit, including the same order and format of the output. There is no user input.
    ***Testing printVector***
    printVector1 should be 1 2 3 1: 1 2 3 1
    printVector2 should be 1 2 4 1: 1 2 4 1
    printVector3 should be 7: 7
    
    ***Testing findLast42***
    findLast42a should be 2: 2
    findLast42b should be 5: 5
    findLast42c should be -1: -1
    
    ***Testing eraseLast42***
    eraseLast42a should be 42 1 3 4: 42 1 3 4
    eraseLast42b should be 0 1 42 3: 0 1 42 3
    eraseLast42c should be 0 1 2 3 4: 0 1 2 3 4
    
    ***Testing b4Last42Add7***
    b4Last42Add7a should be 0 1 7 42 3: 0 1 7 42 3
    b4Last42Add7b should be 0 1 2 7 42: 0 1 2 7 42
    b4Last42Add7c should be 7 42: 7 42
    
    ***Testing insertInOrder***
    insertInOrder1 should be 0 1 2 3: 0 1 2 3
    insertInOrder2 should be 1 2 3 4: 1 2 3 4
    insertInOrder3 should be -1 2 3 42: -1 2 3 42
    
    ***End of Tests***
    
  9. When all of the tests pass, upload your completed source code file with the rest of the assignment as described in Deliverables.
Hints:
  • To find the position of the last 42 to erase or insert before, call findLast42().

♯♪ ♬

Project 2: Song Playlist

Many people like to make and share lists of songs that they enjoy hearing. These lists are often called playlists. Many music players let us play back these song either sequentially or in a shuffled order.

In this assignment, we take our Song class from Assignment 9 to develop a playlist of songs. We use a vector to make our list of songs. Visualize the vector as a series of entries in a list. Each element of the vector represents a song. The entries are identified by a number as shown below.

songs = 
string title
string artist
double rating
int length
[0]
string title
string artist
double rating
int length
[1]
string title
string artist
double rating
int length
[2]

The vector stores each Song object, one object to each box (element) as shown. We can add or delete songs in the list by adding or removing Song objects to and from the vector. Each object listed above holds four pieces of data. To access the data inside an object we use member functions like read(), print(), getTitle(), setTitle() and so on.

Project Specifications
  1. Correct any errors in your Song class from Assignment 9.

    If you had problems with your the Song class and are not sure how to correct them, ask the instructor for help. Make sure that print() uses both fixed and setprecision().

  2. Using your (possibly corrected) Song class from Assignment 9, develop a song playlist application.

    Keep the same name for the Song class. Do NOT add any new member variables to the class (including vectors) and do not remove or modify any of the existing member functions specified in the last assignment, unless you had an error that you are correcting. Make sure to #include <climits> if you use INT_MAX and #include <cstdlib> if you use exit() to ensure the program compiles on all computers.

  3. Name the source code file songlist.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled title. Naming is important in programming.

  4. Add a read() member function (and no other function) to your Song 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 Song object in the following order:
    1. title
    2. artist
    3. rating
    4. length

    The read() function must operate like the following:

    Enter the title of the song: 7 Rings
    Enter the artist of the song: Ariana Grande
    Enter the rating for 7 Rings: 5.0
    Enter the length of 7 Rings: 178
    

    In the above example, the user entered the values shown in aqua italics (for emphasis). The user must be able to enter any number of words for the songs's title with spaces between each word as shown.

  5. Use a vector to store your Song objects and add at least three of the songs from assignment 9 to the vector near the start of main().

    Thus, your songs playlist will have at least three songs included automatically when your program starts.

  6. Develop a menu to manage the songs playlist that is redisplayed after each option is completed and looks like the following:
    Welcome to the Song Playlist Manger!
    
    Please choose one of the following operations:
    0. Exit program
    1. List songs
    2. Add a song
    3. Delete a song
    4. Change song rating
    5. List by length
    Choice (0-4): _
    

    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. If the user enters a number not on the menu then output an error message and continue operating.

    Choice (0-4): 9999
    
    Invalid choice!
    
    Please choose one of the following operations:
    0. Exit program
    1. List songs
    2. Add a song
    3. Delete a song
    4. Change song rating
    5. List by length
    Choice (0-4): _
    

    Do NOT change the number of a menu operation nor have any input before the menu input.

    Design hint: Place your menu in main() and call a non-member function to perform each menu option. See lesson 10.4.2 for an example. Do not use infinite loops, which we have not covered. Instead, think of what should stop the loop and code that in the loop test condition. Also avoid while loops with no statements in the body.

  7. In addition to main() and Song 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.

    Remember that a non-member function is declared (has a prototype) outside the class.

  8. Structure your code to declare non-member function prototypes before main() and non-member function definitions after main() for all your functions.
  9. 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 aqua italics for clarity 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 songs in the playlist vector when selecting menu item 1, The listing includes the information from the print() function with a heading and item number starting at one (1) as shown below. In addition, summarize the length of all songs combined at the end of the list. Do NOT print song lists when the program starts unless the user enters menu item 1.
      Welcome to the Song Playlist Manger!
      
      Please choose one of the following operations:
      0. Exit program
      1. List songs
      2. Add a song
      3. Delete a song
      4. Change song rating
      5. List by length
      Choice (0-5): 1
      
      Songs in my playlist:
       # Title                 Artist/Band             Rating  Length
       1 Happier               Marshmello                 4.4    3:34
       2 Without Me            Halsey                     3.8    3:21
       3 Head Above Water      Avril Lavigne              5.0    3:40
      Playlist length: 10:35
      
    2. Add a song to the songs playlist when selecting menu item 2 by calling the read() member function of the Song class and inserting the new entry at the end of the playlist.
      Please choose one of the following operations
      0. Exit program
      1. List songs
      2. Add a song
      3. Delete a song
      4. Change song rating
      5. List by length
      Choice (0-5): 2
      
      Adding a new song:
      Enter the title of the song: 7 Rings
      Enter the artist of the song: Ariana Grande
      Enter the rating for 7 Rings: 4.9
      Enter the length of 7 Rings: 178
      

      Notice that the user can enter spaces within the song title.

    3. Delete a song from the playlist when selecting menu item 3. Present a playlist of songs and allow the user to enter the number of the song to remove from the playlist. This operation removes the Song object from the vector. Do NOT list the songs after deleting unless the user enters menu item 1.
      Please choose one of the following operations:
      0. Exit program
      1. List songs
      2. Add a song
      3. Delete a song
      4. Change song rating
      5. List by length
      Choice (0-5): 3
      
      Deleting a song:
      Songs in my playlist:
       # Title                 Artist/Band             Rating  Length
       1 Happier               Marshmello                 4.4    3:34
       2 Without Me            Halsey                     3.8    3:21
       3 Head Above Water      Avril Lavigne              5.0    3:40
       4 7 Rings               Ariana Grande              4.9    2:58
      Playlist length: 13:33
      Enter the number of the song: 1
      

      Notice that the program inserted the song from the previous step at the end of the list.

    4. Change the rating for a song when selecting menu item 4. Present a numbered playlist for the user to choose from and allow the user to choose which song to change by entering their number.
      Please choose one of the following operations:
      0. Exit program
      1. List songs
      2. Add a song
      3. Delete a song
      4. Change song rating
      5. List by length
      Choice (0-5): 4
      
      Change rating for a song:
      Songs in my playlist:
       # Title                 Artist/Band             Rating  Length
       1 Without Me            Halsey                     3.8    3:21
       2 Head Above Water      Avril Lavigne              5.0    3:40
       3 7 Rings               Ariana Grande              4.9    2:58
      Playlist length: 9:59
      Enter the number of the song: 2
      Enter the new rating: 4.8
      

      Which updates the rating for the song.

    5. List the songs if their lengths are within a specified range by selecting menu item 5. Allow the user to enter the minimum and then maximum length before listing the songs.
      Please choose one of the following operations:
      0. Exit program
      1. List movies
      2. Add a movie
      3. Delete a movie
      4. Change movie revenue
      5. List by length
      Choice (0-5): 5
      
      Searching by length
      Enter the starting length in seconds: 180
      Enter the ending length in seconds: 220
      
      Songs in my playlist:
      Title                 Artist/Band             Rating  Length
      Without Me            Halsey                     3.8    3:21
      Head Above Water      Avril Lavigne              4.8    3:40
      

      Which lists only those movies within the year range. Notice that the numbering of each movie is optional because the user does not select a movie from the listing.

  10. Submit this project with the rest of the assignment as described in Deliverables.
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 "Partial Listing of prodvector.cpp" in Exercise 10.4. 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); 
  3. When adding a song title and artist, remember how getline() and cin >> ws; work together. See lesson 6.1.4.
  4. To update the rating:
    1. Ask the user for the position of the song in the vector:
      cin >> position;

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

    2. Ask for the rating and use the setRating() function to update the rating:
      list[position].setRating(input);
      
  5. To see an example of how the program operates, download and run the executable file from the command line:

Extra Credit

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 correctly, 1 point for robust test cases, and 1 point for inserting or deleting elements)
    1. Make up a new descriptive name for your function and submit the extra function and its main() function in a separate file named xcvectorwork.cpp.

      The purpose of main() is to test the function. Do not include the rest of the vectorwork functions in this file--only include your new function and main().

    2. The function must have a vector parameter and must find, insert or delete values in the vector in a significantly different problem than vectorwork.
    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 ***
      

      Use the name of your function instead of 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. Each case must display all on one line and have an expected result like:
      myFabFun should be 42: 42
      

      Use the name of your function instead of myFabFun and show correct results.

    6. Be sure to credit the source of the extra function idea, even if yourself, in both the function comments and README.txt.

Describe any extra credit attempted in your README.txt file.

Tutorial Lab

To prepare for the next lesson, complete the following exercises.

  1. Type the babynames.cpp program from the textbook on pages 354-357 (332 -333) into a text editor, and then compile and run the program. Submit the source code file (babynames.cpp) to Canvas for grading. Use the babynames.txt file to test your program.
  2. Complete the Tutorial Exercises in CodeLab 10 and 11 before the specified due date. Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. Click the "Solution" tab if you get stuck on a Tutorial problem.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

Lesson Exercises

  • 2: All lesson exercises attempted and turned in
  • 1: Some lesson exercises completed and turned in
  • 0: No lesson exercises completed or turned in

Vector Function Worksheet Functionality

  • 5: All functions completed and program generates the correct output without error
  • 4: All functions completed but has a minor error
  • 3: Most functions completed or has some small errors
  • 2: At least half the functions completed or has some errors
  • 1: Few functions completed or has many errors
  • 0: Does not compile or wrong file turned in

Favorite Movie List Functionality

  • 10: Demonstrates mastery of the assignment
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • Runs to completion with no abnormal error conditions
    • Generates correct output given correct input
    • Behaves in a reasonable way in response to incorrect data
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Attempts to meet all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Attempts to meet at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Attempts to meet less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 1: Does not compile or wrong file turned in
  • 0: Does not execute, not turned in or uses techniques not covered in course

Program Style

  • 3: Code is well-documented including:
    • Correct file name used
    • Name, date, and program description in file comment block
    • Follows specified format for file comment block
    • Has a function comment block for all function declarations following the specified format
    • Proper use of spaces around operators
    • No tab characters are present in the source code
    • As described in How To Document and Organize C++ Code
    • Correct file name used
  • 2: Code has a minor documentation error
  • 1: Code has some documentation errors
  • 0: No apparent attempt to follow documentation standards or write documentation comments

CodeLab and Other Tutorial Exercises

  • Number CodeLab completed correctly / number exercises * 8 and rounded up to the nearest integer.
  • -1 if the tutorial exercise file does not compile
  • -2 if the tutorial exercise file is not turned in

README.txt File

  • 2: README.txt file submitted following the instructions
  • 1: README.txt file submitted but some information was missing
  • 0: No README.txt file submitted

Total possible: 30, plus extra credit

Deliverables

Submit your assignment to Canvas, in the assignment folder A11-Keeping Lists, following the instructions for submitting homework. Include the following items for grading:

  1. README.txt file
  2. All the exercise files from lesson 10 and lesson 11
  3. vectorwork.cpp
  4. songlist.cpp
  5. babynames.cpp from the Tutorial Lab Exercises

Your assignment must work as submitted, so submit all the files needed to complete your assignment. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.

Last Updated: April 29 2019 @15:22:29