Home > OS >  why isn't my vector pushbacking even and odd numbers
why isn't my vector pushbacking even and odd numbers

Time:11-11

i tried to separate even and odd numbers using vectors from a array ==> so i made a function that returns true is number is even and false for if number is odd then i used an if else statement where if the function returns true then it pushbacks the value in a vector and if the function returns false then it pushbacks the value in another vector , finally i printed all the elements in the vector but the output does not show any element except it shows one in the odd vector.

#include <iostream>
#include <vector>

using namespace std;

bool sort(int arr[] , int i){
    if(arr[i] %2 == 0){
        return true;
    }
    return false;
}

int main(){
    int n;
    cin >> n;
    int *arr = new int[n];
    for(int i=1 ; i<n ; i  ){
        arr[i-1] = i;
    }
    vector <int> even , odd;
    int i=0 ;
    if(sort(arr , i)){
        even.push_back(arr[i]);
        sort(arr , i 1);
    }else{
        odd.push_back(arr[i]);
        sort(arr,i 1);
    }

    cout << "the even numbers are : " << endl;
    for(auto element:even){
        cout << element << " ";
    }
    cout << endl;
    cout << "the odd numbers are : " << endl;
    for(auto element:odd){
        cout << element << " ";
    }
}

CodePudding user response:

As @TonyDelroy said, you have to make for loop around call to sort(arr, i). Also first loop should go up to i <= n instead of i < n.

Your fixed working code below (see also std::partition_copy variant afterwards):

Try it online!

#include <iostream>
#include <vector>

using namespace std;

bool sort(int arr[] , int i){
    if(arr[i] %2 == 0){
        return true;
    }
    return false;
}

int main(){
    int n;
    cin >> n;
    int *arr = new int[n];
    for(int i=1 ; i<=n ; i  ){
        arr[i-1] = i;
    }
    vector <int> even , odd;

    for (int i = 0; i < n;   i)
        if (sort(arr, i))
            even.push_back(arr[i]);
        else
            odd.push_back(arr[i]);

    cout << "the even numbers are : " << endl;
    for(auto element:even){
        cout << element << " ";
    }
    cout << endl;
    cout << "the odd numbers are : " << endl;
    for(auto element:odd){
        cout << element << " ";
    }
}

Input:

10

Output:

the even numbers are : 
2 4 6 8 10 
the odd numbers are : 
1 3 5 7 9 

As @chris said you can also use std::partition_copy to implement your algorithm:

Try it online!

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    int n = 0;
    std::cin >> n;
    std::vector<int> arr(n), odd, even;
    for (int i = 1; i <= n;   i)
        arr[i - 1] = i;

    std::partition_copy(arr.cbegin(), arr.cend(),
        std::back_insert_iterator(odd), std::back_insert_iterator(even),
        [](auto const & x){ return (x & 1) == 1; });

    std::cout << "the even numbers are : " << std::endl;
    for (auto element: even)
        std::cout << element << " ";
    std::cout << std::endl << "the odd numbers are : " << std::endl;
    for (auto element: odd)
        std::cout << element << " ";
}

Input:

10

Output:

the even numbers are : 
2 4 6 8 10 
the odd numbers are : 
1 3 5 7 9 

CodePudding user response:

You only push one element - the first.
Your partitioning code is equivalent to

if(sort(arr , 0)){
    even.push_back(arr[0]);
    sort(arr , 1);
}else{
    odd.push_back(arr[0]);
    sort(arr,1);
}

You need to loop over all the input numbers.

You can also simplify matters with a more generally useful evenness function that doesn't depend on an array:

bool is_even(int x) { return x % 2 == 0; } 

and then there is no need to store all the inputs before processing them:

int main(){
    vector <int> even , odd;
    int n;
    cin >> n;
    for (int i = 0; i < n;   i) {
        int x;
        cin >> x;
        if (is_even(x)) {
            even.push_back(x);
        }
        else {
            odd.push_back(x);
        }
    }

    cout << "the even numbers are : " << endl;
    for (auto element:even){
        cout << element << " ";
    }
    cout << endl;
    cout << "the odd numbers are : " << endl;
    for (auto element:odd){
        cout << element << " ";
    }
}
  •  Tags:  
  • c
  • Related