# A really hard question .... NEED HELP ... !!!!

hi everyone ....
i got that problem that i coded but it doesn't give me the same that the sample output is giving ....
here is my question ......

==========================

Consider the following algorithm to generate a sequence of numbers.
Start with an integer n. If n is even, divide by 2. If n is odd, multiply
by 3 and add 1. Repeat this process with the new value of n, terminating
when n = 1.
For example, the following sequence of numbers will be generated for n = 22:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

For an input n, the cycle-length of n is the number of numbers generated up
to and including the 1. In the example above, the cycle length of 22 is 16.
Given any two numbers i and j, you are to determine the maximum cycle length
over all numbers between i and j, including both endpoints.

Input
The input will consist of a series of pairs of integers i and j, one pair of
integers per line. All integers will be less than 1,000,000 and greater than 0.

Output
For each pair of input integers i and j, output i, j in the same order in which
they appeared in the input and then the maximum cycle length for integers between
and including i and j. These three numbers should be separated by one space,
with all three numbers on one line and with one line of output for each line of input.

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

=========================================

// my code and plz tell me what am i doing wrong

#include <iostream.h>
#include <math.h>
#include <stdlib.h>

int test (int, int &);
void end (int);

void main ()

{
int i,j;            // the cycle-length
int x=0, y=0;      // the changes on "i" and "j"
int counter=0;
int count=0;

do
{

cin >> i ;
cin >> j;

}while (i < 0 || i > 1000000 || j < 0 || j > 1000000);

x=i;
y=j;

do
{
test(x, counter);

if (counter==1)
{
x=(x*3)+1;
count++;

}

else if (counter==2)
{
x=(x/2);
count++;
}

}while (x != 1);

do
{
test(y, counter);

if (counter==1)
{
y=(y*3)+1;
count++;

}

else if (counter==2)
{
y=(y/2);
count++;
}

}while ( y != 1);

end (count);

}

int test (int a, int &cntr)
{
if (a%2 != 0)
return cntr=1;

else if (a%2 == 0)
return cntr=2;

else
return cntr=0;
}

void end (int count)
{
cout << count << endl;
exit(0);
}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hey, I typed a whole bunch of explanation only to find that my link was broken :( Your effort is pretty good for a beginer: I am now typing the solution in notepad and will get back as soon as I am finished typing :)
...Snehanshu
Commented:
FYI:
<iostream.h> is not part of the C++ standard.

You should use <iostream> instead.
Commented:
Ah, the Collatz problem...

Problem statement : find the length of the longest "trajectory" for the numbers between X and Y

This is homework obviously, so I'll only give the pseudo-code

set MAX to 0
for each I going from X to Y
compute the length L of the trajectory for I
if L is greater than MAX
set MAX = L
end if
end for
Commented:
I changed 2 functions from your code for
//generateSequence and return the the cycle-length
int generateSequence(int x)
{
int count=0;
int origX = x;
do
{
count++;
cout << x << ' ';
if (testEven(x))
{
x=(x/2);
}
else
{
x=(x*3)+1;
}
}while (x != 1);

//include and print 1
count++;
cout << '1' << endl;
cout << origX << ' ' << count << endl;
return count;
}

//test if even
int testEven (int a)
{
return (a%2 == 0);
}
I hope, you find these functions useful
------------------------------------------------------------

PS: it is not a good idea to use exit(0) in the end-function, like this:
void end (int count)
{
cout << count << endl;
exit(0);
}

it is better to use return 0; in the main-function
int main (int argc, char* argv[])
{
....
cout << i << ' ' << j << ' ' << count << endl;
......
return (0);
}

Commented:
One error in your code is you are finding only the sequences for i and j not for numbers inbetween. You could add a for loop to increment the starting number from i to j and inside it use your do {} while sequence.
Commented:
As I said earlier, your effort looks good for a beginer.
Looks like you did not understand understand the question well.
For a given set of numbers, you need to find the maximum series length of all numbers that lie in between.
So, suppose your iputs are
1,5,
then you must calculate series lengths of 1(1), 2(2), 3(8), 4(3) and 5(6)
and then display the maximum length (i.e. 8)
So, your output will be
1 5 8            <------- (RIGHT)
while what you do is add length of 1 (1) and 5 (6) so your output would be
1 5 7 <------- (WRONG)

You would need to add a loop that iterates from i to j and calculate series length of each.
something like

MaxLen = 0;
for(x=i;x<j+1;x++)
{
tmp = getlen(i);
if(tmp>maxlen)
tmaxlen = tmp;
}

cout << i << j << maxlen;

getlen would have code similar to your while loops and would return the count you calculate now.

Since this is homework, I won't provide any more help. You can ask if you are stuck with a specific difficulty, but looking at your initial effort, I don't think you'll need more help :)
Good luck,
...Snehanshu

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
This is a perfect problem to use recursion.  Here is the code to figure the cycleLength recursively.  This will teach you a lot, but be warned, your teacher will grill you on this if you use it, so understand it well :-> ..

int cycleLength(int x)
{
if(x & 0x01 && x > 1) { return cycleLength(x*3 +1); }
else if (x == 1) { return 1; }
else { return cycleLength(x/2);}
}

Commented:
correction to recursive code above:

int cycleLength(int x)
{
if(x & 0x01 && x > 1) { return cycleLength(x*3 +1) +1; }
else if (x == 1) { return 1; }
else { return cycleLength(x/2) + 1;}
}
Author Commented:
thx all of u guys for helping me ......

and special thanks for snehanshu .... thx dude ....
Commented:
My pleasure.

...Snehanshu
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.