# Thread: Deision makiing and loop control

1. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Deision makiing and loop control

Hey guys i just got a program for class that i have to do and it involves decision making, instead of trying to explain what it is ill just copy and past the document here """Program 230b
(Ulam hypothesis)

Program Description: The mathematician Stanislau Ulam of the University of Colorado hypothesized that any positive integer would always converge to 1 if treated as follows

If it is odd, multiply it by three and add 1
If it is even, divide it by 2

The procedure is then applied to the result of each calculation. For example, starting with eleven, the following is produced:

11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

Write a program that tests this theory and shows the output for all numbers from 1 to 25.

Required Statements: output, loop control, decision making

Sample output:
1: 4 2 1
2: 1
3: 10 5 16 8 4 2 1
4: 2 1
5: 16 8 4 2 1
6: 3 10 5 16 8 4 2 1
7: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
8: 4 2 1
9: 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
10: 5 16 8 4 2 1
11: 34 17 52 26 13 40 20 10 5 16 8 4 2 1
12: 6 3 10 5 16 8 4 2 1
13: 40 20 10 5 16 8 4 2 1
14: 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
15: 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
16: 8 4 2 1
17: 52 26 13 40 20 10 5 16 8 4 2 1
18: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
19: 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
20: 10 5 16 8 4 2 1
21: 64 32 16 8 4 2 1
22: 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
23: 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
24: 12 6 3 10 5 16 8 4 2 1
25: 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1"""

There it is,i dont know how to make it determine if its odd or even number and if so use the corresponding algorithm. heres the code i have so far...
Java Code:
``` import java.util.*;
public class Testerr {
public static void main(String args[])
{

int x = 0;
while( x != 2)
{
System.out.println(x + ": ");
x++;
while(x != 1)
{

}
}

}
}```

2. ## Re: Deision makiing and loop control

Have you used the mod operator, %? I usually mod my number by 2 and check if this returns 0 or not.

3. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Re: Deision makiing and loop control

Originally Posted by Fubarable
Have you used the mod operator, %? I usually mod my number by 2 and check if this returns 0 or not.
Im not really sure what you mean by that but i dont think it is what i need to do, if its a odd number it needs to be multiplied by three then one must be added, if its even it must be divided by 2

4. ## Re: Deision makiing and loop control

Originally Posted by MrPosotive
Im not really sure what you mean by that but i dont think it is what i need to do, if its a odd number it needs to be multiplied by three then one must be added, if its even it must be divided by 2
You wanted help determining if it is even or odd. I'm offering a way to test this, so I'm not sure why you're saying that you "don't think it is what you need to do". But it's up to you, as it's your program to do with as you wish.

5. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Re: Deision makiing and loop control

Originally Posted by Fubarable
You wanted help determining if it is even or odd. I'm offering a way to test this, so I'm not sure why you're saying that you "don't think it is what you need to do". But it's up to you, as it's your program to do with as you wish.
Ohhh i re-read your post and now i get what your saying, that might help.

6. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Re: Deision makiing and loop control

Java Code:
```import java.util.*;
public class Testerr {
public static void main(String args[])
{

int x = 0;
int y = 0;
int z = 0;
int a = 0;
int b = 0;
while( x != 25)
{
System.out.println(x + ": "/n);
x++;
while(x != 1)
y = x;
{
z = y / 2;
if(z == 0)
{
a = x / 2;
System.out.println("This is if it was a even number "+ a);
}
else
{
b = x * 3+1;
System.out.println("Tis is if it was a odd number " + b);
}
}
}

}
}```
there is mynew code, ive got it set up to determine if the number is even or odd but now im stuck again and i have no idea where to go fromhere.

7. ## Re: Deision makiing and loop control

Don't make a simple problem harder by adding too many unneeded variables. Besides, you still haven't used the % (mod) operator. % returns the remainder of a division, so 7 % 2 returns 1. Then again, 7 % 4 returns 3. Following this example, any number that is divisable by 2 (which we call "even"), will return 0 when "modded": 2 % 2 returns 0, 10 % 2 returns 0, 1234184 % 2 returns 0, etc.

So, knowing this we can write:
Java Code:
```if (x % 2 == 0) {
//this is executed when x is even
}
else {
//this is executed when x is odd
}```

8. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Re: Deision makiing and loop control

Java Code:
```import java.util.*;
public class Testerr {
public static void main(String args[])
{

int x = 0;
int y = 0;

while( x != 25)
{
System.out.println(x + ": " );

x++;
while(x != 1)
y = x;
{

if(x % 2 ==0)
{
x = x / 2;
System.out.println(x);

}
else
{
x= x * 3+1;
System.out.println(x);
}
}
}

}
}```
there is my new new code but i cant seem to figure out what it needs to do to be fixed

9. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Re: Deision makiing and loop control

Java Code:
```import java.util.*;
public class Testerr {
public static void main(String args[])
{

int x = 0;

for(x = 1; x <= 25; x++)
{
System.out.println(x + ": " );

while(x != 1)
{

if(x % 2 ==0)
{
x = x / 2;
System.out.println(x);

}
else
{
x= x * 3+1;
System.out.println(x);
}
}
}

}
}```
ok i thought this would work but now i have an neverending loop

10. ## Re: Deision makiing and loop control

You're changing the loop index, x, inside of the for loop. Don't do that. Instead first thing to do in the loop is to copy x into another variable and then do your while loop gymnastics with the other local variable.

11. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Re: Deision makiing and loop control

Java Code:
```import java.util.*;
public class Testerr {
public static void main(String args[])
{

int x = 0;
int y = 0;

for(x = 1; x <= 25; x++)
{
System.out.println(x + ": " );

while(x != 1)
{

if(y % 2 ==0)
{
y = y / 2;

}
else
{
y= y * 3+1;

}
}
}

}
}```
is this what you mean? sorry if it seems like im not understanding, ive had a long day for half of it and the other half....well i dont understand

12. ## Re: Deision makiing and loop control

Your loop is fine like that. Like Fubarable says, do not modify x. X is your loop counter and should not be changed. Modify y instead. Problem is: y is nothing yet. So instead of testing x != 0 you should test y != 0, but assign x to y first.
Java Code:
```y = x;
while(y != 1) {
...
}```
The printing of y stilll needs to be done :)

On a side note: personally I would prefer to rename x into "counter" and y into "result" so it's a bit more clear what everything is used for. It's trivial for this excercise, but still, a good habit...
Last edited by SurfMan; 11-09-2012 at 10:18 AM. Reason: Typo

13. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Re: Deision makiing and loop control

Originally Posted by SurfMan
Your loop is fine like that. Like Fubarable says, do not modify x. X is your loop counter and should not be changed. Modify y instead. Problem is: y is nothing yet. So instead of testing x != 0 you should test y != 0, but assign x to y first.
Java Code:
```y = x;
while(y != 1) {
...
}```
The printing of y stilll needs to be done :)

On a side note: personally I would prefer to rename x into "counter" and y into "result" so it's a bit more clear what everything is used for. It's trivial for this excercise, but still, a good habit...
Thanks man that did fix it! but now i just need to figure out how to get the numbers to line up beside eachother now (There stacked) do you know how to do that?

Java Code:
```import java.util.*;
public class Testerr {
public static void main(String args[])
{

int counter = 0;
int result = 0;

for(counter = 1; counter <= 25; counter++)
{
System.out.println(counter + ": " );
result = counter;
while(result != 1)
{

if(result % 2 ==0)
{
result = result / 2;
System.out.println(result);

}
else
{
result= result * 3+1;
System.out.println(result);
}
}
}

}
}```

14. ## Re: Deision makiing and loop control

System.out.println does what is says: print a line. That includes a newline character. System.out.print will *not* have a newline character. Sometimes it's *that* simple :) You can also choose to build a String containing your output, then when you're done with one iteration, print the String.

String s = "a";
s = s + "b"
s now contains "ab"

There are much better ways to do this, but if I explain those now, your teacher will know that you got your answer from a forum (if he's not already reading this) :)

15. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## Re: Deision makiing and loop control

Yea thanks lol i figured that out but yea thatas what i had to do. Plus Rep

#### Posting Permissions

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