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.,
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 :-)
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.
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.
If you'd like to work with collections and files consider these problems.
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:.
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.
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.
A sentence ends with a period, question mark, or exclamation point.
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.
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.
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