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!