#include #include /*************************************************** * Sum and product of a local array * * Write two functions: * * sum(...) * product(...) * * that calculate the sum and product, respectively, * of the entries in an array. * * The functions are given: * * - the array; and * - the capacity of the array. * * Because neither function should modify the * entries of the array, the array parameter is * declared const. ***************************************************/ // Function declarations int main(); int sum( int const array[], std::size_t const capacity ); int product( int const array[], std::size_t const capacity ); // Function definitions int main() { int array1[1]{ 42 }; int array2[5]{ 34, 15, 65, 59, 42 }; int array3[27]{ 50, 65, 23, 46, 57, 3, 29, 22, 99, 73, 44, 2, 48, 88, 98, 38, 17, 46, 19, 34, 93, 88, 46, 52, 69, 38, 79 }; // An array with no entries has sum 0. std::cout << sum( nullptr, 0 ) << " = 0" << std::endl; std::cout << sum( array1, 1 ) << " = 42" << std::endl; std::cout << sum( array2, 5 ) << " = 215" << std::endl; std::cout << sum( array3, 27 ) << " = 1366" << std::endl; std::cout << std::endl; // An array with no entries has product 1. std::cout << product( nullptr, 0 ) << " = 1" << std::endl; std::cout << product( array1, 1 ) << " = 42" << std::endl; std::cout << product( array2, 5 ) << " = 82134900" << std::endl; // What happens here? // // Is the actual product of these 27 integers // representable by an int? std::cout << product( array3, 27 ) << std::endl; return 0; } /*************************************************** * sum * * Return the sum of the entries of the array. ***************************************************/ int sum( int const array[], std::size_t const capacity ) { return 0; } /*************************************************** * product * * Return the product of the entries of the array. ***************************************************/ int product( int const array[], std::size_t const capacity ) { return 1; } // Hints: // // You will need a for loop that steps through the // array. Because capacity has type std::size_t, the // loop variable should also have type std::size_t. // // For sum(...), use a local variable that stores the // running sum. // // For product(...), use a local variable that stores // the running product. // // // Question: // // What should be the sum of an array containing no // entries? // // What should be the product of an array containing // no entries? // // // Question: // // Why can product(...) not correctly calculate the // product of all 27 entries of array3? // // Think about the largest value that can be stored // in an int. // // The entries in array3 have a geometric mean of // approximately 38. Thus, the product is roughly // // 38^27. // // Since // // log2(38) is approximately 5.25, // // we have // // 38^27 // // = 2^(27 log2(38)) // // is approximately 2^142. // // This is enormously larger than the largest value // that can normally be stored in a 32-bit int, // which is // // 2^31 - 1. // // Thus, even though every individual entry easily // fits into an int, their product does not. // Why is the product of no entries equal to 1? // // Consider // // 2*3*4*5*6*7 // // = (2*3*4*5*6)*7. // // More generally, the product of n objects is the // product of the first n - 1 objects multiplied by // the final object. // // Now consider a product containing just one value: // // 7 = (product of no entries)*7. // // Therefore, the product of no entries must be 1. // // This also agrees with the fact that 1 is the // multiplicative identity. // // Similarly, the sum of no entries is 0 because 0 // is the additive identity. // One solution: /* int sum( int const array[], std::size_t const capacity ) { int running_sum{ 0 }; for ( std::size_t k{ 0 }; k < capacity; ++k ) { running_sum += array[k]; } return running_sum; } int product( int const array[], std::size_t const capacity ) { int running_product{ 1 }; for ( std::size_t k{ 0 }; k < capacity; ++k ) { running_product *= array[k]; } return running_product; } */