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.
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 |
|||||||||||||||||||||||||||||||||||||||||||||||||
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:
Hint: One car jumped on the dog. Also you will need code similar to this to get the first letter of an article capitalized: Caparticle = article(ranum.Next(0, 5) |
|||||||||||||||||||||||||||||||||||||||||||||||||
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:
then its transpose is:
Write a program to transpose the array when a button is clicked, and display the result. |