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 1<br />

The following program evaluates each of the functions for a specific input given by the value of the variable<br />

n.<br />

Private Sub cmdEvaluate_Click()<br />

Dim n As Single, root As Single<br />

‘Evaluate functions at a variable<br />

picResults.Cls<br />

n = 6.76<br />

root = Sqr(n)<br />

picResults.Print root; Int(n); Round(n,1)<br />

End Sub<br />

[Run <strong>and</strong> then click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

2.6 6 6.8<br />

EXAMPLE 2<br />

The following program evaluates each of the preceding functions at an expression.<br />

Private Sub cmdEvaluate_Click()<br />

Dim a As Single, b As Single<br />

‘Evaluate functions at expressions<br />

picResults.Cls<br />

a = 2<br />

b = 3<br />

picResults.Print Sqr(5 * b + 1); Int(a ^ b); Round(a / b, 3)<br />

End Sub<br />

[Run <strong>and</strong> then click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

4 8 0.667<br />

EXAMPLE 3<br />

The following program shows an application of the Sqr function.<br />

Private Sub cmdComputeHyp_Click()<br />

Dim leg1 As Single, leg2 As Single, hyp As Single<br />

‘Find the length of the hypotenuse of a right triangle<br />

picHyp.Cls<br />

leg1 = Val(txtFirst.Text)<br />

leg2 = Val(txtSecond.Text)<br />

hyp = Sqr(leg1 ^ 2 + leg2 ^ 2)<br />

picHyp.Print “The length of the hypotenuse is”; hyp<br />

End Sub<br />

[Run, type 3 <strong>and</strong> 4 into the text boxes, <strong>and</strong> then click the comm<strong>and</strong> button.]<br />

Built-In Functions 59

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

Saved successfully!

Ooh no, something went wrong!