简体   繁体   中英

odd or even number using pointer in C

I have an array, the elements inside this array should be assigned randomly then I create two more arrays one for odd and one for even numbers using pointers I want to put the odd numbers inside the odd array, and the even numbers in the even one. how can I assign it using pointers?

I came up with this:

int main()
{
    int array[10];
    int *arrayPtr = &array;

    int arrayEven[10];
    int *pEven = &arrayEven;

    int arrayOdd[10];
    int * pOdd = &arrayOdd;

    int i, j;

    for(i=0; i<10; i++)
        {

        array[i] =rand()%100 ;




        if(array[i] %2 ==0)
        {
            printf("%d\n", array[i]);
            printf("Even number");
        }
        else
        {
            printf("%d\n", array[i]);
            printf("Odd number");

        }

    }

These declarations of pointers

int *arrayPtr = &array;
int *pEven = &arrayEven;
int * pOdd = &arrayOdd;

are incorrect. The initializers have the type int( * )[10] according to the declarations of the arrays as for example of the variable array

int array[10];

while the declared pointers has incompatible type int * .

What you need is declarations like this

int *arrayPtr = array;

There is no great sense to define arrays of the same size as the source array because for example the source array can have no odd or even numbers. You should deal with arrays depending on the number of odd and even numbers in the source array. If your compiler supports variable length arrays then you can use them. Or you should dynamically allocate arrays.

Here is a demonstrative program that shows how the assignment can be done using variable length arrays and pointers.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void copy_if( const int a1[], size_t n, int *a2, int *a3, int predicate( int ) )
{
    for ( const int *p = a1; p != a1 + n; ++p )
    {
        if ( predicate( *p ) )
        {
            *a2++ = *p;
        }
        else
        {
            *a3++ = *p;
        }
    }
}

size_t count_if( const int a[], size_t n, int predicate( int ) )
{
    size_t  cnt = 0;

    for ( const int *p = a; p != a + n; ++p )
    {
        if ( predicate( *p ) ) ++cnt;
    }

    return cnt;
}

int is_even( int x )
{
    return x % 2 == 0;
}

int main(void) 
{
    enum { N = 10 };
    int a1[N];

    srand( ( unsigned int )time( NULL ) );

    for ( int *p = a1; p != a1 + N; ++p )
    {
        *p = rand() % N;
    }

    for ( const int *p = a1; p != a1 + N; ++p )
    {
        printf( "%d ", *p );
    }

    putchar( '\n' );

    size_t cnt = count_if( a1, N, is_even );

    if ( cnt != 0 && cnt != N )
    {
        int a2[cnt];
        int a3[N-cnt];

        copy_if( a1, N, a2, a3, is_even );

        for ( const int *p = a2; p != a2 + cnt; ++p )
        {
            printf( "%d ", *p );
        }

        putchar( '\n' );

        for ( const int *p = a3; p != a3 + N - cnt; ++p )
        {
            printf( "%d ", *p );
        }

        putchar( '\n' );
    }

    return 0;
}

The program output might look like

6 3 3 8 3 0 5 5 3 2 
6 8 0 2 
3 3 3 5 5 3 

If to use your straightforward approach then the program can look the following way

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) 
{
    enum { N = 10 };
    int array[N];
    int *arrayPtr = array;

    srand( ( unsigned int )time( NULL ) );

    for ( int *p = arrayPtr; p != arrayPtr + N; ++p )
    {
        *p = rand() % N;
    }

    for ( const int *p = arrayPtr; p != arrayPtr + N; ++p )
    {
        printf( "%d ", *p );
    }

    putchar( '\n' );

    int arrayEven[10];
    int *pEven = arrayEven;

    int arrayOdd[10];   
    int *pOdd = arrayOdd;

    int *pEvenEnd = pEven;
    int *pOddEnd  = pOdd;

    for ( const int *p = array; p != arrayPtr + N; ++p )
    {
        if ( *p % 2 == 0 )
        {
            *pEvenEnd++ = *p;
        }
        else
        {
            *pOddEnd++ = *p;
        }
    }

    for ( const int *p = pEven; p != pEvenEnd; ++p )
    {
        printf( "%d ", *p );
    }

    putchar( '\n' );

    for ( const int *p = pOdd; p != pOddEnd; ++p )
    {
        printf( "%d ", *p );
    }

    putchar( '\n' );

    return 0;
}

Its output might look for example like

1 0 0 3 4 5 2 1 9 5 
0 0 4 2 
1 3 5 1 9 5 

For it to work you need two additional coutner variables

int oddCount, evenCount;

these need to be initialized to be 0 (ie how many odd/even number you have inserted so far). Then while iterating your first array you increment the respective coutner if you encounter an even/odd number. Also you can just use these variables to know where you need to put the next even/odd number in their respective array.

You can use binary comparison since all odd finish by 1 in binary mode:

If( a & 1)
//Odd
Else
//Even

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM