#include<iostream>
using namespace std;
int rank[1000000];
void main()
{
long a, b, c;
while (cin>>a>>b)
{
//Calculate & Track for max
int max = 0;
for (int i = a; i <= b; i++)
{
if (rank[i] == 0)
rank[i] = findrank(i);
if (max < rank[i])
max = rank[i];
}
//Print out
cout << max;
}
}
int findrank(int& x)
{
int rank, next;
if (x == 1)
{
rank = 0;
}
else
{
if (x % 2 == 0)
next = x/2;
else
next = x*3+1;
if (next > 1000000 || rank[next] == 0)
rank = 1 + findrank[next];
else
rank = 1 + rank[next];
}
if (x < 1000001)
rank[x] = rank;
return rank;
}
Last edited by Laputa on Tue Jan 31, 2006 2:48 am, edited 1 time in total.
laputa.cc:5: error: `main' must return `int'
laputa.cc: In function `int main(...)':
laputa.cc:16: error: `findrank' undeclared (first use this function)
laputa.cc:16: error: (Each undeclared identifier is reported only once for each function it appears in.)
laputa.cc: In function `int findrank(int&)':
laputa.cc:26: error: `int findrank(int&)' used prior to declaration
laputa.cc:39: error: invalid types `int[int]' for array subscript
laputa.cc:40: error: pointer to a function used in arithmetic
laputa.cc:40: error: pointer to a function used in arithmetic
laputa.cc:40: error: invalid conversion from `int (*)(int&)' to `int'
laputa.cc:42: error: invalid types `int[int]' for array subscript
laputa.cc:46: error: invalid types `int[int]' for array subscript
Your rank array is declared in main function and you are trying to access it in your findrank function. Just like in Java, you cannot access another method's local variables. Maybe you would like to describe what you intended to do with this code?
It's my very first try to the 3n+1 problem. I see that I should move the rank arrays into the beginning of the class. Is there anything else I need to fix?
Thank you for all trying. I bring the code up to my tutor and he helped me fixed it. By the way, I know that the code is wrong already. I just didn't know how to fix it. The compiler told me I had these errors already.