CST1801 Visual Basic I
Lab Week 6


Exercises

Exercise DieTotals (10 points)

Write a program that simulates the rolling of two dice.  The program should use the random class object to roll the first die, and should use random class object again to roll the second die.  The sum of the two values should then be calculated.  Note:  Since each die can show an Integer value from 1 to 6, the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and and 12 being the least frequent sums.  The figure below shows the 36 possible combinations of the two dice.  Your program should roll the two dice 36,000 times.  Use a single subscripted array to tally the number of times each possible sum appears.  Print the results in a tabular format.  Are the results you obtained reasonable? (i.e., there are six ways to roll a 7), so approximately one sixth of all the rolls should be 7.

  1 2 3 4 5 6
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11
6 7 8 9 10 11 12

Hint: The following formatting string is useful.  (x,2:G) means 2 digits in a general format.  (z,10:P) means 10 digits in a percentage format.  More information can be found in the MSDN help.

myString += String.Format("{0,2:G} {1,9:G} {2,10:P}", x, y, z) & vbCrLf

  Result

Exercise SentenceGeneration (10 points)

Write a program that uses random number generation to create sentences.  Use four arrays of Strings called article, noun, verb and preposition.  Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun.  As each word is picked, concatenate it to the previous words in the the sentence.  The words should be separated by spaces.  When the sentence is output, it should start with a capital letter and end with a period.  The program should generate 20 sentences and output them to a Listbox.

The arrays should be filled as follows:

article noun verb preposition
the boy drove to
a girl jumped from
one dog ran over
some town walked under
any car skipped on

Hint:
Example sentences would be:

One car jumped on the dog.
The girl ran under some car.

Also you will need code similar to this to get the first letter of an article capitalized:

Caparticle = article(ranum.Next(0, 5)
Capletter = Caparticle.substring(0, 1).toUpper
Caparticle = Caparticle.Remove(0, 1)
Caparticle = Capletter & Caparticle

  Result

Exercise 15.4 Transpose EC (Extra Credit 10 points)

The transpose of an array is the technical term used to describe swapping the elements in an array across one of the diagonals. The numbers on the diagonal do not change. So if an array is: 

2 4 6 8
10 12 14 16
18 20 22 24
26 28 30 32

then its transpose is:

2 10 18 26
4 12 20 28
6 14 22 30
8 16 24 32

 

Write a program to transpose the array when a button is clicked, and display the result.