# Thread: Can you find the error?

1. I have found the error. The mistake is definitely not a compilation or presentation error, as I had mentioned before, There probably is some input for which I may not be getting the right output..

I have found that type of input scenario and corrected my program to handle that case well. Here is a modification of my program that now works Thanks a lot guys!!

Code:
```//110101 The 3n + 1 Problem
# include <iostream>
# include <fstream>
using namespace std;
int main()
{
int i,j,k,x,n1,n,l,h,maxcount,temp=0; /*x=index of counter, n=no fo elements in counter, maxcount is the max element from the counter*/
while (cin>> i>>j)
{
if ((i<1000000) && (i>0) && (j<1000000) && (j>0))
{
if(i > j)
{l=j;h=i;} //l=low; h=high
else
{l=i;h=j;}
n=h-l;
int  counter[n+1];
for (x=0;x<=n;x++)
counter[x]=1;
x=0;
for (k=l;k<=h;k++)
{
n1=k;
while (n1!=1)
{
if ((n1%2)==0)
{
n1= n1/2;
counter[x]=counter[x]+1;
}
else
{
n1= n1*3; n1++;
counter[x]=counter[x]+1;
}
}
x++;
}
maxcount=counter[0];
for (x=1;x<=n;x++)
if (counter[x]>maxcount)
maxcount=counter[x];
cout <<i<<" "<<j<<" "<<maxcount<<"\n";
}
else cout<<"invalid input!";
}
return (0);
}```

Well, yes needless to say, I have rewritten the program with proper functions and definitely much neat appearance (which is not posted here now). But I wanted to find my mistake in this program, hence was behind this program all this while. Thanks a lot for the wonderful help and suggestions!

2. You have yet to fix the VLA. Did you actually listen to feedback?

3. Yes I have rewritten the program with proper functions, without VLA, which is not posted here now. Thanks.

Originally Posted by neeha_khan
Well, yes needless to say, I have rewritten the program with proper functions and definitely much neat appearance (which is not posted here now).

4. Originally Posted by Khabz
As for the problem itself I don't really undertand why are there 2 inputs. Shouldn't it be just 1 so it would give the cycle length from that number until 1?
The Problem statement asks us to compute cycle length of EVERY number between two input numbers. Hence, two inputs.

Originally Posted by Khabz
I haven't managed to compile any of your codes posted here (but the one you looked up online) nor do I understand why they should work.
-Arrays must have a constant size. You may not change it throughout your program.
>int counter[n+1]
This is wrong.
The program compiles perfectly my end and at the judge's end(website) with such arrays. Hence wasn't focusing much on that for this particular program.

Originally Posted by Khabz
Also, the usage of magic numbers isn't healthy programming. You should avoid them, even though I see you fixed it on your latest version.
Yes, fixed that in the latest version. Will use vectors next time . Thanks a lot.

5. Originally Posted by neeha_khan
The program compiles perfectly my end and at the judge's end(website) with such arrays. Hence wasn't focusing much on that for this particular program.
Because you and the website is using a compiler that incorrectly allows non-standard features to compile.

6. Originally Posted by neeha_khan
The program compiles perfectly my end and at the judge's end(website) with such arrays. Hence wasn't focusing much on that for this particular program.
it would literally take you ten seconds to change

Code:
`int  counter[n+1];`
to

Code:
`std::vector<int>  counter(n+1);`
the surrounding program would still function exactly the same, and would be compliant with the standard that everyone accepts. I don't understand why you resist.

Popular pages Recent additions