-5

First program:

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

int main() {
int n,taken,i,j,ans=0,t,num,k;
cin>>n>>t;
vector<int>a(n);

for(i=0;i<n;++i)
    cin>>a[i];

i=0;j=0;    
for(;i<n;++i)
{
    if(i>j)
    {
        j=i;
        k=0;
        taken=0;
    }

    for(;j<n;++j)
    {
        if(taken+a[j]<=t)
        {
            taken+=a[j];
            k++;
        }
        else
            break;
    }
    ans=max(ans,k);
    taken-=a[i];
    k--;
}

cout<<ans;
return 0;
}

Second program:

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

int main() {
int n,taken,i,j,ans=0,t,num,k;
cin>>n>>t;
vector<int>a(n);

for(i=0;i<n;++i)
    cin>>a[i];


for(i=0;i<n;++i)
{
    if(i>j)
    {
        j=i;
        k=0;
        taken=0;
    }

    for(j=0;j<n;++j)
    {
        if(taken+a[j]<=t)
        {
            taken+=a[j];
            k++;
        }
        else
            break;
    }
    ans=max(ans,k);
    taken-=a[i];
    k--;
}

cout<<ans;
return 0;

}

it seems these 2 programs give 2 diffrent outputs,here is a testcase to check it out yourself: 4 5 3 1 2 1

the first one outputs "3" while the second outputs "1991243264 " on codeforces tests while the 2 programs give the same output on ideone..any help?

BaherZ
  • 69
  • 1
  • 1
  • 6
  • 9
    Why do you `#include `? Who's teaching this?? :( – Lightness Races in Orbit Jul 16 '15 at 14:11
  • 2
    ``, `using namespace std;`, and a bunch of early declarations. Ouch. – Quentin Jul 16 '15 at 14:17
  • 1
    @LightnessRacesinOrbit This [Google result](http://codeforces.com/blog/entry/8387) made me cringe. And I don't think it being from CodeForces too is a coïncidence. – Quentin Jul 16 '15 at 14:18
  • I have seen this trend in competitive programming that people include `bits/stdc++.h` so that they don't have to care whether they have included the proper header file or not.I am not a supporter of it. – Gaurav Sehgal Jul 16 '15 at 14:19
  • it's a habit to save time during a contest..i usually write this now even if i don't need to include many headers to solve a specific problem,as for the `using namespace std` part...when i first learned c++ i learned it this way so it's kind of a habit – BaherZ Jul 16 '15 at 14:28

3 Answers3

1

In the first program you are setting the values of i and j outside the for loops and then only changing j inside the if statement in the outer for loop.

//...
if(i>j) 
{
    j=i; //<- set j here
    k=0;
    taken=0;
}

for(;j<n;++j) //<- using j from above
{
//...

In your second piece of code you are resetting j to zero each time the for loop is ran. You also never set j to a value before you enter the first for loop so when you use j in your if statement it will invoke undefined behavior.

//...
if(i>j) // j is uninitialized here so you could get anything
{
    j=i; //<- set j here
    k=0;
    taken=0;
}

for(j=0;j<n;++j) //<- set j to zero here
{
//...
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Just a technical/nitpick question. Isn't using an uninitialized int is unspecified behavior (https://en.wikipedia.org/wiki/Unspecified_behavior) instead of undefined behavior? – A.J. Jul 16 '15 at 14:31
  • @user3282085 I am pretty sure this falls under the UB banner: http://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c – NathanOliver Jul 16 '15 at 14:35
0

The second program invokes undefined behavior, since you read the value of j in the first iteration of the outer for-loop without first initializing it. The general result of this type of UB is reading whatever random data happened to be in that stack location... and that depends on a lot of factors, and is likely to differ between environments.

There's also a bunch of other stuff wrong in those programs. But purely in terms of why it's giving you different results.... that's why.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
0

In the second program j is uninitialized.

for(i=0;i<n;++i)
{
   if(i>j)//j is uninitialized here
    {
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34