Chapter 16 Using Arrays

An array is a mechanism of using the one variable name to access many instances of data of the one data type by using an index. An example of this is the result of the spin of a poker machine. One of many instances of a given prize range may be the output. In this example, there are 6 possible results - plum, Apple, Apricot, Orange, Banana or Pineapple.

Casino problem

You have been contracted by Lucky Dot Casinos  to produce a Visual Basic simulation program that will allow a customer to "roll" two dice. When each die has the same value, the customer wins a prize. The values of each die are Plum, Apple, Apricot, Orange, Banana and Pineapple.

Planning

An array of 6 values must be declared. When the roll button is clicked, two random number between 1 and 6 are generated. These are then used to reference the array. The referenced values are returned and compared to each other. If they are the the same, a prize is awarded.

Pseudcode

    Set roll to Apricot, Apple, Orange, Banana, Plum, Pinapple
    Get Rnd01Num 
    Get Rnd02Num 
    If roll(Rnd01Num) =  roll(Rnd02Num) then
      Write Winner message
    End if

Coding

Public roll (6) as string 'this bit goes under Public Form 1
this bit under cmdPlay_click


Dim rnd01Num, rnd02Num As Integer
 'load the array with the values.
    roll(0) = "Apricot"
    roll(1) = "Apple"
    roll(2) = "Orange"
    roll(3) = "Banana"
    roll(4) = "Plum"
    roll(5) = "Pinapple"
    Rnd01Num = Int(6 * Rnd)
    lblRoll01.text = roll(Rnd01Num)
    
    Rnd02Num = Int(6 * Rnd)
    lblRoll02.text = roll(Rnd02Num)

    If roll(Rnd01Num)= roll(Rnd02Num) then 
        msgbox("Winner")
    End if

Explanation

When the application loads, the array is "loaded" with the information required for the display.
When the roll button is clicked, the click event creates two random numbers between 0 and 5 inclusive, displays the indexed value for each number in a label and then compares the two. If they are the same, a message is displayed that a win has occurred. An array is used as it allows a variety of messages to be displayed by referencing one variable with an index number.

Exercises Chpt 16

Problem 1

Mendez fish supply has 10 boats that catch fish. Each day, the weight of the biggest fish caught (in kgs) is sent to the head office. Your task is to write a program that will allow the weight of each fish and name of the boat to be recorded (in arrays). The weight of the largest fish is then to be determined by searching the array (of weights) and displayed along with the name of the boat that caught it.

Note: This requires two arrays - one for the weight of the fish and one for the name of the boat.

Problem 2

Visual Basic has a function named "join" which will join all of the individual components of an array into a single sentence (string).

Your task is to write a program that will allow the user to enter the character/word and position of that component. The result will be a sentence that can be changed programmatically.

Problem 3 (Very difficult)

Palindromes are sentences that say the same thing in either direction  - madam i'm adam - is an example (omitting spaces and other punctuation). Your task is to write a program that will take a sentence - excluding spaces and other punctuation - that will determine if the text makes a palindrome.

Note 1: You may need to use the following functions to disassemble into characters and reassemble characters into strings : len, mid, join

Note 2: make all test data without spaces and punctuation ie madam i'm adam  becomes madamimadam 

Problem 4

Write a program that will multiply 2 arrays (3x3) of numbers.


Author Mike Leishman (Last Updated 15 April 2000)

Edited by FDS 16/4/07