0
#include<bits/stdc++.h>
using namespace std;

int arr[]={1,2,3,4,5};
int r=3;

int rec(int index,int cnt,vector<int> v)
{
    if(cnt==r)//if required no is reach
    {
        cout<<"IN ";
        for(int i=0;i<v.size();i++)
        {
            cout<<v[i]<<" ";
        }
        cout<<endl;
        return 1;
    }
    if(index>=5)//crossing the array
    {
        return 0;
    }

    int a;

    vector<int> temp;
    temp=v;
    temp.push_back(arr[index]);

    a=rec(index+1,cnt+1,temp)+rec(index+1,cnt,v);

    return a;
}

int main()
{
    vector<int> v;
    int cnt=0;
    int a=rec(0,cnt,v);
    cout<<"a="<<a<<endl;
    return 0;
}

```either select the element at the given index and increase the cnt or i dont select the element.In both cases I move forward.The total no of ways in which it can happen is stored in variable a.
(1,2,3) is the same as (2,1,3) or(3,2,1)
I am getting different outputs<br>
CASE-1

a=rec(index+1,cnt++,temp)+rec(index+1,cnt,v);

OUTPUT

IN 1 2 3 IN 1 2 4 IN 1 2 5 IN 1 3 4 IN 1 3 5 IN 1 4 5 IN 2 3 4 IN 2 3 5 IN 2 4 5 IN 3 4 5 a=10

CASE-2

a=rec(index+1,cnt+1,temp)+rec(index+1,cnt,v);

OUTPUT

IN 1 2 IN 1 3 IN 1 4 IN 1 IN 2 3 IN 2 4 IN 2 IN 3 4 IN 3 IN a=10


I get different outputs for cnt++ and cnt+1 though both are the same

Why is this happening
Aastha
  • 41
  • 8
  • 4
    That's because `cnt++` increases `cnt` and returns the non-increased value whereas `cnt+1` doesn't increase `cnt` but returns a value `1` greater than `cnt`. They're completely different. – Blaze Oct 23 '19 at 14:34
  • 2
    You get different output because you run different code. The two expressions are not at all equivalent. Furthermore, `rec(index+1,cnt++,temp)+rec(index+1,cnt,v)` exhibits undefined behavior, by way of having an access to and a modification of the same variable that are unsequenced. – Igor Tandetnik Oct 23 '19 at 14:35

3 Answers3

1

Order of evaluation is unspecified unsequenced for:

rec(index + 1, cnt++, temp) + rec(index + 1, cnt, v);
  • cnt++ [A]
  • rec(index + 1, cnt, temp) [B]
  • rec(index + 1, cnt, v) [C]

[A] should happen before [B].

And:

If a side effect on a scalar object is unsequenced relative to a value computation using the value of the same scalar object, the behavior is undefined.

You read [C] and write cnt [A] in unsequenced, you have UB.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

the reason is the same here

see the warning : main.cpp:39:21: warning: operation on 'cnt' may be undefined [-Wsequence-point]

consider your code as following:

#include <iostream>
#include <vector>
using namespace std;

int arr[] = { 1,2,3,4,5 };
int r = 3;

static int callCounter = 0;

int rec(int index, int cnt, vector<int> v)
{
    std::cout << "1+cnt = "<< 1 + cnt << endl;

    if (cnt == r)//if required no is reach
    {
        cout << "IN ";
        for (int i = 0; i < v.size(); i++)
        {
            cout << v[i] << " ";
        }
        cout << endl;
        return 1;
    }
    if (index >= 5)//crossing the array
    {
        return 0;
    }

    int a;

    vector<int> temp;
    temp = v;
    temp.push_back(arr[index]);

    //int firstCall = rec(index + 1, cnt, v);
    //int secondCall = rec(index + 1, ++cnt, temp);
    //a = secondCall + firstCall;

    `enter code here`a = rec(index + 1, ++cnt, temp) + rec(index + 1, cnt, v);
     //a = rec(index + 1, 1+cnt, temp) + rec(index + 1, cnt, v);
    ++callCounter;
    std::cout << "callCounter " << callCounter <<endl;
    return a;
}

int main()
{
    vector<int> v;
    int cnt = 0;
    int a = rec(0, cnt, v);
    cout << "a=" << a << endl;

    return 0;
}

the out put from GCC 9.2 is:

main.cpp: In function 'int rec(int, int, std::vector)':

main.cpp:17:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector::size_type' {aka 'long unsigned int'} [-Wsign-compare]

17 | for (int i = 0; i < v.size(); i++)

main.cpp:39:21: warning: operation on 'cnt' may be undefined [-Wsequence-point]

39 | a = rec(index + 1, ++cnt, temp) + rec(index + 1, cnt, v);

abdulrhmanOmran
  • 111
  • 1
  • 7
0

Because cnt++ is using the postfix increment operator, it will increment the local variable cnt after it is passed to the function rather than before. Try prefix operator ++cnt instead.

voxoid
  • 1,164
  • 1
  • 14
  • 31