/*--- Examples for CS 111 - Week 10 - Lecture 1 by: Sharon Tuttle last modified: 2026-10-27 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*=== signature: abs_value: double -> double purpose: expects a 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 num) { if (num >= 0) { return num; } else { return num * -1; } } /*=== signature: next_sound: string -> string purpose: expects an animal sound as a string, ; and returns the next sequential animal sound ; based on the beginning of Sandra Boynton's ; "Moo, Baa, La La La" as follows: ; * for "moo", it returns "baa" ; * for "baa", it returns "la la la" ; * for "la la la", it returns "oink" ; * for "oink", it returns "moo" ; * for any other sound, it returns "???" tests: next_sound("moo") == "baa" next_sound("baa") == "la la la" next_sound("la la la") == "oink" next_sound("oink") == "moo" next_sound("rarrgh") == "???" ===*/ string next_sound(string farm_sound) { if (farm_sound == "moo") { return "baa"; } else if (farm_sound == "baa") { return "la la la"; } else if (farm_sound == "la la la") { return "oink"; } else if (farm_sound == "oink") { return "moo"; } else { return "???"; } } /*=== signature: porridge_pref: double -> string purpose: expects a porridge temp as a double and returns one of these three options; * if temp is less than 50, it is "mama-approved" * if temp is greater than 80, it is "papa-approved" * if the temperature is in [50, 80], it is "baby-approved" tests: porridge_pref(45) == "mama-approved" porridge_pref(50) == "baby-approved" porridge_pref(55) == "baby-approved" porridge_pref(80) == "baby-approved" porridge_pref(85) == "papa-approved" ===*/ string porridge_pref(double temp) { if (temp < 50) { return "mama-approved"; } else if (temp > 80) { return "papa-approved"; } /*else if ((temp >= 50) && (temp <= 80))*/ else { return "baby-approved"; } } /*--- test the functions above ---*/ int main() { cout << boolalpha; 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("rarrgh") == "???") << endl; cout << "Just for fun: " << next_sound("moo") << endl; cout << "Testing: porridge_pref" << endl; cout << (porridge_pref(45) == "mama-approved") << endl; cout << (porridge_pref(50) == "baby-approved") << endl; cout << (porridge_pref(55) == "baby-approved") << endl; cout << (porridge_pref(80) == "baby-approved") << endl; cout << (porridge_pref(85) == "papa-approved") << endl; // cout << () << endl; return EXIT_SUCCESS; }