Skip to main content

Posts

Showing posts with the label Javaprogramming

Homework 3

Combinations : One of the things that computers are particularly good at is doing “the same thing over and over” in extremely tedious calculations. One of the applications for this is producing all the possible combinations of groups of things – for example, imagine you have 3 pennies in your pocket, and you flip them, one at a time, producing Heads(H), Tails(T), and Heads(H). Think of the sequence H,T,H as being one possible combination (outcome) of your coin flipping experiment. If we wanted to write a computer program to simulate ALL possible combinations of the flipping of your 3 pennies, the following algorithm would be just the ticket: int heads = 0; // let’s let the value 0 represent a heads coin toss int tails = 1; // let’s let the value 1 represent a tails coin toss for (int coin1 = heads; coin1 <= tails; coin1++) { for (int coin2 = heads; coin2 <= tails; coin2++) { for (int coin3 = heads; coin3 <= tails; coin3++) { String coin1str = ""; if (coin1 == 0) coin...