19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EXAMPLE 8<br />

The DC Lottery number is obtained by selecting a Ping-Pong ball from each of three separate bowls. Each<br />

ball is numbered with an integer from 0 through 9. Write a computer program to produce a lottery number.<br />

(Such a program is said to simulate the selection of Ping-Pong balls.)<br />

SOLUTION:<br />

The value of Int(10 * Rnd) will be an integer from 0 through 9, <strong>and</strong> each of these integers has the same<br />

likelihood of occurring. Repeating the process three times produces the requested digits.<br />

Private Sub cmdDisplayANumber_Click()<br />

‘Display a lottery number<br />

picNumber.Cls<br />

R<strong>and</strong>omize<br />

picNumber.Print Int(10 * Rnd);<br />

picNumber.Print Int(10 * Rnd);<br />

picNumber.Print Int(10 * Rnd)<br />

End Sub<br />

[Run <strong>and</strong> then click the comm<strong>and</strong> button. One possible output to be displayed in the picture box is as follows.]<br />

8 3 9<br />

Note: Run the program in Example 8 several times <strong>and</strong> notice that the output changes each<br />

time. Then delete the R<strong>and</strong>omize statement <strong>and</strong> run the program several times.<br />

COMMENTS<br />

1. Requesting the square root of a negative number terminates the execution of the<br />

program <strong>and</strong> gives the error message “Invalid procedure call or argument.”<br />

2. If n is greater than the length of str, then the value of Left(str, n) will be the<br />

entire string str. A similar result holds for Mid <strong>and</strong> Right.<br />

3. <strong>Visual</strong> <strong>Basic</strong> has a function called LCase that is analogous to UCase. LCase<br />

converts all uppercase letters in a string to lowercase letters.<br />

4. Because the values of the functions Left, Mid, Right, UCase, <strong>and</strong> the Format<br />

functions are strings, they are referred to as string-valued functions.<br />

5. Mid is an important function. It will be used several times in this book to examine<br />

each letter of a string.<br />

6. Trim is useful when reading data from a text box. Sometimes users type spaces<br />

at the end of input. Unless the spaces are removed, they can cause havoc elsewhere<br />

in the program. Also, Trim is useful in trimming the leading spaces from<br />

numbers that have been converted to strings with Str.<br />

7. The InStr function has a useful extension. The value of InStr(n, str1, str2) is the<br />

position of the first occurrence of str2 in str1 in position n or greater. For<br />

instance, InStr(5, “Mississippi”, “ss”) is 6.<br />

8. In Example 4, we found that 4 is the remainder when 256 is divided by 14.<br />

Mathematicians say “4 = 256 modulo 14.” <strong>Visual</strong> <strong>Basic</strong> has an operation, Mod,<br />

that performs this calculation directly. If m <strong>and</strong> n are positive integers, then n<br />

Mod m is the remainder when n is divided by m. <strong>Visual</strong> <strong>Basic</strong> also has an operation<br />

called integer division, denoted by \, which gives the quotient portion of<br />

a long division problem. For instance, 14/4 is 3.5 whereas 14\4 is the integer 3.<br />

9. Recall that the function Mid has the form Mid(str, m, n) <strong>and</strong> returns the substring<br />

of str starting with position m <strong>and</strong> having length n. <strong>Visual</strong> <strong>Basic</strong> does its<br />

best to please for unexpected values of m <strong>and</strong> n. If m is greater than the length<br />

Built-In Functions 65

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!