CS368 Homework 5
Lec 1, Spring 2016
Due by 11:59 pm on Wednesday, April 6 (not accepted late)

Announcements

Check here periodically.

3/22/2016  Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: http://www.piazza.com/wisc/spring2016/cs368 and sign-in using your wisc.edu account.

Problems

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.

  1. Write a code fragment that repeatedly flips a coin until heads has been seen thirty times. Your code should then display the number of coin flips needed to get thirty heads. To receive full credit, you must use a while loop (and may not use a for loop or any break statements).

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.

  1. Using your code from part 2.a, write a code fragment that runs 2000 trials and then displays the average number of coin flips needed to get thirty heads. (Hint: you will need to nest a copy of your code from part 2.a inside a for loop.)

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.

  1. (2 pts) Start with all 52 cards face up. Write code fragment with a loop that flips all even-numbered cards (i.e., the cards at positions 2, 4, 6, etc., up to 52). Use the MATLAB disp command to show the array card after the loop.

  2. (1 pt) After the loop from part 3.a, add a loop that then turns the cards that are multiples of 3, (i.e., 3, 6, 9, 12, etc., up to 52). Use the MATLAB disp command to show the array card after the loop.

  3. (4 pts) Using your code from part 3.b as a guide, write a function named flipCards that has one parameter, N, representing the number of cards. The flipCards function should create the card array (with the cards all starting face up) and then have a set of nested loops that flips all the cards that are multiples of 2, then all that are multiples of 3, then all that are multiples of 4, etc., up to the cards that are multiples of N. The return value of the flipCards function should be the card array.

  4. (1 pt) In your script, call your flipCards function with 52 cards and display the positions of the cards that are face up. Tip: you can avoid having to write a loop by using the find function. One way the find function can be used is to find which indices in a vector are not 0. In the MATLAB Command Window, type help find (or doc find) to learn more about the find function.

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.

  1. (1 pt) Call your flipCards function with 156 cards and display the positions of the cards that are face up.

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.

Handing in

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:

  • homework5.m
  • flipCards.m

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, hasti@cs.wisc.edu