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