CS 1150 PEEE   Repetition to Organize Actions

Overview

We have discussed the fundamentals of programming as consisting of data & actions (often on data) and organizing the actions via sequence, selection, repetition and modularization. This practice/learning activity will revisit those ideas in more detail focussing on repetition to organize the actions in a program (or set of scripts). The learning activity is described after the Process Reminders below.

Process Reminders

Pair Programming

Keep the "pair programming" process in mind as you work. One person types and the other person watches and corrects, questions, etc. After a bit (at most 30 minutes) you change roles.

Grading

This activity is for you to learn, so it is not graded. The learning will be inferred in the PARR (Programming Activity and Reflection Report) document you (eventually) submit and in one of the in-term exams. Remember to add to your notes for the PARR.

If you have questions or difficulties

If you have questions about the assignment send me an e-mail or drop by my office. If you have a question while working on the assignment do the same. Keep in mind that when you encounter something you can't figure out you can/should think, explore, seek answers on google, etc. but, do not spend more than 15-30 minutes trying to overcome a particular error or problem.

Extra Credit

The syllabus noted a couple ways you might earn extra credit. I am adding another.

One is to share some aspect(s) of programming or Snap! you think others might not have experienced. You should annotate your program using the program notes (File icon | Program notes) and/or comments blocks (see p.68 of the Snap! Reference Manual. The annotation should describe or explain the learning/insight(s) you want to share.

To receive extra credit for such an annotated program on this activity you will also need to submit a copy of your algorithms. Either create a PDF of a document or include the text in your submission message.

The alternative method to work for extra credit is that the partnership could suggest additions to our how to do it page. You might suggest a revision to clarify (or finish) something that is there already or an entirely new topic. Please indicate how/why you think the would be helpful. Be sure you fully and clearly describe what you are talking about.

The third method to receive a bit of extra credit is to submit the "Create your own problems ..." that you created and solved. Include all three problems and a pseudo-code algorithm of each solution.

Submitting something for either kind of extra credit is as simple as sending an e-mail message. The message should:

Learning Activity

The goal here is to create use repetition to help organize the execution of actions to accomplish particular goals. We are at the point where programs/scripts are getting more complex. We will be use repetition statements (repeat __, repeat until _____, and forever blocks. But when developing our algorithms and programs we will need to consider:

I suggest you use the following script in the Stage to control the program action.

Snap! repeat until 0 entered code

Then for each problem there will a matching script that gets executed by some sprite. The code for that would replace the "say" block in the following script and perhaps use a different number in the "when I receive __" block.

Snap! when I receive block

The Items

Produce scripts that accomplish the tasks below. Note that while you might be able to avoid repetition, you are not allowed to. The tasks are to be accomplished using repetition.

  1. count to 10

    Show (say) each of the numbers you would use when counting to 10.

  2. countdown (10 to 1)

    Count backwards. After you get to one, announce "blast off" or "go".

  3. count to 100 by 5

    Show each number as you would count to 100 by 5.

  4. count to user specified value

    Count (by 1) to a user specified value.

  5. count by user specified value for 20 instances

    Get a count-by value from the user and then starting with that value count/show 20 counts from the starting value.

  6. count by user specified value to a user specified value

    Get the two values from the user, then start with the indicated count-by value and count until you get to (or beyond) the count-to value.

  7. craps game

    Using two sprites with six dice costumes and a button you create for clicking to roll the dice, create a little craps game. Craps is played with two basic phases—come-out and point and starts in the come-out phase.

    • do come-out phase

      Roll the two dice until one of three outcomes occurs.

      • roll is 2, 3, or 12 — annouce (say) "craps, loser"
      • roll is 7 or 11 — announce "natural, winner!"
      • roll is 4, 5, 6, 8, 9, 10 — announce "point is set to __" (include the roll value in the blank)
    • do point phase

      Snap! set block to establish the possible craps point values.
      Snap! set block to establish a point value in a craps simulation.

      Before the rolling begins you will need to generate a random value for the point. I suggest you put the values 4, 5, 6, 8, 9, & 10 in a list then randomly choose one of them for you your point value. You would need two variables and the blocks shown here. Note initializing the list of values would only be done once. After establishing the data elements of the problem, roll the two dice until one of two outcomes occurs.

      • roll is 7 — announce "seven out, loser"
      • roll is the same as the point — announce "__, a winner" (the point value goes in the blank

      If neither 7 or the point value is rolled, just keep rolling.

  8. demonstrate the quadrants

    Move the sprite to the (approximate) center of each quadrant in the coordinate plane and have the sprite announce the quadrant number (I, II, III, IV). Note you will likely need lists (as described above in the craps game item) for quadrant numbers and x- and y-coordinates of each of the quadrants.

  9. Sieve of Eratosthenes

    Build a sieve of Eratosthenes for the values 1 to N (I plan to use 100 for N). The sieve of Eratosthenes is essentially a list of the prime numbers less than the N. Below are both a general and a more detailed algorithm for accomplishing the task.

    • General outline
          set primeList to include all the numbers from 2 to N
          set counter to 1
          for each number from 2 to sqrt(N) rounded up
          |   
          |   count divisors of item:counter:of primeList
          |   
          |   if  divisorCount = 2
          |   |   remove, from primeList, multiples of item:counter:of primeList
          |   |________________
          |____________________
      
    • Detailed outline pieces
      TO  set primeList to include all the numbers from 2 to N
          set N to 100
          for each number from 2 to 100
          |   add  number to primeList
          |____________________
      
      
      TO  count divisors of item:counter:of primeList
          set divisors to 1          {this will count the number itself as a devisor}
          for each number from 1 to sqrt(count) rounded up
          |   if  item:counter:of primeList is divisible by number
          |   |   change divisors by 1
          |   |________________
          |____________________
      
      
      TO  remove, from primeList, multiples of item:counter:of primeList
          set  number to counter + 1
          repeat until number > length of primeList
          |   if  item:number:of primeList is divisible by item:counter:of primeList
          |   |   delete item:number:of primeList
          |   else
          |   |   change number by 1
          |   |________________
          |   change number 
          |____________________
      
      

    Note: you can "import tools" (via the file icon) to get the for loop block or you can build your for loop using the repeat until. A for block that says for i = 1 to N could be written as:

        set i to 1
        repeat until i > N
        |   ...
        |   change i by 1
        |____________________
    
  10. create (and solve) your own problem(s) that require the use of selection statements