I made some modifications and wrote this now:
Code:
//110101 The 3n + 1 Problem
# include <iostream>
# include <fstream>
using namespace std;
int main()
{
int i,j,k,x,n1,n,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)
{temp=i;i=j;j=temp;}
n=j-i;
int counter[n+1];
for (x=0;x<=n;x++)
counter[x]=1;
x=0;
for (k=i;k<=j;k++)
{
n1=k;
while (n1!=1)
{
if ((n1%2)==0)
{
n1= n1/2;
counter[x]=counter[x]+1;
//x++;
}
else
{
n1= n1*3; n1++;
counter[x]=counter[x]+1;
//x++;
}
}
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);
}
But it still shows "Wrong answer". However, I have got my hands on a program (that I found online) that gives the correct result.
Code:
#include <iostream>
using namespace std;
int length(int n)
{
int i = 1;
while(n != 1)
{
if(n % 2 == 0)
{
n /= 2;
}
else
{
n *= 3;
n += 1;
}
i++;
}
return i;
}
int main()
{
int a, b, low, high;
while(cin>>a>>b)
{
if(a < b)
{
low = a;
high = b;
}
else
{
low = b;
high = a;
}
int max = length(low);
for(int i = low + 1; i <= high; i++) {
int l = length(i);
if(l > max) {
max = l;
}
}
cout<<a<<" "<<b<<" "<<max<<endl;
}
return 0;
}
But I still cannot find the mistake in my program