CS368 Homework 5
Check here periodically.
For this homework create one main script file named homework5.m and the supporting function named flipCards (in a file named flipCards.m). Use one of your previous homework files as a starter template for this homework. Complete ALL your work in MATLAB (in your homework5.m script and any functions you write) and make sure to display output exactly as specified in the problem descriptions.
Problem 1: (5 points) Nested for Loops
Write code using for loop(s) that will display a single line of asterisks (*) based on the value of n and followed by the value of n. Then, nest your code inside another for loop over n where n goes from 1 to 12 to show that it works. Your output should look like:
* (1) ** (2) *** (3) **** (4) ***** (5) ****** (6) ******* (7) ******** (8) ********* (9) ********** (10) *********** (11) ************ (12)
Problem 2: (6 points) Flipping a Coin
Suppose you repeatedly flip a coin and keep track of the number of times you get heads. How many coin flips will it take before you get thirty heads? You probably have an idea of what the answer should be. In this problem, you will model flipping a coin using MATLAB's randperm function and determine the number of coin flips experimentally.
If you run your code from part 2.a several times, you will find that you get different results each time. Suppose we consider what was done in part 2.a to be a single trial.
Problem 3: (9 points) Card Trick
A traveler is stuck at a hotel in a snowstorm. Bored and wanting to find something to fill the time, the traveler takes out a deck of cards and lays them face up in a row. The traveler then decides to go through the cards from left to right and flip every other one over. The traveler then goes through the cards again from left to right and this time turns every third card. The traveler repeats this process for every fourth card, every fifth card, etc., through all possible multiples.
To model this scenario in MATLAB, you will create an array card that indicates if a card is face up or face down. If card(k) is 1, then the card is face up and if card(k) is 0, then the card is face down. When a card is flipped, it changes: if a card is face up, flipping it over makes the card face down; if a card is face down, flipping it over makes it face up.
At the end of the process, the traveler looks at which cards are face up and notices an interesting pattern. Intrigued, the traveler clears some space, takes out two more decks of cards, and lays out 156 cards face up and repeats the scenario.
Hint: if your flipCards is working correctly, you should find that the cards that are face up at the end are the ones at positions that are perfect squares, i.e., 12, 22, 32, 42, etc.
Upload your m-files to your Dropbox in Learn@UW. See these instructions for uploading files to a Learn@UW Dropbox. The files you should upload are:
In order for your work to be considered to have been turned in on-time, you must upload your files to your Learn@UW Dropbox by 11:59 pm Wednesday, April 6.
|Last Updated: 3/22/2016 © 2016 Beck Hasti, firstname.lastname@example.org|