-1

I'm kinda new to c++, but have gone deeper in python and c# and I don't remember something like this happening. I'm trying to count how many positive, negative and zeroes are in an array, but the count somehow jumps up to around 4198321, it varies slightly though. So why is this happening and what is causing it to happen? I wasn't able to find any answers online. Code:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main()
{
    long n, pos, neg, zer;
    cin >> n;
    vector<int> arr(n);
    for(int arr_i = 0;arr_i < n;arr_i++){
       cin >> arr[arr_i];
    }
    for(int i; i < n; i++)
    {
        if(arr[i] > 0)
        {
            pos++;
            cout << "pos now is: " << pos << endl;
        }
        else if(arr[i] < 0)
        {
            neg++;
            cout << "neg now is: " << pos << endl;
        }
        else if(arr[i] == 0)
        {
            zer++;
            cout << "zer now is: " << pos << endl;
        }
    }
    cout << "pos: " << pos << endl;
    cout << "neg: " << neg << endl;
    cout << "zer: " << zer << endl;

    return 0;
}

Sorry if I took too long to get to the point, this is the first question I've asked. The output (if needed), is:

neg now is: 4198320
pos now is: 4198321
neg now is: 4198321
zer now is: 4198321
pos now is: 4198322
pos now is: 4198323
pos: 4198323
neg: 4
zer: 1
Caenir
  • 11
  • 2
  • 3
    `pos` is uninitialized.Using it is `UB`. – Gaurav Sehgal Jul 26 '17 at 11:33
  • 1
    Possible duplicate of [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) (Yes, I know that's for C, but it applies here too) Another example if you want: [Uninitialized variable behaviour in C++](https://stackoverflow.com/questions/30172416/uninitialized-variable-behaviour-in-c) Suffice it to say this has been discussed to death and does not warrant another question; try reading basic teaching materials. code full of unitialised variables => UB – underscore_d Jul 26 '17 at 11:35
  • @underscore_d It misses the nuances on C++. Like if the object has a constructor it will be default constructed, – NathanOliver Jul 26 '17 at 11:37
  • @NathanOliver Fair point, albeit not applicable to basic types like these. I'm sure there's a better dupe out there for C++! – underscore_d Jul 26 '17 at 11:38
  • See also: https://stackoverflow.com/questions/6032638/default-variable-value / https://stackoverflow.com/questions/40987901/what-happens-to-uninitialized-variables-c / probably many more – underscore_d Jul 26 '17 at 11:41

2 Answers2

0

In for(int i; i < n; i++), the variable i is not initialized.

The rest is undefined behavior.

goodvibration
  • 5,980
  • 4
  • 28
  • 61
  • I didn't need to initialize i, it worked having the for loop the same – Caenir Jul 27 '17 at 08:59
  • Still undefined behavior. I wouldn't bet on that to work on every platform. In fact, I wouldn't bet on that to work even on the same platform, using different project settings. For example, try to build and run your project in Release mode instead of Debug mode. You can ignore the call dude, but variables need to be initialized before used!!! – goodvibration Jul 27 '17 at 09:42
  • Understood. I will remember that for the future. Thanks! – Caenir Jul 28 '17 at 03:53
0

The easiest thing you could have tried was printing pos before counting numbers. That should have been zero, but you didn't actually set pos to zero. You got some random value instead, whatever was in that memory location before.

Formally speaking, this is Undefined Behavior, and you might have gotten "lemon" as a result.

MSalters
  • 173,980
  • 10
  • 155
  • 350