/*--- CS 111 - Week 10 Lecture 1 - 2025-10-28 compile using: g++ 111lect10-1.cpp -o 111lect10-1 run using: ./111lect10-1 by: Sharon Tuttle last modified: 2025-10-28 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*=== someone wants a function that will give them the number of characters combined in someone's first and last names!! ===*/ /*=== signature: name_length: string string -> int purpose: expects a first name and a last name, and returns the total length of that "full" name (the total number of characters in the first name AND the last name) tests: name_length("Sally", "Rafael") == 11 name_length("John", "Doe") == 7 ===*/ int name_length(string f_name, string l_name) { return f_name.length() + l_name.length(); } /*=== signature: abs_value: double -> double purpose: expects any number, returns the absolute value of that number, as so: abs_value(x) = x if x >= 0 -x if x < 0 <---------|------------> 0 [-----> <-----) tests: abs_value(-138) == 138 abs_value(0) == 0 abs_value(2452) == 2452 ===*/ double abs_value(double a_num) { if (a_num >= 0) { return a_num; } else { return (0 - a_num); } } /*=== signature: next_sound: string -> string purpose: expects an animal sound, and returns the "next" animal sound based on the beginning of Sandra Boynton's "Moo, Baa, La La La": * for moo, return baa * for baa, return la la la * for la la la, return oink * for oink, return moo * for anything else, return "???" tests: next_sound("moo") == "baa" next_sound("baa") == "la la la" next_sound("la la la") == "oink" next_sound("oink") == "moo" next_sound("fred") == "???" ===*/ string next_sound(string animal_sound) { if (animal_sound == "moo") { return "baa"; } else if (animal_sound == "baa") { return "la la la"; } else if (animal_sound == "la la la") { return "oink"; } else if (animal_sound == "oink") { return "moo"; } else { return "???"; } } /*--- demo some string methods, demo some of cout's default formats for different C++ types, and test the functions above ---*/ int main() { cout << boolalpha; const string FIRST_COURSE = "CS 111"; cout << FIRST_COURSE.length() << endl; cout << (FIRST_COURSE.at(0) == 'C') << endl; // but remember that cout's default display style // for a char is NOT to print the single quotes cout << FIRST_COURSE.at(0) << endl; // displays C // and demo'ing how cout's default display style for // double has its quirks as well: cout << 1.234567890 << endl; // displays 1.23457 cout << 4.0 << endl; // displays 4 cout << endl; cout << "*** Testing: name_length ***" << endl; cout << (name_length("Sally", "Rafael") == 11) << endl; cout << (name_length("John", "Doe") == 7) << endl; cout << "*** Testing: abs_value ***" << endl; cout << (abs_value(-138) == 138) << endl; cout << (abs_value(0) == 0) << endl; cout << (abs_value(2452) == 2452) << endl; cout << "*** Testing: next_sound ***" << endl; cout << (next_sound("moo") == "baa") << endl; cout << (next_sound("baa") == "la la la") << endl; cout << (next_sound("la la la") == "oink") << endl; cout << (next_sound("oink") == "moo") << endl; cout << (next_sound("fred") == "???") << endl; return EXIT_SUCCESS; }