#include #include // Recall that the largest entry of an array, or // the first-rank entry, can be found by a function // that returns an index of that largest entry. // // If the capacity is zero, there is no largest // entry, so capacity is returned. // // We now want to use this function to implement a // function that returns the index of the second-rank // entry: the largest entry that is strictly less // than the first-rank entry. // // If there is no second-rank entry, return capacity. // This occurs if the array has fewer than two // distinct values. // // We describe this as using two loops because one // loop is hidden in the call to array_1st_rank(...), // while a second loop is used in array_2nd_rank(...). // // In the next problem, we will find both ranks // using only one loop. // Function declarations int main(); std::size_t array_1st_rank( double const array[], std::size_t const capacity ); std::size_t array_2nd_rank( double const array[], std::size_t const capacity ); // Function definitions int main() { double array1[5]{ 3.2, 1.5, 4.9, 8.1, 2.4 }; double array2[7]{ 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5 }; double array3[6]{ 1.2, 4.5, 4.5, 4.5, 4.5, 4.5 }; double array4[6]{ 1.2, 4.5, 4.5, 4.5, 4.5, 1.3 }; double array5[1]{ 1.2 }; double array6[7]{ 4.5, 1.5, 4.5, 1.5, 4.5, 1.5, 4.5 }; std::cout << array_2nd_rank( array1, 5 ) << " == 2" << std::endl; std::cout << array_2nd_rank( array2, 7 ) << " == 7" << std::endl; std::cout << array_2nd_rank( array3, 6 ) << " == 0" << std::endl; std::cout << array_2nd_rank( array4, 6 ) << " == 5" << std::endl; std::cout << array_2nd_rank( array5, 1 ) << " == 1" << std::endl; std::cout << array_2nd_rank( array6, 7 ) << " == 1" << std::endl; return 0; } /*************************************************** * array_1st_rank * * Return the index of the largest entry in the * array. If the array is empty, return capacity. ***************************************************/ std::size_t array_1st_rank( double const array[], std::size_t const capacity ) { if ( capacity == 0 ) { return capacity; } std::size_t rank_1_idx{ 0 }; for ( std::size_t k{ 1 }; k < capacity; ++k ) { if ( array[k] > array[rank_1_idx] ) { rank_1_idx = k; } } return rank_1_idx; } /*************************************************** * array_2nd_rank * * Return the index of the largest entry that is * strictly less than the largest entry. * * Thus, repeated occurrences of the largest value * do not count as second-rank entries. * * If there is no second-rank entry, return * capacity. ***************************************************/ std::size_t array_2nd_rank( double const array[], std::size_t const capacity ) { std::size_t const rank_1_idx{ array_1st_rank( array, capacity ) }; std::size_t rank_2_idx{ capacity }; // Find the index of the second-rank unique // entry. // // If there is no such entry, return capacity. return rank_2_idx; } // Hint: // // First find the index of the first-rank entry: // // rank_1_idx // // If the array is empty, there cannot be a // second-rank entry. // // Otherwise, array[rank_1_idx] is the largest value // in the array. // // Now go through the array again, looking only at // entries that are strictly less than // // array[rank_1_idx]. // // Among those entries, find the largest. // // Initially, no second-rank entry has been found, // so use // // rank_2_idx = capacity // // to indicate this. // // When considering array[k], it should become the // new second-rank entry if // // 1. it is less than the first-rank entry; and // // 2. either no second-rank entry has yet been // found, or array[k] is greater than the // current second-rank entry. // Here is one solution: /* std::size_t array_2nd_rank( double const array[], std::size_t const capacity ) { std::size_t const rank_1_idx{ array_1st_rank( array, capacity ) }; if ( rank_1_idx == capacity ) { return capacity; } std::size_t rank_2_idx{ capacity }; for ( std::size_t k{ 0 }; k < capacity; ++k ) { if ( (array[k] < array[rank_1_idx]) && ((rank_2_idx == capacity) || (array[k] > array[rank_2_idx])) ) { rank_2_idx = k; } } return rank_2_idx; } */