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.

114 Control Structures: Part 1 Chapter 4<br />

Lines 7–10 declare variables <strong>to</strong>tal, gradeCounter, and grade <strong>to</strong> be of type<br />

Integer and average <strong>to</strong> be of type Double. In this example, <strong>to</strong>tal accumulates the<br />

sum of the grades entered, and gradeCounter counts the number of grades entered.<br />

Variable grade s<strong>to</strong>res the value entered (line 21).<br />

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

Always place a blank line between declarations and executable statements. This makes the<br />

declarations stand out in a program and contributes <strong>to</strong> program readability. 4.6<br />

Notice from the output that although each grade entered is an integer, the averaging<br />

calculation is likely <strong>to</strong> produce a number with a decimal point (i.e., a floating-point<br />

number). The type Integer cannot represent floating-point numbers, so this program<br />

uses data type Double, which s<strong>to</strong>res double-precision floating-point numbers. <strong>Visual</strong><br />

<strong>Basic</strong> also provides data type Single for s<strong>to</strong>ring single-precision floating-point numbers.<br />

Data type Double requires more memory <strong>to</strong> s<strong>to</strong>re a floating-point value, but is more accurate<br />

than type Single. Type Single is useful in applications that need <strong>to</strong> conserve<br />

memory and do not require the accuracy provided by type Double.<br />

Lines 13–14 initialize <strong>to</strong>tal <strong>to</strong> 0 and gradeCounter <strong>to</strong> 1. Line 17 indicates that<br />

the While structure should iterate while the value of gradeCounter is less than or<br />

equal <strong>to</strong> 10. Lines 20–21 correspond <strong>to</strong> the pseudocode statement “Input the next grade.”<br />

The statement on line 20 displays the prompt Enter integer grade: in the command<br />

window. The second statement (line 21) reads the value entered by the user, and s<strong>to</strong>res that<br />

value in the variable grade.<br />

Next, the program updates the <strong>to</strong>tal with the new grade entered by the user—line<br />

23 adds grade <strong>to</strong> the previous value of <strong>to</strong>tal and assigns the result <strong>to</strong> <strong>to</strong>tal—using<br />

the += assignment opera<strong>to</strong>r. Variable gradeCounter is incremented (line 25) <strong>to</strong> indicate<br />

that a grade has been processed. Line 25 adds 1 <strong>to</strong> gradeCounter, so the condition in<br />

the While structure eventually becomes false, terminating the loop. Line 29 assigns the<br />

results of the average calculation <strong>to</strong> variable average. Line 32 writes a blank line <strong>to</strong><br />

enhance the appearance of the output. Line 33 displays a message containing the string<br />

"Class average is " followed by the value of variable average.<br />

4.12 Formulating Algorithms with Top-Down, Stepwise<br />

Refinement: Case Study 2 (Sentinel-Controlled Repetition)<br />

Let us generalize the class-average problem. Consider the following problem:<br />

Develop a class-averaging program that averages an arbitrary number of grades each time<br />

the program is run.<br />

In the first class-average example, the number of grades (10) was known in advance. In this<br />

example, no indication is given of how many grades are <strong>to</strong> be input. The program must process<br />

an arbitrary number of grades. <strong>How</strong> can the program determine when <strong>to</strong> s<strong>to</strong>p the input<br />

of grades? <strong>How</strong> will it know when <strong>to</strong> calculate and print the class average?<br />

One way <strong>to</strong> solve this problem is <strong>to</strong> use a special value called a sentinel value (also<br />

called a signal value, a dummy value or a flag value) <strong>to</strong> indicate “end of data entry.” The<br />

user inputs all grades and then types the sentinel value <strong>to</strong> indicate that the last grade has<br />

been entered. Sentinel-controlled repetition is called indefinite repetition because the<br />

number of repetitions is not known before the loop begins its execution.

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

Saved successfully!

Ooh no, something went wrong!