0

I have a problem with this piece of code, I'm trying to print the EVEN and ODD numbers, but there is a problem when it comes to show them, the vectors don't save the numbers as I'm expecting.

    #include <iostream>
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
        int vect[n], even[n], odd[n]; // CREATING VECTORS LIMIT AFTER "n"
    
        for(int i = 1; i <= n; ++i) { // ENTERING The ELEMENS IN VECTOR
                cin >> vect[i];
        }
    
        for(int i = 1; i <= n; ++i) {
                if(vect[i] % 2 != 0) {
                    odd[i] = vect[i];           // I think that here's the problem, the vectors don't save the right numbers.
                }                               /// VERIFYING IF THE NUMBER IS ODD OR EVEN.
                else if (vect[i] % 2 == 0) {
                    even[i] == vect[i];
                }
        }
        for(int i = 1; i <= n; ++i) {
                cout << even[i] << " " << endl;    /// PRINTING THE ODD AND EVEN numbers.
                cout << odd[i] << " " << endl;
        }
    
    
    return 0;x
    }
Dominique
  • 16,450
  • 15
  • 56
  • 112
G Andrei
  • 154
  • 8
  • 1
    Can you specify, what do you mean " the vectors don't save the numbers as i'm expecting" ? – IndieGameDev Nov 26 '20 at 09:46
  • 3
    Look closely at `even[i] == vect[i];` for a few minutes. (A decent complier would warn you.) – molbdnilo Nov 26 '20 at 09:48
  • 1
    you are going from 0 to n and insert the number into odd/even. However, even/odd is filled only on even/odd index. I recommend using a `std::vector` instead. Variable length arrays are not standard c++. – Raildex Nov 26 '20 at 09:49
  • 1
    There are `n` numbers, and your last loop assumes that among them are `n` even numbers and `n` odd numbers. – molbdnilo Nov 26 '20 at 09:49
  • You are also indexing outside the arrays, which has undefined behaviour. – molbdnilo Nov 26 '20 at 09:50
  • 1
    there are no vectors in your code. The name of `std::vector` is debatable, but calling c-arrays "vector" is highly confusing – 463035818_is_not_an_ai Nov 26 '20 at 09:51
  • 1
    Note that array indexes in C++ start at zero and end at `n - 1`, so attempting to access `vect[i]` at the end of your first `for` loop is already undefined behaviour. And variable length arrays are *not* part of the C++ (standard) language. – Adrian Mole Nov 26 '20 at 09:51
  • 1
    please read this: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Nov 26 '20 at 09:53
  • please dont fix errors in your question. The wrong code belongs to the question, fixes are for answers. – 463035818_is_not_an_ai Nov 26 '20 at 10:04

3 Answers3

2

I have fixed the problem, thanks all for help. Now it works perfectly.

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int vect[n], even[n], odd[n], z = 0, x = 0; // CREATING VECTORS LIMIT AFTER "n"

    for(int i = 1; i <= n; ++i) { // ENTERING The ELEMENS IN VECTOR
            cin >> vect[i];
    }

    for(int i = 1; i <= n; ++i) {
            if(vect[i] % 2 != 0) {
                odd[1+z] = vect[i];
                z++;
                            // I think that here's the problem, the vectors don't save the right numbers.
            }                               /// VERIFYING IF THE NUMBER IS ODD OR EVEN.
            else if (vect[i] % 2 == 0) {
                even[1+x] = vect[i];
                x++;
            }
    }

    for(int i = 1; i <= x; i++) {
        cout << even[i] << " ";
    }
    cout << endl;
      for(int i = 1; i <= z; i++) {
        cout << odd[i] << " ";
    }


return 0;
}
G Andrei
  • 154
  • 8
  • 2
    So you have a problem, describe it clearly, and in case you find the answer yourself you add it to this website. **Great!** I would just give you a simple remark: you are calling your coefficients `z` and `x`. If ever within a long time (let's say in a year) you see this code again you might think "but what do those variables mean?", you can clarify this by using longer names, like `even_index` or `odd_number` or something like that. – Dominique Nov 26 '20 at 10:43
1

Considering the hints of the comments, your program shall be changed into this:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main() {
        int n, number;
        cin >> n;
        vector<int> vect, even, odd; // CREATING DYNAMIC VECTORS
    
        for(int i = 0; i < n; ++i) { // ENTERING THE ELEMENTS IN VECTOR
                cin >> number;
                vect.push_back(number);
        }
    
        for(int i = 0; i < n; ++i) {
                if(vect[i] % 2 != 0) { /// VERIFYING IF THE NUMBER IS ODD OR EVEN.
                    odd.push_back(vect[i]);
                }                      
                else {
                    even.push_back(vect[i]);
                }
        }
        for (int i = 0; i < n; ++i)
            cout << vect[i] << " ";
        cout << endl;
        /// PRINTING THE ODD AND EVEN NUMBERS.
        for (auto& val : odd)
            cout << val << " ";
        cout << endl;
        for (auto& val : even)
            cout << val << " ";
        cout << endl;
    return 0;
    }

It uses the vector container of STL for your arrays, start the indexing at 0 and prints out the resulting arrays separately, as the number of odd and of even entries might be different.

Hope it helps?

CKE
  • 1,533
  • 19
  • 18
  • 29
1

With standard, you might use std::partition (or stable version) to solve your problem:

void print_even_odd(std::vector<int> v)
{
    auto limit = std::stable_partition(v.begin(), v.end(), [](int n){ return n % 2 == 0; });
    
    std::cout << "Evens:";
    // Pre-C++20 span:
    // for (auto it = v.begin(); it != limit; ++it) { int n = *it;
    for (int n : std::span(v.begin(), limit)) {
        std::cout << " " << n;    
    }
    std::cout << std::endl;
    std::cout << "Odds:";
    for (int n : std::span(limit, v.end())) {
        std::cout << " " << n;    
    }
    std::cout << std::endl;
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302