Spring 2018 - CS 111 - Homework 11 p. 1 Deadline 11:59 pm on Friday, April 27, 2018 Purpose To practice with loops, arrays, and more! How to submit CS 111 - Homework 11 Submit your main.cpp (or it may be renamed as well, as long as it ends in.cpp) file for this lab on https://canvas.humboldt.edu. (Remember, it is good to submit early and often!) (It is not REQUIRED for this homework -- but if you would LIKE to write any of the non-main functions in their own.h and.cpp files, as demonstrated with function cheer during Week 13 Lecture 2, you may! Just be sure to submit all of your lab's.h and.cpp files in that case.) Important notes IF you would like: FEEL FREE to include additional cout's of endl or spacing or headings between testing calls of different problems if you would like to have more-readable program output! You are still expected to follow the Design Recipe for all functions that you design/define. Remember, you will receive significant credit for the signature, purpose, header, and examples/tests portions of your functions. Typically you'll get at least half-credit for a correct signature, purpose, header, and examples/tests, even if your function body is not correct. (and, you'll lose at least half-credit if you omit these or do them poorly, even if your function body is correct). Be especially careful to include at least two examples/tests for every function, including at least one specific example/test for each "kind"/category of data, and (when there are boundaries) for boundaries between data. You can lose credit for not doing so. Be sure to follow class coding standards! Homework 11 Program Setup (with some IMPORTANT NEW STEPS!) Start NetBeans. From the File menu, select New Project.... Select Category of C/C++ and Project of C/C++ Application, and click Next>. Type 111hw11 in the Project Name box, and use Browse... to direct the Project Location folder to your desired location. (REMEMBER: in an HSU lab, this needs to be to the U: drive or to a flash drive.) You can rename the main.cpp file if you would like -- all other options should remain as they are. Then select "Finish". In the left-side window, expand the Source Files section, then double-click on the name of the file containing your main function this should open an editor window with the contents of that file. REPLACE its current contents with the "first main.cpp template" from the CS 111 public course web site, under "References".
Spring 2018 - CS 111 - Homework 11 p. 2 (You can REMOVE, or just not paste in, the very first "FIRST VERSION" comment.) Find the comment that has by: and last modified: START that comment with: CS 111 - HW 11 Then put your name after by:, and today's date after last modified:. For example: /*--- CS 111 - HW 11 by: Your Name last modified: 2018-04-21 ---*/ On the course Canvas site, on the Homework 11 assignment link, you will find links to some provided header and source code files for some functions you are to use in this homework assignment. ADD them to this project by doing the following: From Canvas, go to the Homework 11 assignment link, click on each of the links for each of these listed files, and DOWNLOAD each into the project folder for your project 111hw11: char_list.h char_list.cpp is_vowel.h is_vowel.cpp pig_lite.h pig_lite.cpp starline.h starline.cpp get_worth.h get_worth.cpp Now, in NetBeans, in the Project window on the left, for EACH of the following files, right-click on the Header Files folder for project 111hw11, select "Add Existing Item...", highlight THAT file from the 111hw11 folder, and click the "Select" button to add it to the header files for your project: char_list.h is_vowel.h pig_lite.h starline.h get_worth.h Then, in NetBeans, in the Project window on the left, for EACH of the following files, right-click on the Source Files folder for project 111hw11, select "Add Existing Item...", highlight THAT file from the 111hw11 folder, and click the "Select" button to add it to the source files for your project:
Spring 2018 - CS 111 - Homework 11 p. 3 char_list.cpp is_vowel.cpp pig_lite.cpp starline.cpp get_worth.cpp Now, open the file containing your main function, and where you see: #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; now ADD the following #includes BEFORE the using namespace std; line as shown: #include <cstdlib> #include <iostream> #include <string> #include <cmath> #include "char_list.h" #include "is_vowel.h" #include "pig_lite.h" #include "starline.h" #include "get_worth.h" using namespace std; Now you should be able to use any of the functions provided in these files in all of the functions, including the main function, in this file! COME to office hours EARLY in the week if you would like help with the above! Problem 1 In the main function, put a blank line followed by a comment: /*=== Problem 1 ===*/ The purpose of this problem is to write a while loop that is NOT a count-controlled loop. Consider Homework 9 - Problem 2's function pig_lite, which uses is_vowel, first, and rest, and which expects a word and returns a Pig-Latin-ish version of that word. (Remember, you copied the needed files for is_vowel, first, rest, and pig_lite into your 111hw11 project as part of the Homework 11 setup above.) An interactive front end for function pig_lite might be interesting -- so, in your main function, include statements that do the following (I am giving you pseudocode for a question-controlled while loop here): ask the user to enter y if they would like to "piggify" a word read in their answer while their answer is "y", ask the user to enter a word containing NO blanks they would like to see a Pig-Latinish version of read in the word they enter print to the screen, on its own line, the result of calling pig_lite with whatever the user enters ask the user to enter y if they would like to "piggify" ANOTHER loop read in their answer
Spring 2018 - CS 111 - Homework 11 p. 4 Make sure the above runs successfully. Problem 2 Find the comment: /*--- PUT YOUR SIGNATURES, PURPOSES, and FUNCTION DEFINITIONS HERE ---*/ After this comment, type a blank link, and then type the comment: /*=== ===*/ Problem 2 Now for some for loop practice. Consider -- what would you see on-screen if you called Week 12 Lab Exercise's function starline repeatedly? Remember, starline expects a desired number of stars/asterisks, has the side-effect of outputting a line of that many asterisks to the screen, and returns the number of asterisks printed to the screen. (Also note, you copied the needed files for starline into your 111hw11 project as part of the Homework 11 setup above.) Using the design recipe, write a C++ function starbox that expects a desired number of rows and a desired number of asterisks per row, has the side-effect of printing to the screen that many rows of asterisks, each with that many asterisks per row, and returns the total number of asterisks printed out. This function must appropriately use a for loop. This function must appropriately call starline. For example, starbox(3, 5) == 15 and has the side-effect of causing the following to be printed to the screen: * * * And, starbox(4, 2) == 8 and has the side-effect of causing the following to be printed to the screen: And, since starbox has side-effects, you should write tests for starbox in your main function that include a DESCRIPTION of what side-effects should be seen, along with the hoped-for true result from comparing the actually-returned value to the expected returned value. That is, for EACH of its examples/tests: it should first print a message saying that what follows should be a box of stars with <num> rows and <num> columns, followed by true, and then put that example/test in its own separate cout statement, such that the result of that test will be printed on its own line.
Spring 2018 - CS 111 - Homework 11 p. 5 Make sure starbox runs, and its tests pass! Problem 3 After Problem 2's starbox, (before your main function), type a blank link, and then type the comment: /*=== Problem 3 ===*/ The purpose of this problem is to provide more for loop practice, along with keeping track of a running total during a loop. On Homework 9, Problem 4, one of the options was to use recursion to write a function total_worth to compute the value of a string of "coin characters". Here, you will do this task using a for loop instead. Recall that, in Homework 8, Problem 5, you wrote a function get_worth that expects a character representing a coin: 'Q' or 'q' -- quarter 'D' or 'd' -- dime 'N' or 'n' -- nickel 'C' or 'c' or 'P' or 'p' -- cent/penny...and it returns the decimal worth of that coin. (Note that you copied what you need for get_worth into your 111hw11 project as part of the Homework 11 setup above.) Also recall the string class method at, that expects the position of a desired character in that string, and returns the char at that position in the string (remembering, also, that it considers the position of the first character in the string to be position 0, not 1). Think about this -- Do you see that you could use a count-controlled for loop with this at method to do something with each char within a string? (Hint: consider Week 12 Lecture 2's function vertical, that uses a count-controlled loop and the at method to "grab" each character in a string and output it on its own line (thus outputting it in a "vertical" style).) Likewise, consider Week 12 Lecture 1's function sum_pos_ints, that shows how you can use a count-controlled loop to sum all of the integers from 1 to a given integer. NOW consider: what if you had a string whose characters were coin values? For example: "qdnncdqpn" Using the design recipe, write a function sum_worth that expects a string of coin characters, and returns the sum of the decimal worths of the coin characters in that string. For example, sum_worth("qdnncdqpn") == 0.25 +.10 +.05 +.05 +.01 +.10 +.25 +.01 +.05 sum_worth("qn") == 0.30 For full credit, sum_worth must also: appropriately call and use get_worth appropriately use a for loop Make sure you have at least the required number of tests for this function, and make sure you include them
Spring 2018 - CS 111 - Homework 11 p. 6 in the main function, also. Make sure sum_worth runs, and its tests pass! Problem 4 In the main function, after your tests of Problem 3's sum_worth, put a blank line followed by a comment: /*=== Problem 4 ===*/...followed by a blank line. This problem's purpose is simply to serve an an array warm-up -- to allow you to try creating an example array and then do something with its contents. Here are the requirements: decide on how big an array you would like -- any size larger than 5 is acceptable. Declare an appropriately-named named constant whose value is this size. decide what type of value you would like for your array to contain. declare an array of that type and size, and initialize it to values of your choice. then write a for loop to do something to/with each value in your array. You can choose -- it can be as simple as simply printing each value in the array, or something more elaborate (you can modify each value, call some function of your choice for each value, etc.) Whatever you choose, make sure it has some output/result printed to the screen. Make sure the above runs successfully. Problem 5 After Problem 3's sum_worth, (before your main function), type a blank link, and then type the comment: /*=== ===*/ Problem 5 The purpose of this problem is to write a function whose parameters include an array and its size. Consider, again, function starline, that expects the number of asterisks/stars to output, and it returns that number, but also has the side-effect of printing that many asterisks on one line to the screen (if <=0, no stars are output). You could use this to create a kind of horizontal bar chart, calling it for each of a set of values. And what is an array but a set of values? Using the design recipe, write a C++ function bar_chart that expects an array of integers and its size, returns the number of rows in the resulting bar chart (that is, the array's size...!), and has the side-effect of printing to the screen a horizontal bar chart with the help of starline, printing a line of *'s the length of each array value. This function must appropriately call starline And, it must use an appropriate for loop. For example, for:
Spring 2018 - CS 111 - Homework 11 p. 7 const int NUM_MSRS = 7; int measures[num_msrs] = {3, 1, 6, 2, 8, 4, 5}; bar_chart(measures, NUM_MSRS) == NUM_MSRS...and has the side-effect of causing the following to be printed to the screen: * * * Since bar_chart has side-effects, you should write tests for bar_chart in your main function that include a DESCRIPTION of what side-effects should be seen, along with the hoped-for true result from comparing the actually-returned value to the expected returned value. That is, for EACH of its examples/tests: it should first print a message saying that what follows should rows of stars of lengths <list them>, followed by true, and then put that example/test in its own separate cout statement, such that the result of that test will be printed on its own line. Make sure bar_chart runs, and its tests pass! Problem 6 After Problem 5's bar_chart, (before your main function), type a blank link, and then type the comment: /*=== ===*/ Problem 6 The purpose of this problem is to write another function whose parameters include an array and its size. Using the design recipe, write a C++ function how_many that expects a desired string, an array of strings, and the array's size, and returns the number of times the desired string appears in that array. It is required to appropriately use a for loop. (suggestion: during the design recipe, after making your specific examples/tests, develop pseudocode for how you figured out how many times your example string appeared in your example array -- how did you know when a match was found? how do you keep track of how many you've seen so far?) (hint: for this one there had better be more than one example call! What are the different cases you should check? But as an additional hint -- if you design it well, you can probably use the same example array for more than one example call. What should be different in those calls, then?) how_many does not have side-effects, but remember that, when you write its tests in your main function, you need to also declare and initialize at least one appropriate array to use as argument for its tests, also declaring a named constant for its size. and then you can put each example/test in its own separate cout statement, such that the result will be printed on its own line.
Spring 2018 - CS 111 - Homework 11 p. 8 Make sure how_many runs, and its tests pass!