1,105,427 Community Members

sort even and odd numbers from an array

Member Avatar
purepecha
Newbie Poster
12 posts since Feb 2008
Reputation Points: -6 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

/* this program compiles is almost done, but i cannot get the las function to sort numbers from the array. the output should have the even number on the left and odd numbers on the fright.*/

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

#define N 9

void revArray(int *a); /* reverse an array in place */
int *revElement(int *a); /* reverse iterator for array */
void evenOdd(int *a); /* sort array for even/odd order */

int main(int argc, char **argv) {
    char x;
    int array[N]; 
    int idx; 
    int *ptr = array; 
    while(ptr < array + N) {
        *ptr++ = array + N - ptr;
    }
    printf("Original: ");
    ptr = array;
    while(ptr < array + N) {
        printf("%2d ",*ptr++);
    }

    revArray(array);
    printf("\nReversed: ");
    ptr = array;
    while(ptr < array + N) {
        printf("%2d ",*ptr++);
    }
    printf("\n");

    printf("Original: ");
    for (idx = 0; idx < N; idx++) {
        printf("%2d ",*revElement(array));
    }
    printf("\n");

    /*Put even numbers first, odd numbers last in the array. Order of
    the elements is not important as long as all evens are before first odd */
    printf("Even: ");
    evenOdd(array);
    ptr = array;
    while(ptr < array + N) {
        printf("%2d ",*ptr++);
    }
    printf("\n");
    printf("\nEnter RETURN to continue...");
    scanf("%c",&x);
    printf("\n[done]\n");
    exit(0);
}

void revArray(int *ptr) {
    int arr[N], i;
    int *s = ptr;
    for(i = N-1; i >=0; --i)
    {
        arr[i] = *ptr++;
    }
    for(i = 0; i < N; ++i)
    {
        *(s+i) = arr[i]; 
    }

    return;
}

int *revElement(int *ptr) {
    static int idx2 = N;
    idx2 = --idx2;
    return ptr + idx2;
}

void evenOdd(int *ptr)
{ 
    int arr[N], i;
    for(i = 0; i < N; ++i)
    {
        if (arr[N] %2==0)
            *ptr;
    }
}
Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
0
 

To print two colums, one with evens, the other with odds. I'd separate the original array into two separate arrays, one for evens and one for odds. Then I'd loop through the arrays printing evens and odds with the same array index on the same line. If there was more even than odd or visa versa, then I'd print an empty string or the space char at the appropriate index.

BTW: this program is written in pure C and may be better located on the C board than the C++ board. Also, please learn how to use code tags to maintain the indentation and other human friendly characteristics of the code you write when posting code to the board. Use of code tags is explaned in one of the sticky notes at the top of the board.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

I'd separate the original array into two separate arrays, one for evens and one for odds. Then I'd loop through the arrays printing evens and odds with the same array index on the same line. If there was more even than odd or visa versa, then I'd print an empty string or the space char at the appropriate index.

That's pretty much the way I would do it as well. For a concrete example, here's a kewl C++ version that's unlikely to be accepted as a homework solution:

#include <cstdlib>
#include <iostream>
#include <vector>

template <typename T>
struct even_odd_indexer {
    // Number of indices supported
    static const int indices = 2;

    // Convert a value to an index
    int index ( T value ) { return value % 2 != 0; }
};

template <typename T, typename Indexer>
std::vector<std::vector<T> > jsw_split (
    const std::vector<T>& src, Indexer comp )
{
    std::vector<std::vector<T> > split ( comp.indices );
    std::vector<T>::const_iterator it = src.begin();
    std::vector<T>::const_iterator end = src.end();

    // Split the source values by index
    while ( it != end ) {
        split[comp.index ( *it )].push_back ( *it );
        ++it;
    }

    return split;
}

int main()
{
    std::vector<int> v;

    for ( int i = 0; i < 20; i++ )
        v.push_back ( std::rand() % 100 );

    std::vector<std::vector<int> > split =
        jsw_split ( v, even_odd_indexer<int>() );
    std::vector<int>::const_iterator it_even = split[0].begin();
    std::vector<int>::const_iterator end_even = split[0].end();
    std::vector<int>::const_iterator it_odd = split[1].begin();
    std::vector<int>::const_iterator end_odd = split[1].end();

    std::cout<<"Even\t\tOdd\n";

    while ( it_even != end_even || it_odd != end_odd ) {
        if ( it_even != end_even )
            std::cout<< *it_even++;

        std::cout<<"\t\t";

        if ( it_odd != end_odd )
            std::cout<< *it_odd++;

        std::cout<<'\n';
    }
}
Member Avatar
vijayan121
Posting Virtuoso
1,769 posts since Dec 2006
Reputation Points: 1,097 [?]
Q&As Helped to Solve: 329 [?]
Skill Endorsements: 16 [?]
 
0
 

std::partition with a predicate could be used to split the sequence.

#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std ;

template< typename ITER > inline
void column_print( ITER beginc1, ITER endc1,
                   ITER beginc2, ITER endc2 )
{
   while( (beginc1!= endc1) || (beginc2!= endc2) )
   {
      if( beginc1 != endc1 ) cout << *beginc1++ ;
      cout << "\t\t" ;
      if( beginc2 != endc2 ) cout << *beginc2++ ;
      cout << '\n' ;
   }
}

int main()
{
   srand( unsigned( time(0) ) ) ;
   std::vector<int> v;
   for ( int i = 0; i < 20; i++ )
         v.push_back ( std::rand() % 100 ) ;
   vector<int>::iterator part = partition( v.begin(), v.end(),
                       not1( bind2nd( modulus<int>(), 2 ) ) ) ;
   column_print( v.begin(), part, part, v.end() ) ;
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

BTW: this program is written in pure C and may be better located on the C board than the C++ board.

It was. He spammed the forums with this problem

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

>std::partition with a predicate could be used to split the sequence.
That's boring though. ;)

Member Avatar
vijayan121
Posting Virtuoso
1,769 posts since Dec 2006
Reputation Points: 1,097 [?]
Q&As Helped to Solve: 329 [?]
Skill Endorsements: 16 [?]
 
0
 

> That's boring though.
yeah. it also doesn't have enough meat for submission as a solution to an assignment.

Member Avatar
IVR_Developer
Newbie Poster
11 posts since Mar 2009
Reputation Points: -1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

rewrite your evenOdd() function

void evenOdd(int *ptr){
        int arr[N], i;
        int j; /*for keeping track of the index for arr*/
        int*tmp;

        j=0;
        tmp=ptr;

        for(i = 0; i < N; ++i, tmp++){
                if (*tmp %2==0) {
                        arr[j]=*tmp;
                        j++;
                }
        }

        tmp=ptr;
        for(i = 0; i < N; ++i, tmp++){
                if (*tmp %2!=0) {
                        arr[j]=*tmp;
                        j++;
                }
        }

        for(i = 0; i < N; ++i, ptr++)
                *ptr=arr[i];

}
Member Avatar
_marie_
Newbie Poster
1 post since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

what is the code that will search the list of even numbers in an array of 10 integers and print them out.

Member Avatar
avillafuerte
Newbie Poster
2 posts since Feb 2011
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

yes i also have the same problem,.can anybody will help us????..please!..any answer will be appreciated.. :)

Member Avatar
arkoenig
Practically a Master Poster
691 posts since Jun 2010
Reputation Points: 340 [?]
Q&As Helped to Solve: 110 [?]
Skill Endorsements: 9 [?]
 
0
 

@narue: In line 11 of your example, shouldn't value%2 be value%indices?

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

I think rather than use value % indices , the indices member should be removed as stale code. There's really no need for it anyway, and using it wouldn't make the code any better.

Member Avatar
arkoenig
Practically a Master Poster
691 posts since Jun 2010
Reputation Points: 340 [?]
Q&As Helped to Solve: 110 [?]
Skill Endorsements: 9 [?]
 
0
 

That would work too.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article