top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

create a fuction to separate odd & even integers

+4 votes
444 views

Write the function READ, which is passed two double pointers pointing to the head pointers of two linked lists.

One list will hold even integers, the other one will hold odd integers. READ reads a series of integers. It separates adds odd integers to the first list, and even ones to the second, all in sorted order.

posted Apr 27, 2016 by Devendra Bohre

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Try this

#include <iostream>
#include <list>

using namespace std;

// unary predicates implemented as classes
struct is_odd {
    bool operator() (const int& value) { return (value%2)==1; }
};

struct is_even {
    bool operator() (const int& value) { return (value%2)==0; }
};

// function Google wants
void READ(list<double> *odd, list<double> *even);

// main with test data to make sure my function works
int main() {

    list<double> odds;  // list for odds
    list<double> evens; // list for evens

    double myDoubles[] = {1, 5, 4, 6, 2, 3, 8, 7};  // test data
    double moreDoubles[] = {11, 15, 14, 19, 9, 12}; // test data
    odds.assign(myDoubles, myDoubles + 8);          // give first set of test data to odd list
    evens.assign(moreDoubles, moreDoubles + 6);     // give second set of test data to even list

    READ(&odds, &evens);    // function call

    // printing of the odd list
    for (auto &itr : odds) {
        cout << itr << endl;
    }

    cout << endl;   // printing newline for neatness

    // printing the even list
    for (auto &itr : evens) {
        cout << itr << endl;
    }

    return 0;


}

// implementation of Google function
void READ(list<double> *odd, list<double> *even)
{
    // look at each element in odd list
    for (auto & itr: *odd) {
        // if it is even
        if ((int)itr % 2 == 0) {
            // put that same value in the even list
            even->push_back(itr);
        }
    }

    // remove all evens from odd list
    odd->remove_if(is_even());

    // look at each element in the even list
    for (auto & itr: *even) {
        // if the element is odd
        if ((int)itr % 2 == 1) {
            // put that same value in the odd list
            odd->push_back(itr);
        }
    }

    // remove all odds from the even list
    even->remove_if(is_odd());

    // sort odd list
    odd->sort();

    // sort even list
    even->sort();

}
answer May 20, 2016 by Manikandan J
Similar Questions
+3 votes

Rohit's teacher has asked him to write a function that takes as input parameters the first parameter will be an integer number representing. The number whose digitSum needs to be found the second parameter will be a char representing the
option which would either be 'e' or 'o' representing 'even' or 'odd' respectively.

Function signature public int digitSum(int n, char ch);

digitSum(9625, 'o')
Example 1: if given number is 9625 and the option is 'o' we must add only the odd digit i.e. 9+5=14
digitSum(2134, 'e')
Example 2: if given number is 2134 and the option  is 'e' we must add only the odd digit i.e. 2+4=6
...