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 4 Control Structures: Part 1 115<br />

It is crucial <strong>to</strong> employ a sentinel value that cannot be confused with an acceptable input<br />

value. Grades on a quiz are normally nonnegative integers, thus –1 is an acceptable sentinel<br />

value for this problem. A run of the class-average program might process a stream of inputs<br />

such as 95, 96, 75, 74, 89 and –1. The program would then compute and print the class<br />

average for the grades 95, 96, 75, 74 and 89. The sentinel value, –1, should not enter in<strong>to</strong><br />

the averaging calculation.<br />

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

Choosing a sentinel value that is also a legitimate data value could result in a logic error<br />

that would cause a program <strong>to</strong> produce incorrect results. 4.5<br />

When solving more complex problems, such as that contained in this example, the<br />

pseudocode representation might not appear obvious. For this reason we approach the<br />

class-average program with <strong>to</strong>p-down, stepwise refinement, a technique for developing<br />

well-structured algorithms. We begin with a pseudocode representation of the <strong>to</strong>p:<br />

Determine the class average for the quiz<br />

The <strong>to</strong>p is a single statement that conveys the overall function of the program. As such, the<br />

<strong>to</strong>p is a complete representation of a program. Unfortunately, the <strong>to</strong>p rarely conveys a sufficient<br />

amount of detail from which <strong>to</strong> write the <strong>Visual</strong> <strong>Basic</strong> algorithm. Therefore, we conduct<br />

the refinement process. This involves dividing the <strong>to</strong>p in<strong>to</strong> a series of smaller tasks that are<br />

listed in the order in which they must be performed resulting in the following first refinement:<br />

Initialize variables<br />

Input, sum and count the quiz grades<br />

Calculate and print the class average<br />

Here, only the sequence structure has been used—the steps listed are <strong>to</strong> be executed in order,<br />

one after the other.<br />

Software Engineering Observation 4.2<br />

Each refinement, including the <strong>to</strong>p, is a complete specification of the algorithm; only the level<br />

of detail in each refinement varies. 4.2<br />

To proceed <strong>to</strong> the next level of refinement (i.e., the second refinement), we commit <strong>to</strong><br />

specific variables. We need a running <strong>to</strong>tal of the numbers, a count of how many numbers<br />

have been processed, a variable <strong>to</strong> receive the value of each grade and a variable <strong>to</strong> hold the<br />

calculated average. The pseudocode statement<br />

Initialize variables<br />

can be refined as follows:<br />

Initialize <strong>to</strong>tal <strong>to</strong> zero<br />

Initialize counter <strong>to</strong> zero<br />

Notice that only the variables <strong>to</strong>tal and counter are initialized before they are used; the variables<br />

average and grade (the program in Fig. 4.16 uses these variables for the calculated<br />

average and the user input, respectively) need not be initialized because the assignment of<br />

their values does not depend on their previous values, as is the case for <strong>to</strong>tal and counter.<br />

The pseudocode statement<br />

Input, sum and count the quiz grades

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

Saved successfully!

Ooh no, something went wrong!