Solved

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

Posted on 2003-10-24
10
266 Views
Last Modified: 2010-04-02
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
Comment
Question by:hackermen5
10 Comments
 
LVL 5

Expert Comment

by:snehanshu
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

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

You should use <iostream> instead.
0
 
LVL 16

Assisted Solution

by:_nn_
_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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Assisted Solution

by:fsign21
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

by:SteH
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

by:
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.
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
 
0
 
LVL 5

Expert Comment

by:BlackDiamond
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

by:BlackDiamond
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

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

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

Expert Comment

by:snehanshu
ID: 9622127
My pleasure.

...Snehanshu
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question