/* 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;
    }
}

Recommended Answers

All 12 Replies

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.

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';
    }
}

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() ) ;
}

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

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

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

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];

}

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

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

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

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.

That would work too.

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.