Untitled

 avatar
unknown
c_cpp
a year ago
877 B
5
Indexable
#include <stdio.h>

unsigned int theBiggestOddIndex( const unsigned int n, const int* tab )
{
    int maxOdd = -1;
    unsigned maxOddIndex = -1;

    for( unsigned int i = 0; i < n; ++i )
    {
        if( *( tab ) % 2 != 0 )
        {
            if( maxOdd == -1 || *( tab ) >= *( tab + maxOddIndex ) )
            {
                maxOdd = *( tab + i );
                maxOddIndex = i;
            }
        }
        ++tab;
    }

    return maxOddIndex;
}

int main()
{
    const int n = 7;
    const int tab[] = { -4, -1, -7, 4, 20, -1, 3 };

    unsigned int result = theBiggestOddIndex( n, tab );
    if( result != -1 )
    {
        printf( "Indeks najwiekszego nieparzystego elementu w tablicy: %d\n", result );
    }
    else
    {
        printf( "Tablica nie zawiera elementow nieparzystych.\n" );
    }

    return 0;
}
Editor is loading...
Leave a Comment