Hello I am attempting to perform the hailstone sequence.
A hailstone sequence is basically: take a given integer n - if even, the next integer in the sequence is n/2, if odd, the next integer in sequence is n * 3 + 1.
The API I must follow for my assignment requires it to be performed as it is with a method returning an arraylist.
My problem is the code just hangs forever, when I added output in the method itself to see what was happening I see it always hangs when it is given the number 10 for some reason.
I am hoping that there is something small I am missing here perhaps in my conditions.
Here is some sample output when given n value of 15 it outputs this over and over again.
15 is odd so I make it 3n+1: 46
46 is even so I divide by 2: 23
23 is odd so I make it 3n+1: 70
70 is even so I divide by 2: 35
35 is odd so I make it 3n+1: 106
106 is even so I divide by 2: 53
53 is odd so I make it 3n+1: 160
160 is even so I divide by 2: 80
80 is even so I divide by 2: 40
40 is even so I divide by 2: 20
20 is even so I divide by 2: 10
15 is odd so I make it 3n+1: 46
My code
import java.util.ArrayList;
import java.util.Scanner;
public class HailstoneSequence {
public static ArrayList<Integer> getHailstoneSequence(int n){
ArrayList<Integer> results;
results = new ArrayList<Integer>();
results.add(n);
//while the last number is not 1 perform these actions
while((results.size() - 1) != 1){
//for each number in the array
for(int i=0; i< results.get(i); i++){
//test if odd or even
if((results.get(i)%2)==0){
System.out.println(results.get(i)+" is even so I divide by 2: "+ (results.get(i)/2));
results.add((results.get(i)/2));
}
else{
//odd
System.out.println(results.get(i)+" is odd so I make it 3n+1: "+ (3*(results.get(i))+1));
results.add((3*(results.get(i))+1));
}
}
}
return results;
}
public static void main(String[] args) {
int n=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n ");
n=sc.nextInt();
sc.close();
//create an initialize new array list to hold results of the hailstonesequence
ArrayList<Integer> list;
list = new ArrayList<Integer>();
list = getHailstoneSequence(n);
//for each number in the array
for(int i=0; i< list.get(i); i++){
if ((list.get(i)!= 1)){
if((list.get(i)%2)==0){
System.out.println(list.get(i)+" is even so I divide by 2: "+ (list.get(i+1)));
}
else{
//odd
System.out.println(list.get(i)+" is odd so I make it 3n+1: "+ (list.get(i+1)));
}
}
else{break;}
}
}
}
In your method for(int i=0; i< results.get(i); i++){ and in main for(int i=0; i< list.get(i); i++){
These do not loops over each element of the list, or at least not only once, and it'll eventually result in out of bounds if you never added to the list.
Say results.get(i) is 10, and that's the only number in the list... You then add 5 ten times because 10 is even and the loop is running ten times. You then are probably adding 16 5*10 times, etc, etc
Adding elements to lists while you iterate over them is generally a bad idea anyway. You only need to keep track of two numbers at a time, and can add to the list separate from the iteration process.
Here's a working sample
ArrayList<Integer> results = new ArrayList<Integer>();
results.add(n);
if (n == 1) return results;
int next;
if (n % 2 == 0) next = n / 2;
else next = 3*n + 1;
results.add(next);
while (next != 1) {
if (next % 2 == 0) next = next / 2;
else next = 3*next + 1;
results.add(next);
}
return results;
So I have an array in java that looks like this:
int[] theArray = {2,3,6,9,10,12,17,16,18,20,23,24,28,30,31};
Desired output:
2 3 6 9 10
12 17 16 18 20
23 24 28 30 31
for(int i =0;i<theArray.length;i++)
{
if(i%5==0 && i!=0)
{System.out.println();
}
System.out.print(theArray[i]+" ");
}
All you have to do is go through the array one by one, and use System.out.print to print the elements for each set of 5 elements. Once you have printed 5 elements, do a system.out.println("");
So, basically you want to output the values in an array with a newline after every fifth element? You can use the modulo operator to achieve that.
In (untested) code:
for (int i = 0; i < arrayWithNumbers.length; i++)
{
if (i % 5 == 0 && i != 0) // end of the line
{
System.out.println(arrayWithNumbers[i]);
}
else
{
System.out.print(arrayWithNumbers[i]);
}
}
I have a counter variable and for every 3 input in the range from 1-5 I need to add an Asterisk to show the total. So if 7 answers in the range 1-5 occurred, it would only print out 2 Asterisks. I need to somehow take the counter total, put it in a loop I'm assuming and for every 3, add the asterisks to the total below.
if(value >= 1 && value <= 5){
counter++;
}
The print out would look something like this
Answers from 1 - 5: ** // from 7 in this range
Answers from 1 - 5: *** // from 9 in this range
This should give the correct result.
int value = counter / 3;
System.out.print("Answers from 1 - 5: ");
for (int i = 0; i < value; i++)
System.out.print("*");
System.out.println();
end should give you the correct result.
Given that the number you want is an integer it will store it as a whole number (e.g. 7/3 = 2). You can use System.out.print() to start the line, print on the same line as many asterisks you need and then end the line.
Hope this helps.
So here are my assignment requirements:
Print out all the numbers between 1 and 100 inclusive, 10 numbers per line evenly spaced out using tabs.
Use a max of 2 loops and 1 if statement.
I have a pretty good understanding of how this needs to be done but I'm just having trouble figuring out a way to start a new line after every 10 numbers.
This is what I have so far:
import java.util.Scanner;
public class Table {
public static void main (String[] args) {
int counter, value;
counter = 1;
value = 0;
while (value < 100) {
value += counter;
System.out.print(value + "\t");
}
}
}
To start a new line, print the '\n' character.
if ((value % 10) == 0) {
System.out.print("\n"); // Or really, just System.out.println();, since that makes a new line.
}
There are two ways to approach this:
Work through each row in your table, and for each of these print each of the numbers x1, x2, x3, x4, ..., x + 10 followed by a newline character.
Work through all of the numbers individually, and after each one check to see if it's a round number of tens (think modulo arithmetic), and if is, follow it with a new line character.
Since you already know the span of numbers you need to iterate over, I'd use a for loop, starting at 0 and ending at < 100.
You're missing the increment, counter++ within your loop.
Use an if statement within the for loop, if (i % 10 = 0) {System.out.println(value + "/n";}, else System.out.println(value + " ");
Best of luck on your assignment.
I am new to Programming so bear with me if I do not properly present my issue. I have an assignment to create a program to assign integer values 1-25 to a 25 element integer array. I need to print the array as five separate lines with each line containing 5 elements separated by commas. The last element does not have a comma following it. The final output should be as follows:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
The code that I came up with comes close, but it's not quite right. The code that I came up with is:
public class Test2 {
/**
* #param args
* the command line arguments
*/
public static void main(String[] args) {
int[] numbers = new int[25];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
System.out.print(numbers[i] + ",");
if (i % 5 == 0 && i > 0)
System.out.println();
}
}
}
The printout that I get from my code is as follows:
1,2,3,4,5,6,
7,8,9,10,11,
12,13,14,15,16,
17,18,19,20,21,
22,23,24,25,
I am not sure why I am getting 1-6 on the first line as well as how to remove the comma at the end of each line. Any help pointing out my errors would be appreciated.
The error is that you are checking if int i is divisible by 5 (i % 5), not numbers[i] (numbers[i] % 5). This way, your code prints:
number 1 when i = 0,
number 2 when i = 1,
number 3 when i = 2,
number 4 when i = 3,
number 5 when i = 4,
number 6 when i = 5
and finally prints line break.
The correct code is:
int[] numbers = new int[25];
for (int i = 0; i < numbers.length; i++) {
numbers[i]=i+1;
System.out.print(numbers[i]);
if (numbers[i] % 5 == 0 && i > 0) {
System.out.println();
} else {
System.out.print(",");
}
}
The above code will print (as intended):
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
You're getting 6 numbers on the first line, because you start counting at i=0, and only print the newline once i=5; at which point the number you've just printed is 6, not 5 - you're printing i+1 in each iteration.
If you made your logic such that it printed EITHER a comma OR a newline but not both, you'd get rid of the commas at the ends of the lines.
You're close. Very close.
Consider what your condition is checking - you want to inspect a value i, and want to stop when that particular value is divisible by 5 but is nonzero.
The problem is that you have the wrong value - i isn't what you want, but numbers[i]. The reason: each number in numbers[i] is offset of i by 1.
What you want to do is check if numbers[i] is divisible by 5. You still need to check for a nonzero i, though.
if(numbers[i] % 5 == 0 && i > 0) {
System.out.println(numbers[i]);
} else {
System.out.print(numbers[i] + ",");
}
Replace
if (i % 5 == 0 && i > 0)
with
if (i % 5 == 4)