/*======== Fall 2025 - CS 111 Week 13 Lab Exercise - save as lab13.cpp date: 2025-11-21 ========*/ /*--- USING pair-programming * COPY and PASTE the contents of this file into a file in the CS50 IDE named: lab13.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++ lab13.cpp -o lab13 * IF it compiles with no errors: to run: in that same CS50 terminal that is open to the folder CONTAINING this .cpp file, type: ./lab13 * When you are satisfied with its output, create an example output file by typing: ./lab13 > lab13-out.txt * Download copies of your resulting lab13.cpp and lab13-out.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 these files to BOTH of you. * And, EACH of you should SUBMIT these TWO files, *** lab13.cpp AND lab13-out.txt *** to Canvas BEFORE you leave lab. * REMEMBER to also answer the "Week 13 Lab Exercise - Pair-Programming Peer Review Survey" in Canvas, posted along with this lab exercise, by 11:59 pm TONIGHT (Friday, November 21) ---*/ /*--- by: PUT BOTH of YOUR NAMES HERE last modified: 2025-11-21 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*--- WEEK 13 LAB EXERCISE - PROBLEM 1 ---*/ /*--- The purpose of this problem is to make sure that you are familiar with the class indentation style for while statements (and also when an if statement is contained within a while statement's body). LOOK over Week 13's posted example functions cheer, sum_ints, and vertical, showing the required class style for C++ while statements: while (bool_expr) { statement; ... statement; } The following function header and body for function count_upper are syntactically correct, but their style does NOT follow class coding standards! So -- MODIFY the function below to FOLLOW the class style! (The function count_upper is tested in the main function below, so you can make sure it still works after you fix its style.) NOTE!! The instructor will HAPPILY check over your answer IN LAB before you submit this and let you know if your version meets class style!!! <-- just ask! ---*/ /*=== signature: count_upper: string -> int purpose: expects a message, and returns the number of uppercase letters (A .. Z) in that message tests: count_upper("") == 0 count_upper("moo baa") == 0 count_upper("Hello, HOW are YOU?") == 7 ===*/ /*--- IMPROVE the FORMATTING of this WORKING function to MEET CS 111 CLASS CODING STANDARDS ---*/ int count_upper(string message){int position = 0; int num_upper = 0;int msg_length = message.length(); char curr_char; while (position < msg_length){curr_char = message.at(position); if ((curr_char >= 'A') and (curr_char <= 'Z')){num_upper = num_upper + 1;} position = position + 1;}return num_upper;} /*--- WEEK 13 LAB EXERCISE - PROBLEM 2 ---*/ /*--- Purpose: to practice writing a function with a while statement Using the design recipe, write a function repeat_str that expects a string and the number of times it should be repeated, and it returns a string whose contents are the given string repeated that many times. It should return an empty string if given a number of times that is 0 or negative. For example, repeat_str("moo ", 3) == "moo moo moo " repeat_str("What?", 4) == "What?What?What?What?" repeat_str("unreasonable", 0) == "" repeat_str("really?!", -3) == "" ***NOTE: this function happens to have NO side-effects!*** For FULL credit: * appropriately use a while loop in this function. * include at least 4 appropriate tests (2 for non-empty strings of different lengths and different numbers of repetitions, 1 for a non-empty string and 0 repetitions, 1 for a non-empty string and a negative number of repetitions) ---*/ /*--- signature: purpose: tests: ---*/ /*--- WEEK 13 LAB EXERCISE - PROBLEM 3 --- */ /*--- Purpose: to practice writing a another function with a while statement, and also one that happens to have side-effects in addition to returning a value Use the design recipe to design and write a C++ function starline that: * expects a desired number of asterisks/stars to print to the screen * has the SIDE-EFFECT of printing to the screen that many asterisks on a single line, followed by a newline character * then returns the number of asterisks/stars actually printed to the screen (So, if the argument is <= 0, no asterisks should be printed, just a newline, and it should return 0.) for example, starline(4) == 4 ...and also has the side-effect of printing to the screen: **** For FULL credit: * appropriately use a while loop in this function. * for its tests, describe its side-effects ALSO along with its bool expression (as done for cheer in W13-1 examples and for vertical in W13-2 examples) * when you RUN those tests in main, *before* your cout containing its bool expression, also write a cout describing what side-effect you should see for that test. (for example, for cheer we said something like: cout << "should see 3 HIPs then a HOORAY! then true" << endl; cout << (cheer(3) == 3) << endl; ) * include at least 4 appropriate tests (2 for different positive numbers of stars, 1 for 0 stars, 1 for a negative number of stars) ---*/ /*--- signature: purpose: tests: ---*/ /*--- test the functions above ---*/ int main() { cout << boolalpha; cout << "*** Testing: count_upper ***" << endl; cout << (count_upper("") == 0) << endl; cout << (count_upper("moo baa") == 0) << endl; cout << (count_upper("Hello, HOW are YOU?") == 7) << endl; cout << "*** Testing: repeat_str ***" << endl; // copy each of repeat_str's test expressions into a // cout statement to print its result // cout << () << endl; cout << "*** Testing: starline ***" << endl; // for EACH starline test, // write a cout DESCRIBING what side-effects should be for that // test, // THEN copy its test expression into a second cout statement to // print its result // cout << "Should see ..., then true:" << endl; // cout << () << endl; return EXIT_SUCCESS; } /*--- Remember: once you have compiled and run these and are satisfied with them, * DOWNLOAD copies of this file lab13.cpp AND your example output file lab13-out.txt, and use Gmail to E-MAIL copies of BOTH of these files to BOTH of you. * BOTH of you should submit your files *** lab13.cpp AND lab13-out.txt *** to Canvas BEFORE you leave lab. * ALSO answer the "Week 13 Lab Exercise - Pair-Programming Peer Review Survey" in Canvas, posted along with this lab exercise, by 11:59 pm TONIGHT (Friday, November 21) ---*/