Solved

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

Posted on 2003-10-24
10
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

718 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