Results 1 to 8 of 8
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Finding the highest number

    I am stooped, I can't figure out how to do this without totally exploding my code.

    Here is what I have to do

    Your job for this assignment is to write a one class program that generates a sequence of the so-called hailstone numbers, and report certain statistics on the sequence your program generates. The hailstone numbers can be described succinctly as follows: Start with a positive integer; if that number is even, divide it in half, and continue; if the number is odd, triple it and add 1, and continue. Proceed in this way until you arrive at the value 1.

    For example, if you start with 20, this is the sequence you get: 20,10,5,16,8,4,2,1. If you start with 21, you get this sequence: 21, 64, 32,16, 8, 4, 2, 1.

    Why are these called hailstone numbers? There's an explanation here, and also in the January 1984 issue of Scientific American.

    Here are your precise requirements for the assignment: in one class, write a program that accepts a positive integer input from the keyboard. This is the starting value for your sequence. Then your code should generate and print out, in a column, hailstone numbers until the value 1 is reached. After 1 has been reached, your program should print out: 1) the largest value appearing in the sequence; and 2) the length of the sequence you generated.

    For example, here is the output for the sequence that begins with the seed value 21:

    Welcome to DrJava.
    > java Hail
    Enter a positive hailstone starting value
    21

    21
    64
    32
    16
    8
    4
    2
    1
    start: 21
    term count: 8
    biggest: 64
    Took me like 10 minutes to make it, except I can't think of how to get the highest number in here.

    Here's what mine looks like, can anyone give suggestions or something? Thanks

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer");
    int num10;
    int m=1;
    int num1 = scan.nextInt();
    int numstart= num1;
    while (num1!=1) {

    m++;
    if (num1 %2==0) {
    num1=num1/2;
    System.out.println(num1);

    } else if(num1%2==1) {
    num1=num1*3+1;
    System.out.println(num1);

    } else {
    if(num1==1)

    ;
    }

    }

    System.out.println("Start Number=" + numstart);
    System.out.println("High Number=");
    System.out.println("Loops=" + m);


    }

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    you can insert all got hailstone numbers in the array and then to figure out which is the highest among them.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Here is a very easiest way. Think about the logic first lol, before implementing. Start from the very basis thing what you know.

    Java Code:
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter an integer");
            int num10;
            int m = 1;
            int max = 0;
            int num1 = scan.nextInt();
            int numstart = num1;
        
            while (num1!=1) {
                m++;
                if (num1 %2==0) {
                    num1=num1/2;
                    System.out.println(num1);
                } 
                else if(num1%2==1) {
                    num1=num1*3+1;
                    System.out.println(num1);
                } 
                else {
                    if(num1==1)
                        ;
                }
                
                // Finding the max
                if(max < num1)
                    max = num1;
            }
            System.out.println("Start Number=" + numstart);
            System.out.println("High Number=" + max);
            System.out.println("Loops=" + m);
        }

  5. #5
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Thank you, you are like the awesomest person ever, but I am still a tad unsure of what that just did, at the end of the while loop, you said that if 0<num1
    the max = num1?
    I am still a bit unclear, could you explain it a bit more, thanks again.
    I have a feeling it's quite simple, but I always mess up on simple things.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I'll explain it bit more.

    Your requirement is finding the maximum number out of few values/numbers.

    So you need a variable to hold the maximum. Since you are looking an int value,

    Java Code:
    int max;
    But you have to initialize a local variable before use, so the above line change as follows.

    Java Code:
    int max = 0;
    All the calculated values are positive and so the minimum value you can have is zero.

    Then in the if and if-else of the while loop you are calculate the result, num1. Then at the end what you have to do is, compare it with the max value. If the value max hold currently is greater than max not change, else max is the newly calculated value, num1.

  7. #7
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Alright I think I actually get it now, thanks for your help.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  2. Replies: 21
    Last Post: 11-05-2008, 02:31 PM
  3. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  4. The highest number
    By I-Shine in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 05:05 AM
  5. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM

Posting Permissions

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