Results 1 to 12 of 12

# Thread: Exersices

- 05-28-2008, 03:59 PM #1
## Exersices

Since I think exercises are fun I will post some in this thread for you guys to try. Hopefully if this is implemented I could give points to completed projects. I hope you enjoy these. If you think you have completed the exercise then post your code for others to see. Hopefully this will become a learning experience for us all.

Exercise 1:

Use recursion to make an algorithm that computes the factorial of a certain number. For instances 2 factorial is 2. 3 factorial is 6.

Sorry for the simplicity of this first exercise, I promise I will make more challenging ways later. I will post another exercise later. If you have one that you would like me to post pm me and I will give you credit and will post it most likely.My IP address is 127.0.0.1

- 05-28-2008, 04:53 PM #2
Recursion is fun...you did start easy.

Java Code:static int factorial(int temp) { if (temp < 3) { return temp; } else { return temp * factorial(temp - 1); } }

- 05-28-2008, 05:56 PM #3
@ Chris good job.

Exercise 2:

N number of people keeping flipping their coins until there is a odd man out. Write a program that will find the average number of tries it will take in order for their to be an odd man out. What I mean is if there is 3 people then 2 of them have to have heads and one person has to have tails. or 2 people have to have tails and one person has to have heads. Write this program so that it keeps flipping all the coins and keeps track of the number of tries. Try to find an average number. Bonus for any one who finds the average for up to 10 people.My IP address is 127.0.0.1

- 05-28-2008, 06:17 PM #4
Why not just make it not care about the number of people? And you never mentioned how many times to test to get the average. So those are my two parameters.

Java Code:static double flip(int people, int flips) { Random r = new Random(); double average = 0; for (int x = 0; x < flips; x++) { int count = 0; int total = 0; while ((total != people - 1) & total != 1) { total = 0; for (int y = 0; y < people; y++) { total += Math.abs(r.nextInt()) % 2; } count++; } average += count; } return average / flips; }

- 05-28-2008, 06:31 PM #5
Think of it this way Chris.

Ten people are in a line they all toss their coins. They check to see if there is an odd man out. If not they all flip their coins again. The program is suppose to try and figure out the average number of flips it will take for N number of people to get an odd man out.Last edited by Zosden; 05-28-2008 at 06:48 PM.

My IP address is 127.0.0.1

- 05-28-2008, 06:59 PM #6
And how does mine not do that?

- 05-28-2008, 07:08 PM #7
how does urs check to see if there is an odd man out.

My IP address is 127.0.0.1

- 05-28-2008, 07:24 PM #8
it does a running total of random numbers (example 0 for heads 1 for tails). A 1 or # of people -1 would mean an odd man out.

- 05-28-2008, 07:33 PM #9
ok good job sorry I can't really test it out cause im on my iPhone. Could you please post the average number of flips for n = 3 through n = 10.

Exercise 3:

This one should shock you guys. If you take two random integers and put them in a fraction the probability that they will be in lowest terms has to do with pi. Here's the formula pi = the square root of (6 / (number of fractions in lowest terms / number of tries)).My IP address is 127.0.0.1

- 05-29-2008, 04:06 AM #10
Exercise 4:

Write a program where one computer is a server and the other is a client. The server will ask the client if he wants to hear a joke then if the client sends back yes the server sends a joke. This can all be done with system.out but it needs to be networked. I would also apperiated it if people would pm me some of their ideas.My IP address is 127.0.0.1

- 05-29-2008, 01:48 PM #11
I will now only post an exercise once a day.

Exercise 5:

This one is a bit longer, but I thought it was fun to make. I think I'm going to leave it open and see your creativity at hand. If you guys would like I can post mine too.

Here's the exercise: make a screensaver like app that just looks like a screensaver. Bonus if you actually make it an actual screensaver.My IP address is 127.0.0.1

- 06-03-2008, 04:54 AM #12

## Bookmarks