Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't sort arrays with even numbers followed by odd numbers

I first wrote this: (which works as expected)

#include<iostream>
using namespace std;

int main() {
    int a[5],cpy[5],ctr = 0;

    for (int i = 0 ; i<5 ; i++) {
        cout<<"Enter Value for index "<<i<<": ";
        cin>>a[i];
    }

    for (int i = 0 ; i<5 ; i++)
        if (a[i]%2==0) {
            cpy[ctr]=a[i];
            ctr++;
        }
    for (int i = 0 ; i<5 ; i++)
        if (a[i]%2!=0) {
            cpy[ctr]=a[i];
            ctr++;
        }

    for (int i = 0 ; i<5 ; i++)
        cout<<cpy[i]<<" ";

    return 0;
}

Wanted to make it more condensed/cleaner by improving my logic,
this is what I came up with:

#include<iostream>
using namespace std;

int main() {
    int a[5],cpy[5],ctr = 0;

    for (int i = 0 ; i<5 ; i++) {
        cout<<"Enter Value for index "<<i<<": ";
        cin>>a[i];
    }

    for (int i = 0 ; i<5 && a[i]%2==0 ; i++,ctr++)
            cpy[ctr]=a[i];
    for (int i = 0 ; i<5 && a[i]%2!=0 ; i++,ctr++)
            cpy[ctr]=a[i];

    for (int i = 0 ; i<5 ; i++)
        cout<<cpy[i]<<" ";

    return 0;
}

Expected Result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Enter Value for index 0: 1
Enter Value for index 1: 2
Enter Value for index 2: 3
Enter Value for index 3: 4
Enter Value for index 4: 5
2 4 1 3 5

What i get after running 2nd version:

Enter Value for index 0: 1
Enter Value for index 1: 2
Enter Value for index 2: 3
Enter Value for index 3: 4
Enter Value for index 4: 5
1 0 24 0 0

Can you suggest where I am wrong in the 2nd block of code. The first block works correctly.

>Solution :

The problem here is that you will never enter the first loop. The counter is incremented only if the condition is satisfied, otherwise the loop is broken. You should not implement a condition like this.

I suggest you to try the following with std::vector :

#include<iostream>
#include<vector>
using namespace std;

int main() {
    vector<int> a, cpy;

    for (int i = 0 ; i<5 ; i++) {
        a.push_back(i+1);
    }

    for (int i = 0 ; i<5; i++) {
        if (a[i]%2 == 0)
            cpy.push_back(a.at(i));
    }
    for (int i = 0 ; i<5 ; i++) {
        if (a[i]%2 != 0)
            cpy.push_back(a.at(i));
    }

    for (int i = 0 ; i<5 ; i++)
        cout<<cpy[i]<<" ";

    return 0;
}

It works as expected, and in a more condensed manner.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading