Solved

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

Posted on 2003-10-24
265 Views
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);
}
0
Question by:hackermen5

LVL 5

Expert Comment

ID: 9613073
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
0

LVL 30

Expert Comment

ID: 9613161
FYI:
<iostream.h> is not part of the C++ standard.

0

LVL 16

Assisted Solution

_nn_ earned 50 total points
ID: 9613210
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
0

LVL 1

Assisted Solution

fsign21 earned 100 total points
ID: 9613502
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);
}

0

LVL 13

Expert Comment

ID: 9613586
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.
0

LVL 5

Accepted Solution

snehanshu earned 350 total points
ID: 9613752
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.
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)
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

0

LVL 5

Expert Comment

ID: 9616950
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);}
}

0

LVL 5

Expert Comment

ID: 9616968
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;}
}
0

LVL 1

Author Comment

ID: 9621879
thx all of u guys for helping me ......

and special thanks for snehanshu .... thx dude ....
0

LVL 5

Expert Comment

ID: 9622127
My pleasure.

...Snehanshu
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more adâ€¦
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.htâ€¦
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.