Results 1 to 15 of 15
Thread: Deision makiing and loop control
- 11-08-2012, 05:09 AM #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) { } } } }
-
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.
- 11-08-2012, 05:17 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
-
Re: Deision makiing and loop control
- 11-08-2012, 05:35 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
- 11-08-2012, 05:44 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: Deision makiing and loop control
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.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); } } } } }
- 11-08-2012, 11:19 AM #7
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
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 }
- 11-09-2012, 04:33 AM #8
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: Deision makiing and loop control
there is my new new code but i cant seem to figure out what it needs to do to be fixedJava 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); } } } } }
- 11-09-2012, 04:41 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: Deision makiing and loop control
ok i thought this would work but now i have an neverending loopJava 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); } } } } }
-
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-09-2012, 06:41 AM #11
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: Deision makiing and loop control
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 understandJava 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; } } } } }
- 11-09-2012, 10:17 AM #12
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
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.
The printing of y stilll needs to be done :)Java Code:y = x; while(y != 1) { ... }
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
- 11-10-2012, 05:17 AM #13
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: Deision makiing and loop control
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); } } } } }
- 11-10-2012, 05:03 PM #14
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
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) :)
- 11-11-2012, 06:09 AM #15
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Similar Threads
-
Control loop do not work
By Pojahn_M in forum New To JavaReplies: 5Last Post: 12-26-2011, 01:17 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
Moving control backwards within loop in java
By jharishabh7 in forum New To JavaReplies: 16Last Post: 10-07-2010, 04:20 PM -
Really need help with an assignment... counter control loop...
By maxpower1000sa in forum New To JavaReplies: 7Last Post: 02-21-2009, 10:52 PM -
control app width based on certain control
By thebillybobjr in forum SWT / JFaceReplies: 0Last Post: 05-15-2008, 04:52 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks