c ++ functions


i want to write a function that would get a copy of an array and go through it to see which of the below conditions is satisfied by that array. function prototype is this : unsigned g( const unsigned a[], unsigned elements ) . g returns a code in the range [0, 8) according to the data in a

0: elements < 2, so no element has a predecessor.
e.g. {}
e.g. {1234}
1: elements >= 2, and every element is < its predecessor.
e.g. {10, 9, 2}
2: elements >= 2, and all the elements are equal.
e.g. {8, 8, 8, 8}
3: at least one element is < its predecessor, at least one element is == its predecessor, but no element is > its predecessor.
e.g. {10, 10, 8, 8, 5, 5, 5, 5, 3}
4: elements >= 2, and every element is > its predecessor.
e.g. {2, 4, 8, 16}
5: at least one element is < its predecessor, at least one element is > its predecessor, but no element is == its predecessor.
e.g. {3, 2, 3, 4}
6: at least one element is == its predecessor, at least one element is > its predecessor, but no elements is < its predecessor.
e.g. {5, 5, 10, 20}
7: at least one element is < its predecessor, at least one element is == its predecessor, and at least one element is > its predecessor.
e.g. {5, 4, 5, 5}
If I haven’t made a mistake, you should be able to convince yourself that every array will fall into exactly one of these 8 categories. g’s job is to return the number of that category.
For instance, g( {5, 4, 5, 5} should return 7


Comments: i am confused and don't know how to proceed, i would appreciate it if anyone explain where should i start.
@parham93
i want to write a function that would get a copy of an array and go through it to see which of the below conditions is satisfied


If you want it then do it!:)
This will print out the counts of how many elements are less than, equal to or greater than their immediate predecessor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    const unsigned int a[] = { 10, 10, 8, 8, 5, 5, 5, 5, 3, 1 } ;
    const unsigned int elements = sizeof(a) / sizeof(*a) ;

    int lt = 0, eq = 0, gt = 0 ;
    for( unsigned int pred = 0 ; pred < (elements-1) ; ++pred )
    {
        const unsigned int succ = pred+1 ;
        if( a[succ] < a[pred] ) ++lt ;
        else if( a[succ] == a[pred] ) ++eq ;
        else ++gt ;
    }

    std::cout << lt << " elements are less than their immediate predecessor\n"
              << eq << " elements are equal to their immediate predecessor\n"
              << gt << " elements are greater than their immediate predecessor\n" ;
}


Take it up from there.
The same as above but using standard algorithms. Also I changed the original array

int a[] = { 10, 10, 8, 8, 5, 5, 5, 5, 1, 3 } ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <numeric>
#include <iterator>

int main()
{
    int a[] = { 10, 10, 8, 8, 5, 5, 5, 5, 1, 3 } ;
    enum { LT, EQ, GT };
    unsigned int count[3] = {};
    
    std::inner_product( std::begin( a ), std::prev( std::end( a ) ), std::next( std::begin( a ) ), count,
                        []( unsigned int *p, unsigned int i ) { return ( ++p[i], p ); },
                        []( int x, int y ) { return ( x < y ? LT : ( x == y ? EQ : GT ) ); } );
                        
    std::cout << "less than " << count[LT] << ", equal to " << count[EQ] << ", greater than " << count[GT] << std::endl;
    
    return 0;
    
}


The output is

less than 1, equal to 5, greater than 3


If the code is not compiled with MS VC++ then remove the enum and substitute its enumerators for magic numbers 0, 1, and 2 correspondingly.:)
Last edited on
@parham93


So taking into account the previous code you can easy write the general function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
unsigned g( const unsigned a[], unsigned elements )
{
    if ( elements < 2 ) return ( 0 );

    enum { LT, EQ, GT };
    unsigned int count[3] = {};
    
    std::inner_product( a, a + elements - 1, a + 1, count,
                        []( unsigned int *p, unsigned int i ) { return ( ++p[i], p ); },
                        []( int x, int y ) { return ( x < y ? LT : ( x == y ? EQ : GT ) ); } );

   if ( count[LT] == elements - 1 ) return ( 1 );
   else if ( count[EQ] == elements - 1 ) return ( 2 );
   else if ( count[LT] && count[EQ] && !count[GT] ) return ( 3 );
   // other if-else
   else if ( count[LT] && count[EQ] && count[GT] ) return ( 7 );
   else throw ( std::logic_error( "Unpredictable result." ) );
}
Last edited on
Topic archived. No new replies allowed.