-4

my code is going to take ten numbers and separate the numbers to two groups : odd and positive numbers

   #include <iostream>
using namespace std;


int odd(int a)
{
if (a % 2 != 0)
        {
            return a;
        }
}

int pos(int p)
{
if (p >= 0)
        {
    return p;
        }
}
int main()
{
    int i;
    int entery[10];
    
    cout << "please enter ten number : ";
    for (i = 0; i < 10; i++)

    {

        cin >> entery[i];

        }
    
    cout << "the odd numbers are : \n" << odd(entery[i]);
         
    cout << "your positive numbers are : \n" << pos(entery[i]);
}   

it it somehow works but the odd number is always 0 and the positive number always is -858993460

I use it without array and both groups become zero

  • 1
    Your `odd` and `pos` functions do not return a value when the condition is `false`. That makes your program have _undefined behavior_. Instead do: `bool odd(int) { return a%2 != 0; }` and `bool pos(int p) { return p >= 0; }` – Ted Lyngmo Dec 13 '21 at 10:50
  • 1
    -858993460 = 0xCCCCCCCC which means you're reading uninitialized memory: [When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/q/370195/995714) – phuclv Dec 13 '21 at 11:05
  • 3
    *my code works but it does not give me what I want*: it means that it does not work. The code merely *compiles*, but due to the number of errors invoking undefined behavior, is not working at all. – prapin Dec 13 '21 at 11:27

1 Answers1

1

Here it goes your code corrected. Just to let you know, your code wasn't checking all the possible conditions inside odd and pos functions. It just was returning something when the condition was false.
I modified these functions, making it work with a bool now instead of a number. In your main method, now, when this functions returns a true, it will print the number, but won't do it if it's returning a false.

#include <iostream>
using namespace std;
    
bool odd(int a) { return a % 2 != 0; }

bool pos(int p) { return p >= 0; }

int main()
{
    int i;
    int entery[10];
    
    cout << "Please enter ten numbers: ";
    for (i = 0; i < 10; i++)
        cin >> entery[i];
    
    cout << "The odd numbers are : ";
    for(int i = 0; i < 10; i++) {
        if (odd(entery[i]))
            cout << entery[i] << " ";
    }
    
    cout << "\nYour positive numbers are: ";
    for(int i = 0; i < 10; i++) {
        if (pos(entery[i]))
            cout << entery[i] << " ";
    }
}   

If you want to change your program in the future, you can change the for to make it work while sizeof(entery)/sizeof(entery[0]), so it will iterate the array looking at the size instead of the number 10.

FourBars
  • 475
  • 2
  • 14
  • The return value from `odd` and `pos` would be better as `bool` instead of `int` – Ted Lyngmo Dec 13 '21 at 10:58
  • @TedLyngmo `odd` and `pos` methods in my code are working/returning with a `bool` var. I'm not sure what you mean. Can I have a better explanation? – FourBars Dec 13 '21 at 11:01
  • 3
    I mean, `bool odd(int a);` and `bool pos(int p);` - there's no reason to convert the `bool` you have inside the function to an `int` that will then be used as a `bool` in boolean context afterwards. Stay with `bool` all the way. You could also simplify both functions: `bool odd(int a) { return a % 2 != 0; }` and `bool pos(int p) { return p >= 0; }` – Ted Lyngmo Dec 13 '21 at 11:02
  • 1
    @TedLyngmo ooops! You're right!! Didn't notice at first, copy-paste issues, I guess. It's corrected now. Thanks!! – FourBars Dec 13 '21 at 11:07