/*======== Fall 2025 - CS 111 Week 14 Lab Exercise - save as lab14.cpp date: 2025-12-05 ========*/ /*--- USING pair-programming * COPY and PASTE the contents of this file into a file in the CS50 IDE named: lab14.cpp * ADD the parts asked for below *to* this file as specified (one student saying what to type, the other student typing it into the CS50 IDE) * each time you want to compile: in a CS50 terminal that is open to the folder CONTAINING this .cpp file, ("Open in Integrated Terminal"), type: g++ lab14.cpp -o lab14 * IF it compiles with no errors: to run: in that same CS50 terminal that is open to the folder CONTAINING this .cpp file, type: ./lab14 * When you are satisfied with its output, create an example output file by typing: ./lab14 > lab14-out.txt * Download copies of your resulting lab14.cpp and lab14-out.cpp ******** and ALSO the file your program creates, array-contents.txt ******** ...by right-clicking on their names in the file explorer on the left of the CS50 IDE, and use Gmail to MAIL a copy of ALL THREE of these files to BOTH of you. * And, EACH of you should SUBMIT these THREE files, *** lab14.cpp AND lab14-out.txt AND array-contents.txt *** to Canvas BEFORE you leave lab. * (Because of the importance of the Students' Perceptions NSF Grant Survey, there is NO Pair-Programming Peer Review Survey for the Week 14 Lab Exercise.) ---*/ /*--- by: PUT BOTH of YOUR NAMES HERE last modified: 2025-12-05 ---*/ /*--- WEEK 14 LAB EXERCISE - PROBLEM 1 ---*/ /*--- Below, add the #include needed for stream-based file input/output in C++ ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*--- WEEK 14 LAB EXERCISE - PROBLEM 2 ---*/ /*--- The purpose of this problem is to practice writing a "classic" for-statement. During Week 14 Lecture 2, we REFACTORED our sum_array function from Week 14 Lecture 2, replacing its count-controlled while-statement with a for-statement. Below is Week 13 Lecture 1's function cheer, which uses a count-controlled while-statement. REFACTOR it -- MODIFY its function body -- as follows: * Replace its current while-statement with an appropriate for-statement. * As you are doing so, include at least ONE appropriate use of a C++ "SHORTHAND" operator (++, --, +=, *=, /=, or -=) (Its tests are already in the main function below, ready to help you determine if your refactoring was successful.) ----*/ /*=== signature: cheer: int -> int purpose: expects the number of HIPs in a cheer desired, has the SIDE-EFFECT of printing to the screen that many HIPs, each on their own line, followed by HOORAY!, and returns the number of HIPs actually printed tests: cheer(3) == 3 but also has the side-effect of printing to the screen: HIP HIP HIP HOORAY! cheer(5) == 5 but also has the side-effect of printing to the screen: HIP HIP HIP HIP HIP HOORAY! cheer(0) == 0 but also has the side-effect of printing to the screen: HOORAY! cheer(-13) == 0 but also has the side-effect of printing to the screen: HOORAY! ===*/ int cheer(int num_hips) { int count = 0; // print HIP to the screen num_hips times while (count < num_hips) { cout << "HIP" << endl; count = count + 1; } // ...THEN print HOORAY! cout << "HOORAY!" << endl; return count; } /*--- WEEK 14 LAB EXERCISE - PROBLEM 3 --- */ /*--- Purpose: to practice writing another function with an array parameter Use the design recipe to design a function indexed_display, that expects an array of string values and its size, has the side effect of printing to the screen the strings in that array, one array element per line, BUT each PRECEDED by its array index, a colon, and a blank**, and returns the number of lines printed. For example, if you have: string pets[3] = {"Snoopy", "Ginger", "Caesar"}; then: indexed_display(pets, 3) == 3 ...and has the side-effect of printing to the screen: 0: Snoopy 1: Ginger 2: Caesar ** ALTERNATE OPTION: you can use a dash, or a blank, or a set of square brackets, or some combination, to tastefully set apart the array index and the string at that index FOR FULL CREDIT: * Appropriately use a "classic" for-statement in your function. * Include at least ONE appropriate use of a C++ "SHORTHAND" operator (++, --, +=, *=, /=, or -=) * BE SURE to write at least TWO tests for this, using at least two DIFFERENT arrays of DIFFERENT sizes. ---*/ /*--- signature: purpose: tests: ---*/ /*--- this week - this main function will give you a chance to: * test the functions above, * practice a bit more with arrays, * practice writing to a file, and * practice reading from a file. ----*/ int main() { cout << boolalpha; cout << endl; cout << "*** Testing: refactored version of cheer ***" << endl; cout << "should see 3 HIPs then a HOORAY! then true" << endl; cout << (cheer(3) == 3) << endl; cout << "should see 5 HIPs then a HOORAY! then true" << endl; cout << (cheer(5) == 5) << endl; cout << "should see 0 HIPs then a HOORAY! then true" << endl; cout << (cheer(0) == 0) << endl; cout << "should see 0 HIPs then a HOORAY! then true" << endl; cout << (cheer(-13) == 0) << endl; cout << endl; cout << "*** Testing: indexed_display ***" << endl; /*--- WEEK 14 LAB EXERCISE - PROBLEM 4 ---*/ // Declare an array of at least 10 string elements of your choice. // * Give it an appropriate descriptive name // * There are several ways to fill an array with an initial // set of values -- choose one, and use it to fill your array. /*--- WEEK 14 LAB EXERCISE - PROBLEM 5 ---*/ cout << endl; // Call your indexed_display function with your array from // Problem 4 and its size as its arguments. // // * NOTE: we don't need what indexed_display returns in this // particular call -- so write this function call expression // like a statement, by itself and followed by a semicolon! /*--- WEEK 14 LAB EXERCISE - PROBLEM 6 ---*/ // Declare an OUTPUT file stream with an appropriate name of // your choice // Open this output file stream so that it will be able to // write to a file named array-contents.txt // Now write a for-statement to write the contents of your array // from Problem 4 to the file array-contents.txt, // one array element per line. // Close your output file stream /*--- WEEK 14 LAB EXERCISE - PROBLEM 7 ---*/ // Declare an INPUT file stream with an appropriate name of // your choice // Open this input file stream so that it will be able to // read from the file array-contents.txt // Read at least three things from the file array_contents.txt, // printing to the screen what you read in a readable, // attractive fashion. // (That is, you are not required to read everything from this // file -- just show that you can read at least three things // from it.) // Close your input file stream return EXIT_SUCCESS; } /*--- Remember: once you have compiled and run these and are satisfied with them, * DOWNLOAD copies of this file lab14.cpp AND your example output file lab14-out.txt AND your resulting file array-contents.txt, and use Gmail to E-MAIL copies of ALL THREE of these files to BOTH of you. * BOTH of you should submit your files *** lab14.cpp AND lab14-out.txt AND array-contents.txt *** to Canvas BEFORE you leave lab. * (But, because of the importance of the Students' Perceptions NSF Grant Survey, there is NO Pair-Programming Peer Review Survey for the Week 14 Lab Exercise.) ---*/