#include #include // Function declarations unsigned int max_common( unsigned int const array1[], std::size_t const capacity1, unsigned int const array2[], std::size_t const capacity2 ); int main(); // Function definitions int main() { unsigned int array2[5]{ 25, 15, 31, 32, 8 }; unsigned int array3[10]{ 34, 7, 24, 20, 10, 21, 3, 31, 13, 27 }; unsigned int array4[20]{ 15, 2, 25, 27, 33, 23, 8, 12, 10, 8, 21, 26, 19, 5, 21, 18, 22, 17, 22, 11 }; // This should create the symmetric table // // 0 0 0 0 // 0 32 31 25 // 0 31 34 27 // 0 25 27 33 std::cout << max_common( nullptr, 0, nullptr, 0 ) << "\t" << max_common( nullptr, 0, array2, 5 ) << "\t" << max_common( nullptr, 0, array3, 10 ) << "\t" << max_common( nullptr, 0, array4, 20 ) << std::endl; std::cout << max_common( array2, 5, nullptr, 0 ) << "\t" << max_common( array2, 5, array2, 5 ) << "\t" << max_common( array2, 5, array3, 10 ) << "\t" << max_common( array2, 5, array4, 20 ) << std::endl; std::cout << max_common( array3, 10, nullptr, 0 ) << "\t" << max_common( array3, 10, array2, 5 ) << "\t" << max_common( array3, 10, array3, 10 ) << "\t" << max_common( array3, 10, array4, 20 ) << std::endl; std::cout << max_common( array4, 20, nullptr, 0 ) << "\t" << max_common( array4, 20, array2, 5 ) << "\t" << max_common( array4, 20, array3, 10 ) << "\t" << max_common( array4, 20, array4, 20 ) << std::endl; return 0; } /*************************************************** * max_common * * Return the greatest value that occurs in both * arrays. * * If there is no common entry, return 0, the * smallest possible unsigned integer. ***************************************************/ unsigned int max_common( unsigned int const array1[], std::size_t const capacity1, unsigned int const array2[], std::size_t const capacity2 ) { return 0; } // Hint: // // This should be relatively straightforward. // // Use two nested for loops. For each entry in // array1, compare it with each entry in array2. // // Whenever you find two equal entries, you have // found a common entry. If that common entry is // greater than the greatest common entry found so // far, remember it. // // Begin with // // unsigned int max_entry{ 0 }; // // because 0 is the smallest possible unsigned // integer. // // If no common entry is ever found, max_entry will // remain 0. // One solution: /* unsigned int max_common( unsigned int const array1[], std::size_t const capacity1, unsigned int const array2[], std::size_t const capacity2 ) { unsigned int max_entry{ 0 }; for ( std::size_t k1{ 0 }; k1 < capacity1; ++k1 ) { for ( std::size_t k2{ 0 }; k2 < capacity2; ++k2 ) { if ( (array1[k1] == array2[k2]) && (array1[k1] > max_entry) ) { max_entry = array1[k1]; } } } return max_entry; } */