/*--- CS 111 - Week 13 Lecture 1 - 2024-11-19 by: Sharon Tuttle last modified: 2024-11-19 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*=== signature: cheer: int -> int purpose: expects the number of HIPs desired in a cheer, has the SIDE-EFFECT of printing THAT many HIP cheers to the screen, each on its own line, followed by HOORAY!, and then it returns how many HIPs it actually printed. (If called with a negative number, it just prints HOORAY! and returns 0) tests: cheer(3) == 3 and also has the side-effect of printing to the screen: HIP HIP HIP HOORAY! cheer(5) == 5 and also has the side-effect of printing to the screen: HIP HIP HIP HIP HIP HOORAY! cheer(0) == 0 and also has the side-effect of printing to the screen HOORAY! cheer(-5) == 0 and also has the side-effect of printing to the screen HOORAY! ***/ int cheer(int num_hips) { // set up a local variable to keep track of how many HIPs // have been printed so far int count = 0; // repeatedly print HIPs until you print the desired number // of them (and keep a count of the number printed) while (count < num_hips) { cout << "HIP" << endl; count = count + 1; } // print a final HOORAY! after all of those HIPs and // return the number of HIPs actually printed cout << "HOORAY!" << endl; return count; } /*** signature: sum_ints: int -> int purpose: expects a positive integer, and returns the sum of the integers from 1 to (and including) thart positive integer. (if given 0 or a negative integer, it returns 0) tests: sum_ints(4) == (1 + 2 + 3 + 4) sum_ints(2) == (1 + 2) sum_ints(0) == 0 sum_ints(-5) == 0 ***/ int sum_ints(int up_to_val) { // want to start summing at 1 int next_num = 1; // want to keep track of the sum-so-far along the way int sum_so_far = 0; // repeatedly add the next number up to desired number while (next_num <= up_to_val) { sum_so_far = sum_so_far + next_num; next_num = next_num + 1; } // when done adding those numbers, return the resulting sum return sum_so_far; } /*--- test the functions above ---*/ int main() { cout << boolalpha; cout << endl; cout << "*** Testing: cheer ***" << endl; // I am going to describe what the user should // see (what side-effects) IF this function // is working properly cout << endl; cout << "You should see 3 HIPs then a HOORAY!, " << endl << " followed by true: " << endl; cout << (cheer(3) == 3) << endl; cout << endl; cout << "You should see 5 HIPs then a HOORAY!, " << endl << " followed by true: " << endl; cout << (cheer(5) == 5) << endl; cout << endl; cout << "You should see 0 HIPs then a HOORAY!, " << endl << " followed by true: " << endl; cout << (cheer(0) == 0) << endl; cout << endl; cout << "You should see 0 HIPs then a HOORAY!, " << endl << " followed by true: " << endl; cout << (cheer(-5) == 0) << endl; cout << endl; cout << "*** Testing: sum_ints ***" << endl; cout << (sum_ints(4) == (1 + 2 + 3 + 4)) << endl; cout << (sum_ints(2) == (1 + 2)) << endl; cout << (sum_ints(0) == 0) << endl; cout << (sum_ints(-5) == 0) << endl; cout << endl; cout << "just for fun: what IS the sum from 1 to 17?" << endl; cout << sum_ints(17) << endl; cout << endl; return EXIT_SUCCESS; }