30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

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.

Chapter 7 Arrays 255<br />

Good <strong>Program</strong>ming Practice 7.1<br />

Strive for program clarity. Sometimes, it is worthwhile <strong>to</strong> forgo the most efficient use of memory<br />

or processor time if the trade-off results in a clearer program. 7.1<br />

Performance Tip 7.1<br />

Sometimes, performance considerations outweigh clarity considerations. 7.1<br />

The For structure (lines 22–24) reads the responses from the array responses one<br />

at a time and increments one of the 10 counters in the frequency array (frequency(1)<br />

<strong>to</strong> frequency(10)). The key statement in the loop appears in line 23. This<br />

statement increments the appropriate frequency counter as determined by the value of<br />

responses(answer).<br />

Let us consider several iterations of the For structure. When counter answer is 0,<br />

responses(answer) is the value of responses(0) (i.e., 1—see line 14). Therefore,<br />

frequency(responses(answer)) actually is interpreted as frequency(1),<br />

meaning the first counter in array frequency is incremented by one. In<br />

evaluating the expression frequency(responses(answer)), <strong>Visual</strong> <strong>Basic</strong> starts<br />

with the value in the innermost set of parentheses (answer, currently 0). The value of<br />

answer is plugged in<strong>to</strong> the expression, and <strong>Visual</strong> <strong>Basic</strong> evaluates the next set of parentheses<br />

(responses(answer)). That value is used as the index for the frequency<br />

array <strong>to</strong> determine which counter <strong>to</strong> increment (in this case, the 1 counter).<br />

When answer is 1, responses(answer) is the value of responses(1) (i.e.,<br />

2—see line 14). As a result, frequency(responses(answer)) actually is interpreted<br />

as frequency(2), causing array element 2 (the third element of the array) <strong>to</strong> be<br />

incremented.<br />

When answer is 2, responses(answer) is the value of responses(2) (i.e.,<br />

6—see line 14), so frequency(responses(answer)) is interpreted as frequency(6),<br />

causing array element 6 (the seventh element of the array) <strong>to</strong> be incremented<br />

and so on. Note that, regardless of the number of responses processed in the survey, only<br />

an 11-element array (in which we ignore element zero) is required <strong>to</strong> summarize the results,<br />

because all the response values are between 1 and 10, and the index values for an 11-element<br />

array are 0–10. Note that, in the output in Fig. 7.5, the numbers in the frequency<br />

column correctly add <strong>to</strong> 40 (the elements of the frequency array were initialized <strong>to</strong> zero<br />

when the array was allocated with New).<br />

If the data contained out-of-range values, such as 13, the program would attempt <strong>to</strong> add<br />

1 <strong>to</strong> frequency(13). This is outside the bounds of the array. In other languages like C<br />

and C++ programming languages, such a reference would be allowed by the compiler and<br />

at execution time. The program would “walk” past the end of the array <strong>to</strong> where it thought<br />

element number 13 was located and would add 1 <strong>to</strong> whatever happened <strong>to</strong> be s<strong>to</strong>red at that<br />

memory location. This could modify another variable in the program, possibly causing<br />

incorrect results or even premature program termination. <strong>Visual</strong> <strong>Basic</strong> provides mechanisms<br />

that prevent accessing elements outside the bounds of arrays.<br />

Common <strong>Program</strong>ming Error 7.2<br />

Referencing an element outside the array bounds is a runtime error. 7.2

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

Saved successfully!

Ooh no, something went wrong!