/*--- CS 111 - Week 10 Lecture 1 - in-class examples by: Sharon Tuttle last modified: 2024-10-29 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*=== signature: name_length: string string -> int purpose: expects a first name and a last name, and returns the total length of the full name (first and last combined) tests: name_length("Jonathan", "Vex") == 11 name_length("Holly", "Dawn") == 9 ===*/ int name_length(string first_name, string last_name) { return first_name.length() + last_name.length(); } /*=== signature: abs_value: double -> double purpose: expects a number, and returns the absolute value of that number tests: abs_value(-5) == 5 abs_value(0) == 0 abs_value(3.75) == 3.75 ===*/ double abs_value(double num) { if (num < 0) return 0 - num; else return num; } /*--- test the functions above and also play around ---*/ int main() { cout << boolalpha; // trying out some string methods const string FIRST_COURSE = "CS 111"; cout << FIRST_COURSE.length() << endl; // cout's default formatting for char and // and char* and string is to JUST print the // content, not any quotes cout << FIRST_COURSE.at(0) << endl; cout << "MOO" << endl; cout << FIRST_COURSE << endl; cout << "*** Testing: name_length ***" << endl; cout << (name_length("Jonathan", "Vex") == 11) << endl; cout << (name_length("Holly", "Dawn") == 9) << endl; cout << "*** Testing: abs_value ***" << endl; cout << (abs_value(-5) == 5) << endl; cout << (abs_value(0) == 0) << endl; cout << (abs_value(3.75) == 3.75) << endl; // cout << () << endl; // cout << () << endl; return EXIT_SUCCESS; }