-1

My function that checks if numbers in the vector are even or odd doesn't work properly. This function prints the numbers that i have typed in the vector and puts them in 2 categories EVENS/ODDS. But if i have a negative number there is a problem with the odds not printing it but evens work. problematic code is here:

void printEvensAndOddsVector(const vector <int>& new_v1)
{
    cout << "Vector Evens: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        if (new_v1.at(i) % 2 == 0)
        {
            cout << new_v1.at(i) << " ";
        }
    }
    cout << " \n";

    cout << "Vector Odds: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        if (new_v1.at(i) % 2 == 1) // Here is the problem.
        {
            cout << new_v1.at(i) << " ";
        }
    }
    cout << " \n";
}

Or more exactly this line: if (new_v1.at(i) % 2 == 1) It prints the numbers that are odd but only the positive ones not the negative ones. But if i change it to if (new_v1.at(i) % 2 != 0) then it works correctly. Why does that happen and is there a problem with the equal operator? If yes then why are the evens getting printed even if they are negatives while still using the equal operator?

Code here for reference.

clude <iostream>
#include <vector>

using namespace std;

void fillVector(vector <int>&);

void printVector(const vector <int>&);

void printEvensAndOddsVector(const vector <int>&); // Prints Evens and Odds.

int main()
{
    vector <int> v1;

    fillVector(v1);
    printVector(v1);
    printEvensAndOddsVector(v1);

    cout << "\n\n\n";
    system( "pause");
    return 0;
}

// Function Definitions
void fillVector(vector <int>& new_v1)
{
    int number;
    cout << "Type in numbers and type -100 to stop: ";
    cin >> number;

    while (number != -100)
    {
        new_v1.push_back(number);
        cin >> number;
    }
}

void printVector(const vector <int>& new_v1)
{
    cout << "\nVector: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        cout << new_v1.at(i) << " ";
    }
    cout << " \n";
}

void printEvensAndOddsVector(const vector <int>& new_v1)
{
    cout << "Vector Evens: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        if (new_v1.at(i) % 2 == 0)
        {
            cout << new_v1.at(i) << " ";
        }
    }
    cout << " \n";

    cout << "Vector Odds: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        if (new_v1.at(i) % 2 == 1)
        {
            cout << new_v1.at(i) << " ";
        }
    }
    cout << " \n";
}
Johnson
  • 145
  • 4
  • 10
  • 5
    Do you need all of this code to demonstrate a simple issue with the modulo operator? A 3 or 4 line `main()` program is all you need. – PaulMcKenzie Jan 14 '15 at 15:33
  • The result of `%` on a negative number is probably negative. – Roger Rowland Jan 14 '15 at 15:33
  • @Paul yea i know that but the question still remains.I am not asking how is the fastest or best way to code it. I am doing this program for a reason and my code dosn't work. my question is why is that? – Johnson Jan 14 '15 at 15:34
  • 1
    My point is that you have an issue with the '%' operator. Therefore a simple main() program showing usage of the '%' operator is all that was needed. Not vectors, not functions, etc... – PaulMcKenzie Jan 14 '15 at 15:35
  • @Roger nope i checked it too. https://encrypted.google.com/search?output=search&sclient=psy-ab&q=-5+mod+2&btnG=&oq=&gs_l=&pbx=1 – Johnson Jan 14 '15 at 15:35
  • 5
    @Johnson How does a Google calculator tell you anything about the behaviour of `%` in C++? – Angew is no longer proud of SO Jan 14 '15 at 15:36
  • 1
    @Johnson: That calculator link says nothing about how things work in C++. – NPE Jan 14 '15 at 15:36
  • @Johnson You're seriously not listening here. We're trying to help. – Roger Rowland Jan 14 '15 at 15:37
  • @Johnson Please look here: http://ideone.com/peAtQi See how simple it is to write a program, and then present your question using a simple example? Not only will it help us, it helps you, and it may also help someone searching on SO for similar issues. – PaulMcKenzie Jan 14 '15 at 15:39
  • possible duplicate of [Modulo operator with negative values](http://stackoverflow.com/questions/7594508/modulo-operator-with-negative-values) – Tadeusz Kopec for Ukraine Jan 14 '15 at 15:40
  • Roger sorry i didnt say you are not tring to help i am just stating that the google calculator gives the result of a mathematical expresion right? Shouldnt that be universal? If a calculator says the remainder of something == 1 shouldnt that be the correct thing? – Johnson Jan 14 '15 at 15:40
  • You're confusing mathematics with C++ standards. They are not the same thing. Read [the answer](http://stackoverflow.com/a/7594577/2065121) that @NPE linked to. – Roger Rowland Jan 14 '15 at 15:40
  • You can use '&1' instead of '%2'.The problem already explained. – oknsnl Jan 14 '15 at 15:58

1 Answers1

3

This line: if (new_v1.at(i) % 2 == 1). It prints the numbers that are odd but only the positive ones not the negative ones.

For negative n, n % 2 returns either 0 or -1 in C++. It never returns 1, so the condition cannot be true for negative inputs.

As you've discovered, comparing to zero would work.

See Modulo operator with negative values

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Oh that explain why even work still cause its 0 right? Also then why is: https://encrypted.google.com/search?output=search&sclient=psy-ab&q=-5+mod+2&btnG=&oq=&gs_l=&pbx=1 saying remainder 1 and not -1 ? – Johnson Jan 14 '15 at 15:37
  • Ok mate thanks now i understand and chose best answer in 7 minutes. – Johnson Jan 14 '15 at 15:38