Unit V Practice Exercises — Repetition

CS 1130 Visual Basic—Fall 2017


We are gaining facility and experience with programming and should be ready for a little more complexity. Repetition is our primary new capability but with it often comes files, class variables, arrays, and creating our own subroutines.

Computer programs are useful because they allow a problem to be solved repeatedly. Sometimes that means running the program today, then next week, then next month, then next year. Sometimes that means allowing the user to cause repetition by clicking on a button. This assignment addresses the capability of having the computer repeat instructions for solving a problem or (more often) for solving a part of a problem automatically. Knowing what to repeat and how to control the repetition are the main ideas we need to address.

This assignment involves pieces of several problems we have worked on in the past—flashcards, dice games, and text analysis. All the problems will involve looping constructs (For ... or Do Until ... or Do While .... Most will ask you to do at least two versions of looping—a counting loop and an indefinite loop.

I encourage you to work with a partner, either using pair programming (one person types and one person watches for mistakes and swap roles every 30 minutes or so) or working side by side on separate computers. This way each person interacts with the code on each item—the only way to learn the material.

I have prepared a Visual Basic Project you can use to develop and test your code. It is available in the notes directory. I suggest you rename the folder with your initials after you download and unzip it, e.g., unitV_jpe.

Coin Flips

The general idea here is to simulate the repeated flipping of a coin. Before starting to flip the coins there would need to be some initialization. During the iteration we would flip and update our reporting values. After the repetition we would report the results—such values as the number of flips, number of heads, number of tails, number of heads in a row, number of tails in row, longest sequence of heads or tails, and perhaps something else I haven't thought of :-)

  1. a hard-coded number of times
  2. a user-input number of times
  3. until a user-input number of either heads or tails in a row have been flipped
  4. do a large number of trials to see the average number of heads and tails per 1,000 flips
  5. (this is optional) Do the same as above but instead of a fixed number of trials, keep going until the average of heads (or tails) changes less than 0.001

Be aware that the VB random number generator always produces the same sequence of results unless you include a Randomize statement when the program loads.

Flash Cards

There are a variety of ways we might use repetition in the flash card program. Some are noted below. If we were doing a real flash card program we would do all the operations but our goal is to work on loops so only doing a couple operations is okay. Addition and multiplication are the simplest. It would be useful to randomly choose which of the three values to leave blank in the items.

  1. Ask the user how many repetitions they want to do; provide that many (and report number & percent correct)
  2. Provide at least 10 items but keep going until at least 80% (or 90%) are correct (& report ...)
  3. Provide sets that include 20 (or 10) items and keep providing sets of items until a set is completed with 90% (or 80%) accuracy. Report results of each set and of the overall practice session.

If you'd like to work with collections and files consider these problems.

Dice Games—Simulated dice rolling

The idea here is to roll one or more dice and report various results under a variety of circumstances. The data to be reported could include: count of rolls, counts of each possible roll total, percentage of time each roll value occurred, the longest sequence of the same value being rolled in a row, etc. This is similar to the coin flipping activity but there is something new here—recording roll counts in an array. Rolling might be repeated:.

  1. a given number of times
  2. until one of the values has been rolled a certain number of times
  3. until some number of rolls in a row have occurred

Text Analysis

With looping we can make interesting use of our string functions. We can determine the number of words, or sentences, in a piece of text. We can do a frequency distribution of word lengths (like counting the number of rolls for the dice values). We can count the number of vowels in a word. With these capabilities we can also determine the reading level of a piece of text (see last problem).

The repetition activities here are provided below. My recommendation is that you process each character in the string or each character in a line from the file.

  1. count the number of words in a piece of text (from a textbox or from lines in a file)

    A word has a space after it or is at the end of a line or is at the end of the piece of text.

  2. produce a frequency distribution of the words in a piece of text, from a text box or file. (This is like counting the different values when rolling dice.)
  3. count the number of sentences in a piece of text or in a file

    A sentence ends with a period, question mark, or exclamation point.

  4. (this is optional) Produce the Flesch reading scale score for a piece of text or file of text

    The key to this program is a very simple algorithm to count the number of syllables in a word. In general the text analysis portion of the program uses the following rules: Periods, exclamation points, question marks, colons and semi-colons count as end-of-sentence marks. Each group of continuous non-blank characters counts as a word. Each vowel (a, e, i, o, u, y) in a word counts as one syllable subject to the following sub-rules: Ignore final -ES, -ED, -E (except for -LE) Words of three letters or less count as one syllable. Consecutive vowels count as one syllable. Although there are many exceptions to these rules, it works in a remarkable number of cases. The Flesch Index (F) for a given text sample is calculated from three statistics; The total number of sentences (N), The total number of words (W), The total number of syllables (L), according to the following formula: F = 206.835 - 1.015 × (W/N) - 84.6 × (L/W).

Note that I have included three files in the bin\Debug folder that you could use in the text processing.

Extra Credit

If you wish to submit something for extra credit download the blank project and do the assignment in it. Then supply something that goes beyond the assignment, perhaps doing most of the optional problems. Or, you might do something else; impress me.

When done zip up the project and send it to me.

Process Reminders

Don't forget to use Save All. And, use it early and at least after completing a part (form or subroutine or ...) of the program.

If you have problems and want me to examine what is going on

  1. compress/zip the folder—right click on the folder; then select: Send to | Compressed (zipped) folder
  2. change the name, e.g., substitute .txt for .zip
  3. draft an e-mail message describing your difficulty
  4. attach the compressed, renamed file to the message
  5. send me the message