organization function

I am supposed to write a program that gets an array then the program sorts the array based on even or odds. For example, when the array {1,4,2,3,5,8,7,6} is passed to the function the result would be {4,2,8,6,1,3,5,7}. Here is what I have now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

void EvenOddSort(int array[]);

int main()
{
    int array[];
    EvenOddSort(array);
    system("pause");
}
void EvenOddSort(int array[])
{
     int j; 
     for (int i=1; i<=8; i++)
     {
         printf("Enter next integer: ");
         array[j]=GetInteger();
     } 
     /* i dont know what to put next */
}
I won't write the code, but I will try to guide you as much as I can, I assume that you only try to separate the array in to two group, evens and odds

first of all what should we check to achive this serie, first group of numbers are even, well which means that you need to check every number in the array whether it is even, if so then, you have to put it in to first index of the array and increment evenCounter by 1 after finding one more even number increment evenCounter by 1 too and put the second even number in to the proper place (hint this proper place is evenCounter), by the way do not ovver ride your array which you look up the numbers from, write them in to an other array, when you finish looking up evens (this means a for loop through array), just open an other for loop to find odd numbers and put them in to remaining places. I tried to explain as much as I can, this is realy easy question. Do it your self this is the key of learning some thing.
I hope this was clear enough, sory for my english
Last edited on
Okay thanks for the tips.
Would using the Bucket Sort algorithm be an efficient way of solving this?
http://www.cplusplus.com/reference/algorithm/remove_if/

Despite the name, it doesn't really remove anything. The example code is even related to your problem.
Thanks.
You could declare two pointers p_start and p_end and make one point to the first array element and p_end point the the last array element.
If p_start points to an odd number i.e. array[i]%2!=0 and p_end points to an even number i.e. array[i]%2==0 swap them. Now move each pointer back to its respective array end.
Increment p_start until it points to an element holding an odd number.
Similarly decrement the p_end pointer until it points to an array element holding an even number and swap them.
Return the pointers to their correct array ends and continue the process.
Theoretically each pointer only has to iterate through half the array assuming that half the array comprises even numbers and the other half odd. But you need a control to ensure that the pointers don`t cross over each other in the case where there are more even than odd numbers or visa versa.
Sorry for the late reply but I've been busy. Anyways, I tried to make the program and I think I made some progress. Right now the program isn't doing anything with the array. Here is what I have right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <iostream>

void EvenOddSort(int array[]);
int main()
{
    int array[]={1,4,2,3,5,8,7,6};
    EvenOddSort(array);
    system("pause");
}
void EvenOddSort(int array[])
{
     int result[8];
     int size=8;
     int i=0;
     int c;
     for (c=0; c<size; c++)
     {
         if (array[c]%2==0)
         {
                            result[i] = array[c];
                            i+=1;
         }
         else
         {
             result[i]=array[c];
             i+=1;
         }
     }
     for (c=0; c<size; c++)
     {
         printf("%d ", result[c]);
     }
     printf("\n");
}
Okay now the program is only outputting 1. I'm not sure why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

bool IsOdd (int i) { return ((i%2)!=1); }
bool IsEven (int i) { return ((i%2)==1); }
void EvenOddSort(int myints[], int myint[]);
int main()
{
    int myints[] = {1,2,3,4,5,6,7,8,9};            
    int myint[] = {1,2,3,4,5,6,7,8,9};
    EvenOddSort(myints, myint);
    system("pause");
}
void EvenOddSort(int myints[], int myint[])
{
  int* pbegin = myints;                         
  int* pend = myints+sizeof(myints)/sizeof(int);   

  pend = remove_if (pbegin, pend, IsEven);       
  for (int* p=pbegin; p!=pend; ++p)
  {
    printf("%d ", *p);
  }
  int* begin = myint;                          
  int* end = myint+sizeof(myint)/sizeof(int);    
  end = remove_if (begin, end, IsOdd);
  for (int* p=begin; p!=end; ++p)
  {
    printf("%d ", *p);
  }
  printf("\n");
}
Topic archived. No new replies allowed.