/*======== Fall 2024 - CS 111 Week 13 Lab Exercise date: 2024-11-22 ========*/ /*--- USING pair-programming * COPY and PASTE the contents of this file into a lab13.cpp file within the CS50 IDE * ADD the parts asked for below (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.cpp 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 ---*/ /*--- by: PUT BOTH of YOUR NAMES HERE last modified: 2024-11-22 ---*/ #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 - Lecture 1's posted example functions cheer and sum_ints, 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) * 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 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; /*--- WEEK 13 LAB EXERCISE - PROBLEM 4 Purpose: to get some initial practice with a C++ array We started discussing arrays in class yesterday (Week 13 Lecture 2). After class, I added an array example to the end of the main function in 111lect13-2.cpp. Look over this now-posted example. * DECIDE: * what TYPE of elements would you like in an array? * what is a small (4 or more) set of values of that type you would like to store in an array? * what would be a reasonably descriptive name for this array? Declare an array of your chosen type, able to hold at least 4 elements, with a descriptive name of your choice. ---*/ /*--- Write statements initializing the contents of your array. (This might be a set of assignment statements, or it might be a set of statements that includes a while loop, your choice!) ---*/ /*--- Print the contents of your resulting array to the screen, one array element's value per line. (This might be a set of cout statements, or it might be a set of statements that includes a while loop, your choice!) ---*/ cout << endl; cout << "Array's contents: " << endl; cout << "==================" << endl; return EXIT_SUCCESS; }