Results 1 to 8 of 8
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Need help with program for Hailstone Sequence

    Java Code:
    import java.util.*;
    
    public class HailStone
    {
      public static void main(String[] args)
      {
        //create scanner object and prompt for input of values
    
        Scanner input = new Scanner(System.in);
       
        System.out.print("Enter starting number of the range: ");
          int lo = input.nextInt();
    	
        System.out.print("Enter ending number of the range: ");
          int hi = input.nextInt();
    	
       //check if both no.s ve+ indivly and if the first value is > second one
    
           
       do
        {
          if(lo < 0 || hi < 0)
          System.out.println("Invalid! Remember numbers must be be possitive and in the right order.");
         while (lo < 0)
          {
          System.out.println("Try again! Improper range value.");
          System.out.print("Enter starting number of the range: ");
           lo = input.nextInt();
          }
    
         while (hi < 0)
          { 
          System.out.println("Try again! Improper range value.");
          System.out.print("Enter ending number of the range: ");
           hi = input.nextInt();
          }
       } while (lo < 0 | hi < 0);
     
      if(lo > hi) 
       {
         System.out.println("Try again! First value must be lower than second value.");
         System.out.print("Enter starting number of the range: ");
         lo = input.nextInt();
         System.out.print("Enter ending number of the range: ");
         hi = input.nextInt();
       }
      int num_withHiCycles = 0;
      int cycleLength = 0;
      int numOfCycles = 0;
      for(int num = lo; num <= hi; num++)
       {
         int cycleLength_orgi = cycleLength;
          int even = 0;
          int odd = 1;
          int num_EO_check = num % 2 ;
         if( num_EO_check == even )
          {  
            do
             {
               num = num / 2; 
              
               if (num % 2 == even)
                {
                  num = num / 2;
                }
               else if ( num % 2 == odd)
                {
                  num = 3 * num + 1;
                }
     
               ++numOfCycles;
              if(numOfCycles > cycleLength)
                {
                  cycleLength = numOfCycles;
                  num_withHiCycles = num; 
                }
    
                  
             } while (num > 1);
            
          }
         
         else if( num_EO_check == odd)
          {
            do
             {
               num = 3 * num + 1;
              
               if (num % 2 == even)
                {
                  num = num / 2;
                }
               else if ( num % 2 == odd)
                {
                  num = 3 * num + 1;
                } 
    
                ++numOfCycles;
               if(numOfCycles > cycleLength)
                {
                  cycleLength = numOfCycles;
                  num_withHiCycles = num; 
                }
    
             
             } while (num > 1);
          }     
               
    
       }
    
          System.out.print("The number "+ num_withHiCycles + " has the longest cycle length of " + cycleLength + ".");
    
     } 
    }
    Problem: I get no output. It compiles but after I put in the range values it doesn't show anything, it goes to the next line and the cursor appears. Thats it. Have I created an non terminating loop? Or is my algorithm wrong?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with program for Hailstone Sequence

    I get no output.
    Add some more calls to println() that print out messages to show the execution flow and the values of the variables that are controlling the execution flow, like num and num_EO_check

    Also add an ending else following the else if at line 96 and print out a message about what happened that caused the else to be executed.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need help with program for Hailstone Sequence

    Norm,

    I tried what you said and in a way figured out that my algorithm was wrong. So I've been trying to figure it out by just looking at the second half of the program where the actual calculations are done and displayed. I keep getting an error: "reached end of file while parsing" at the moment and I cant figure out why. The code looks right to me.

    Java Code:
    public class HailStone_SecondHalf
    {
      public static void main(String[] args)
      {
    
      int num; 
     for(num = 1; num <= 5; num++)
       {
          int num_withHiCycles = 0;
          int cycleLength = 0;
          int numOfCycles = 0;
              
          int even = 0;
          int odd = 1;
          int num_EO_check = num % 2;
    
         if( num_EO_check == even )
          {  
            do
             {
               num = num / 2; 
              
               if (num % 2 == 0)
                {
                  num = num / 2;
                  ++numOfCycles;
                }
               else if ( num % 2 == 1)
                {
                  num = 3 * num + 1;
                  ++numOfCycles;
                }
     
               ++numOfCycles;
              if(numOfCycles > cycleLength)
                {
                  cycleLength = numOfCycles;
                  num_withHiCycles = num; 
                }
    
                  
             } while (num > 1);
            
          }
         
         else if( num_EO_check == odd)
          {
            do
             {
               num = 3 * num + 1;  
              
               if (num % 2 == 0)
                {
                  num = num / 2;
                  ++numOfCycles;
                }
               else if ( num % 2 == 1)
                {
                  num = 3 * num + 1;
                  ++numOfCycles;
                } 
    
                ++numOfCycles;
               if(numOfCycles > cycleLength)
                {
                  cycleLength = numOfCycles;
                  num_withHiCycles = num; 
                }
    
             
             } while (num > 1);
          }
         
          System.out.print(num + numOfCycles );
          System.out.print("The number "+ num_withHiCycles + " has the longest cycle length of " + cycleLength + ".");
     }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with program for Hailstone Sequence

    getting an error: "reached end of file while parsing"
    Check that all the { have a pairing }
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need help with program for Hailstone Sequence

    I did that, that is why I was concluding that it looked right to me.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with program for Hailstone Sequence

    The compiler is usually correct.
    One technique is to print the source and use a pencil to circle and draw lines connecting pairs of {}. Start at an inner most pair and work outwards.


    Another technique when writing code is to add a comment on the line with the } saying what it is ending.
    Java Code:
           }  // end for(i)
       }  // end main()
    } // end class
    Last edited by Norm; 02-22-2013 at 01:30 AM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Need help with program for Hailstone Sequence

    I resolved this problem. I appreciate your help. Could you help me furthermore? I cant figure out what I am doing wrong with the algorithm for the Hailstone Sequence overall.

    here is the question: [LINK]http://www.java-forums.org/new-java/69187-helppp.html#post323416[/LINK]

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with program for Hailstone Sequence

    Sorry, I don't know anything about the algorithm.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need help with program for Hailstone Sequence
    By abi in forum New To Java
    Replies: 2
    Last Post: 02-21-2013, 07:08 AM
  2. Replies: 1
    Last Post: 01-23-2013, 06:06 PM
  3. Replies: 0
    Last Post: 04-19-2010, 06:40 PM
  4. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •