AGENDA: --> write the functions below --> new function-related topic --> test #2 prep --> lab catch-up (1) Write a function called sum_1_to_n() that returns the sum of integers from 1 to the given value n. (2) Write a function called sum_m_to_n() that returns the sum of integers from m to n. (3) Write a function called m_to_the_n() that returns the value m raised to the nth power. Do NOT use the pow() function -- write your own! (4) Write a function called factorial() that returns the factorial n! of given integer value n. (n! is n * (n-1) * (n-2) * ... * 1) -------------------------------- sum_m_to_n(): | m: 3 i: 4 etc. 7 | | n: 6 | | sum: 3 etc. 3+4+5+6==>18 | -------------------------------- -------------------------------- main(): | m: 3 | | n: 6 | | sum: 18 | -------------------------------- RECURSION: Write a function to calculate n! (n-factorial) recursive definition of n! is below: / | 1 if n == 0 n! = < | n * (n - 1)! if n > 0 \ factorial_using_recursion(): -------------------------------- | n: 4 | ^ -------------------------------- | factorial_using_recursion(): -------------------------------- | n: 5 | ^ -------------------------------- | factorial_using_recursion(): -------------------------------- | n: 6 | -------------------------------- ^ | -------------------------------- main(): | x: 6 | | result: 720 | -------------------------------- factorial_using_recursion(): -------------------------------- | n: 0 return 1; | ^ -------------------------------- | factorial_using_recursion(): -------------------------------- | n: 1 return 1 * 0! | ^ -------------------------------- | factorial_using_recursion(): -------------------------------- | n: 2 return 2 * 1! | -------------------------------- ^ | -------------------------------- main(): | x: 2 | | result: ___ | -------------------------------- Write an integer power function: / p | 1 p == 0 n = < p-1 | n * n p > 0 \ double n_to_the_p( double n, int p ) { if ( p == 0 ) { return 1; } else { // p > 0 return n * n_to_the_p( n, p - 1 ); } } Recursion ------------------ Rules for writing recursive functions: (1) Handle the BASE CASE(S) first. This is your stopping point (e.g. when n == 0 or when size is 1) (2) Break the problem down into smaller and smaller pieces. (3) What needs to be done BEFORE calling the recursive function? (4) What needs to be done AFTER? (5) Make sure each recursive call progresses toward the base case. TEST TWO ------------------------------ OPEN BOOK, OPEN NOTES, NO COMPUTER, NO CALCULATOR Questions: 2 (1) What is the exact output of the given code? (output will be finite!) 2 (2) Circle the errors in the given code ---> then fix the errors (in the margins...) Error types include compiler errors, as well as runtime errors 2 (3) Write some function(s) to do _______________. 1 (4) Write a full program to do _______________. [10am-11:50am] 7 questions (do not include recursion) TOPICS: variables, data types, loops cin, cout, cout.precision() arrays functions variable scope files (ifstream and ofstream)