Thread: Finding the highest number

1. Senior Member Join Date
Nov 2008
Posts
105
Rep Power
0 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);

}  Reply With Quote

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

3. Use another dummy value and compare it with all calculated num1 to find the max.  Reply With Quote

4. 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);
}  Reply With Quote

5. Senior Member Join Date
Nov 2008
Posts
105
Rep Power
0 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.  Reply With Quote

6. 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.  Reply With Quote

7. Senior Member Join Date
Nov 2008
Posts
105
Rep Power
0 Alright I think I actually get it now, thanks for your help.  Reply With Quote

8.   Reply With Quote