I have an array of integers.Let's assume int a[5]={1,2,1,2,2};.I want to divide this array into two arrays by extracting some elements of array "a[5]" into some different arrays like "b[2]" and "c[3]",such that int b[2] will contain {1,2} and int c[3] will contain {1,2,2}. How can it be possible using C programming?

Recommended Answers

All 3 Replies

I assume you are asking how to partition an array into two sub-arrays. This can be done like this:

void partition(int *src, int srclen, int *a, int alen, int *b, int blen, bool(*part)(int))
{
    a=malloc(srclen*sizeof(int));
    b=malloc(srclen*sizeof(int));
    int aindex,bindex;
    aindex=bindex=0;//set the indices to 0
    for (int i=0; i<srclen; ++i)
    {
        if (part(src[i]))//part is a function, it returns true if the element should go in b, otherwise it returns false
            b[bindex++]=src[i];//this line adds it to b and increments its index
        else
            a[aindex++]=src[i];//see above
    }
}

If you don't know about dynamic memory or function pointers ...

you can just do this modification of the above ...

/* partition_C_ary.c */

#include <stdio.h>


/* my partion 'truth function' used here ... */
int isEven( int a )
{
    return a % 2 == 0;
}

void partition( int a[], int lena, int b[], int* lenb, int c[],
                int* lenc ) /* Note all the passing of addressess */
{
    int i;
    *lenb = *lenc = 0;

    for( i = 0; i < lena; ++i )
    {
        if( isEven(a[i]) ) /* if a[i] is true ... then */
            b[(*lenb)++] = a[i];
        else
            c[(*lenc)++] = a[i];/* see above */
    }
}


void print( const int ary[], int size )
{
    int i;
    for( i = 0; i < size; ++i ) printf( "%d ", ary[i] );
    putchar( '\n' );
}



int main()
{
    int a[] = { 5, 4, 3, 2, 1 },
        b[ 5 ], /* get memory to hold all, just in case ... */
        c[ 5 ],
        lena = 5,
        lenb = 0,
        lenc = 0;

    /* Passing in addresses of lenb, lenc ...
       Note that isEven is 'already an address' */
    partition( a, lena, b, &lenb, c, &lenc );

    print( a, lena );
    print( b, lenb );
    print( c, lenc );

    printf( "\nPress 'Enter' to continue/exit ... " );
    fflush( stdout );
    getchar();
    return 0;
}

But if you would like to use (or learn about using) function pointers ... (so you can pass in different partion functions without needing to rewite the code of the function where the array is actually partioned) ...

this simple example, (derived from the idea provided above), may help beginners get started ...

/* partition_C_ary_2.c */

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

void partition( int a[], int lena, int b[], int* lenb, int c[],
                int* lenc, int(*part)(int) ) /* Note passing addressess */
{
    int i;
    *lenb = *lenc = 0;

    for( i = 0; i < lena; ++i )
    {
        if( part(a[i]) ) /* if a[i] is true ... then */
            b[(*lenb)++] = a[i];
        else
            c[(*lenc)++] = a[i];/* see above */
    }
}

int isEven( int a )
{
    return a % 2 == 0;
}

void print( const int ary[], int size )
{
    int i;
    for( i = 0; i < size; ++i ) printf( "%d ", ary[i] );
    putchar( '\n' );
}

int main()
{
    int a[] = { 5, 4, 3, 2, 1 },
        b[ 5 ],
        c[ 5 ],
        lena = 5,
        lenb = 0,
        lenc = 0;

    /* Passing in addresses of lenb, lenc ...
       Note that isEven is 'already an address' */
    partition( a, lena, b, &lenb, c, &lenc, isEven );

    print( a, lena );
    print( b, lenb );
    print( c, lenc );

    printf( "\nPress 'Enter' to continue/exit ... " );
    fflush( stdout );
    getchar();
    return 0;
}

Or ... you may just want something very simple like this:

/* partition_C_ary_4.c */

/*
    I have an array of integers. Let's assume int a[5]={1,2,1,2,2};
    I want to divide this array into two arrays by extracting some
    elements of array "a[5]" into some different arrays like "b[2]"
    and "c[3]",such that int b[2] will contain {1,2} and int c[3]
    will contain {1,2,2}. How can it be possible using C programming?
*/

#include <stdio.h>

void print( const int ary[], int size )
{
    int i;
    for( i = 0; i < size; ++i ) printf( "%d ", ary[i] );
}


int main()
{
    int a[] = { 1, 2, 1, 1, 2 },
        *b,
        *c,
        len_a = sizeof(a) / sizeof(a[0]),
        len_b = 2,
        len_c = 3;


    b = a; /* start address of b */
    c = a+2; /* start address of c, using pointer arithmetic */


    puts( "All a ... " );
    print( a, len_a );
    putchar( '\n' );

    puts( "All b ... " );
    print( b, len_b );
    putchar( '\n' );

    puts( "All c ... " );
    print( c, len_c );

    printf( "\n\nPress 'Enter' to continue/exit ... " );
    fflush( stdout );
    getchar();
    return 0;
}

Or ... merging this approach with the previous 'isEven' example partion above ...

that passed in the partion function using a function pointer ...

/* partition_C_ary_3.c */

#include <stdio.h>

void print( const int ary[], int size )
{
    int i;
    for( i = 0; i < size; ++i ) printf( "%d ", ary[i] );
}

/* Note passing addressess ... */
void partition( int a[], int lena, int* lenp, int(*part)(int) ) 
{
    int i, j, end = lena-1, tmp;

    for( i = 0 ; i <= end ;  )
    {
        if( part(a[i]) ) /* if a[i] is true ... then */
        {
            ++(*lenp); /* slide partition up one place ... */
            ++i;
        }
        else /* save to tmp, move all above dwn one, put in end, --end */
        {
            tmp = a[i];
            for( j = i+1; j <= end; ++j ) a[j-1] = a[j];
            a[end] = tmp;
            --end;
        }
        print( a, lena );
        printf( ": *lenp = %d", *lenp );
        printf( "\nPause for 'Enter' ... " ); fflush( stdin );
        while( getchar() != '\n' );
    }
}

int isEven( int a )
{
    return a % 2 == 0;
}



int main()
{
    int a[] = { 7, 6, 5, 4, 3, 2, 1 },
        len_a = sizeof(a) / sizeof(a[0]),
        len_p = 0;

    /* Passing in address (ref) of lenp ...
       Note that isEven is 'already an address' */
    partition( a, len_a, &len_p, isEven );

    printf( "\nlen_p = %d\n", len_p );

    print( a, len_a );
    putchar( '\n' );

    print( a, len_p );
    putchar( '\n' );

    print( a+len_p, len_a-len_p );

    printf( "\n\nPress 'Enter' to continue/exit ... " );
    fflush( stdout );
    getchar();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.