isODDHigh Function

Write your question here.

Please I need urgent clue on this:

Write a method named, isOddHigh, that takes as input, any array of integers and it returns true, if
every odd number in the array is greater than all even numbers in the array, otherwise it returns false.
e.g. isOddHigh([9, -6, 2, 5, 4, 7]) will return true; whereas, isOddHigh([7, 2, 5, 8]) and isOddHigh([9, 4, 7,
5, 8]) should return false since 5 is not greater than 8. Also, when isOddHigh(...) is called and the input
argument passed is null or an empty array or an array with no odd number, it should return, false.


break it down.

step 0: looks like they jacked in some extra requirements at the end that really went first. Return false here if any of the special conditions are found. If not, proceed.

if every odd > all even, then it must be true:
every odd > largest even.
so that begs: step 1: find largest even.
and
step2: loop, check to see if all odd > largest even.

return the answer to step 2.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <climits>

// return the largest even number in the array
// return INT_MIN if the array is empty or if there are no even numbers
int largest_even_number( const int array[], std::size_t num_elements )
{
    // https://en.cppreference.com/w/cpp/header/climits
    int largest = INT_MIN ; // smallest possible value that an int can have

    for( std::size_t i = 0 ; i < num_elements ; ++i ) // for each position in the array
    {
        const int number = array[i] ; // get the number at position i

        if( number%2 == 0 ) // if it is an even number
        {
            // if it is greater than the largest seen so far, update largest
            if( number > largest ) largest = number ;
        }
    }

    return largest ;
}

// return the smallest odd number in the array
// return INT_MAX if the array is empty or if there are no odd numbers
int smallest_odd_number( const int array[], std::size_t num_elements ) ;
// TO DO: implement this function

// return true if  if every odd number in the array is greater than all even numbers in the array
bool isOddHigh( const int array[], std::size_t num_elements ) ;
// TO DO: implement this function
// hint: return false if array == nullptr or if num_elements == 0
// hint: return true if the smallest odd number in the array is greater than
//       the largest even number in the array; return false otherwise 
Topic archived. No new replies allowed.