Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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);

}

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);

}

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

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.

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

//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);

}

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.

Start your 7-day free trialint cycleLength(int x)

{

if(x & 0x01 && x > 1) { return cycleLength(x*3 +1); }

else if (x == 1) { return 1; }

else { return cycleLength(x/2);}

}

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;}

}

C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

...Snehanshu