Hello, i can not quite figure out how to write a function to take numbers from an array and sort so it displays even numbers first and then odd numbers.

the output should look like this:

Original: 9 8 7 6 5 4 3 2 1
Reversed: 1 2 3 4 5 6 7 8 9
Original: 9 8 7 6 5 4 3 2 1
Even: 2 4 6 8 5 3 7 1 9

All the other functions work it is just the evenOdd function that does not.

Here is what i have written for the function:

void evenOdd(int *arr[])

        {

        int arr[N], i;

        for(i = 0; i < N; ++i)

        {

         if (arr[N] %2 == 0)

         *arr;

        }

        }

Here is the whole program if anyone wants to compile it to check:

#include <stdio.h>
        #include <stdlib.h>
        #define N 9

        void revArray(int arr[])
        {
             int temp[N];
             int i;
             for(i = 0; i < N; ++i)
        {
            temp[i] = arr[N - 1 -i];
        }   
         for(i = 0; i < N; ++i) 
        {
         arr[i] =temp[i];
        } 
        
        }
        
        
        int *revElement(int arr[]) 
        {

        static int idx = N;

        idx = --idx;

        return arr + idx;

        }
        
        
        void evenOdd(int *arr[])

        {

        int arr[N], i;

        for(i = 0; i < N; ++i)

        {

        if (arr[N] %2 == 0)

        *arr;

        }

        }
         
        
 
            
        
       /* MAIN */
       int main(int argc, char **argv) 
       
       {

       /* Load the array with numbers */
       int a[N];
       int idx;
       int *p = a; 
       /* p points to a[0]
       by storing the address &a[0]
       */
       /*
       while(p < a + N) *p++ = a + N - p; /*\
       using p, fill up the array one
       index at a time
       */ 
       while (p < a + N)
       {
       *p = a + N - p;
       p = p + 1;
       }
       printf("Original: ");
       p = a; while(p < a + N) printf("%2d ",*p++);

       /* Part A: Reverse the array */
       /* AS IN reverse the contents of the original array a */
       revArray(a);
       printf("\nReversed: ");
       p = a; while(p < a + N) printf("%2d ",*p++);
       printf("\n");

       /* Part B: Return elements in reverse order */
       /* AS IN NOT to modify the contents of the array
       which are in order
       1 2 3 4 5 6 7 8 9
       but to print the contents of this array
       from the last index to the first one.
       HINT: this would be a good function to
       use static variable(s) in.
       */
       printf("Original: ");
       for (idx = 0; idx < N; idx++) {
       
       printf("%2d ",*revElement(a));
       
       }
       printf("\n");
       
       evenOdd(a);
        
       p = a; while(p < a + N) printf("%2d ",*p++);

       printf("\n");
        
       system("pause");
       exit(0);
       }

Any help would be much appreciated, thank you.

You can use any sorting algorithm with the right comparison:

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

int compare(void* const a, void* const b)
{
    int const ia = *(int const*)a;
    int const ib = *(int const*)b;

    return (abs(ia) % 2) - (abs(ib) % 2);
}

int main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    size_t const sz = sizeof a / sizeof *a;
    size_t x;

    qsort(a, sz, sizeof *a, compare);

    for (x = 0; x < sz; ++x) printf("%d%s", a[x], x < sz - 1 ? " " : "\n");

    return 0;
}

It does not matter that I used qsort. The comparison function is the part that matters for this example. Another way to do it is walk down the array checking for evens, and if the value is not even, move it to the end. The only hard part to that method is filling in the hold left by the vacated value:

/* fill in the hold at &a[x] */
memmove(&a[x], &a[x+1], (SIZE - x) * sizeof *a);
/* now a[SIZE-1] is open for */

You can do the same thing with a loop by shifting every value after the vacated value one spot to the left:

/* fill in the hold at &a[x] */
for (y = x; y < SIZE - 1; ++y) a[y] = a[y+1];
/* now a[SIZE-1] is open for */
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.