/*======== Fall 2025 - CS 111 Week 10 Lab Exercise - save as lab10.cpp date: 2025-10-31 ========*/ /*--- USING pair-programming * COPY and PASTE the contents of this file into a file in the CS50 IDE named: lab10.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++ lab10.cpp -o lab10 * IF it compiles with no errors: to run: in that same CS50 terminal that is open to the folder CONTAINING this .cpp file, type: ./lab10 * When you are satisfied with its output, create an example output file by typing: ./lab10 > lab10-out.txt * Download copies of your resulting lab10.cpp and lab10-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, *** lab10.cpp AND lab10-out.txt *** to Canvas BEFORE you leave lab. * REMEMBER to also answer the "Week 10 Lab Exercise - Pair-Programming Peer Review Survey" in Canvas, posted along with this lab exercise, by 11:59 pm TONIGHT (Friday, October 31) ---*/ /*--- by: PUT BOTH of YOUR NAMES HERE last modified: 2025-10-31 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*--- REMINDERS for PROBLEM 1! --- The C++ string class also includes two substring methods named substr. For the 2-argument version: given a starting position and a length, it returns the string starting at that position in the calling string and going that many characters (or until the end of the string, whatever comes first). For the 1-argument version: given a starting position, it returns the string starting at that position in the calling string and going until the end of the string. So, for example, if you had a string parameter thingy, and you wanted the string that was the first 3 characters in thingy, you could use the expression: thingy.substr(0, 3) // start at position 0, grab 3 characters, // return the string of those 3 characters * so -- what is the difference between thingy.at(0) // grab the char at position 0 and thingy.substr(0, 1) // grab the string of length 1 starting // at pos 0 method at returns a char, but method substr returns a string! THAT IS, if thingy is, say, "moo", thingy.at(0) == 'm' thingy.substr(0, 1) == "m" ---*/ /*--- WEEK 10 LAB EXERCISE - PROBLEM 1 ---*/ /*--- Use the design recipe to design and write a C++ function short_name that expects a first name and a last name, and returns a shorter version of that full name made up of the first initial (the first letter of the first name), a period, a blank, and the last name. For example, short_name("Charlie", "Brown") would return "C. Brown". * (IF you want: in the main function, you can also include one or more cout statements that JUST include an example call of short_name AFTER its tests, so you can SEE the value those call(s) return) ---*/ /*--- signature: purpose: tests: ---*/ /*--- WEEK 10 LAB EXERCISE - PROBLEM 2 ---*/ /*--- The purpose of this problem is to make sure that you are familiar with the class indentation style for the chained if-else-if pattern. LOOK over Week 10 Lecture 1's posted NOTES, which compared the Racket cond expression to its roughly-analogous C++ if-else-if pattern equivalent: if (bool_expr1) { return result_expr1; } else if (bool_expr2) { return result_expr2; } ... else { return result_else; } The above DOES follow the CS 111 class style standards. BUT, the following function header and body for function try_it BELOW, while syntactically correct, does NOT follow class coding standards! So -- MODIFY the function below to FOLLOW the class style! (try_it is called 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! ---*/ /*--- IMPROVE the FORMATTING of this WORKING function to MEET CS 111 CLASS CODING STANDARDS ---*/ double try_it(double val1, double val2) { if (val1 < val2){ return val2 - val1;} else if (val2 < val1){ return val1 - val2; }else {return val1 + val2;}} /*--- WEEK 10 LAB EXERCISE - PROBLEM 3 ---*/ /*--- In the Week 4 Lab Exercise, you designed a Racket function size->quant that expected the *name* of one of these drink sizes: size name: "short" "tall" "grande" "venti" "trenta" # ounces: 8 12 16 20 31 ...and returned just the *number* of ounces you get for that drink size. (that is, for example, you call it with "short", it returns just 8) And it returned a drink size of 0 if the drink name given was NOT equal to one of these. Use the design recipe to design a C++ version of this function named size_to_quant. * for full credit, appropriately use the if-else-if pattern in this function * be careful -- at least how many tests are needed for this function? * (IF you want: in the main function, you can also include one or more cout statements that JUST include an example call of size_to_quant AFTER its tests, so you can SEE the value those call(s) return) ---*/ /*--- signature: purpose: tests: ---*/ /*--- WEEK 10 LAB EXERCISE - PROBLEM 4 ---*/ /*--- In the Week 4 Lab Exercise, you designed a Racket function judge-carbs that expected the number of carbohydrates some food has in a serving, and, based on the following: * Someone has decided that they want to judge a food as low-carbohydrate if if has less than or equal to 10 grams of carbohydrates per serving. * And, if > 10 grams and <= 20 grams, they want to judge it as moderate-carbohydrate, * and if > 20 grams, they want to judge it as high-carbohydrate. ...it returned whether, for this person, they would judge that number of carbohydrates as "lo-carb", "mod-carb", or "hi-carb". Use the design recipe to design a C++ version of this function named judge_carbs. * for full credit, appropriately use the if-else-if pattern in this function * be careful -- at least how many tests are needed for this function? * (IF you want: in the main function, you can also include one or more cout statements that JUST include an example call of judge_carbs AFTER its tests, so you can SEE the value those call(s) return) ---*/ /*--- signature: purpose: tests: ---*/ /*--- test the functions above ---*/ int main() { cout << boolalpha; cout << "*** Testing short_name ***" << endl; // copy each of short_name's test expressions into a // cout statement to print its result // (then REMOVE the // comment parts so you can RUN that cout!) // cout << () << endl; // cout << () << endl; cout << "*** Making sure Problem 2's function try_it runs ***" << endl; cout << "try_it: " << try_it(3.5, 1.2) << endl; cout << "*** Testing size_to_quant ***" << endl; // copy each of size_to_quant's test expressions into a // cout statement to print its result // (then REMOVE the // comment parts so you can RUN that cout!) // cout << () << endl; // cout << () << endl; // cout << () << endl; // cout << () << endl; // cout << () << endl; // cout << () << endl; cout << "*** Testing judge_carbs ***" << endl; // copy each of judge_carbs' test expressions into a // cout statement to print its result // (then REMOVE the // comment parts so you can RUN that cout!) // cout << () << endl; // cout << () << endl; // cout << () << endl; // cout << () << endl; // cout << () << endl; return EXIT_SUCCESS; } /*--- Remember: once you have compiled and run these and are satisfied with them, * DOWNLOAD copies of this file lab10.cpp AND your example output file lab10-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 *** lab10.cpp AND lab10-out.txt *** to Canvas BEFORE you leave lab. * ALSO answer the "Week 10 Lab Exercise - Pair-Programming Peer Review Survey" in Canvas, posted along with this lab exercise, by 11:59 pm TONIGHT (Friday, October 31) ---*/