<< Chapter < Page Chapter >> Page >

One way to build program is to connect the exit point of one control structure to the entry point of the next. This way is called control-structure-stacking.

Another way is to place one control structure inside another control structure. This way is called control-structure-nesting .

Consistent applying reasonable indentation conventions throughout your programs greatly improves program readability. We suggest a fixed-size tab of about ¼ inch or three blanks per indent.

For example, we indent both body statements of an if..else structure as in the following statement:

if (grade>= 60)

cout<<“Passed”;

else

cout<<“Failed”;

Top-down stepwise refinement

Using good control structures to build programs is one of the main principles of structured programming. Another principle of structured programming is top-down, stepwise refinement.

Consider the following problem:

Develop a class-averaging program that will process an arbitrary number of grades each time the program is run.

We begin with a pseudocode representation of the top:

Determine the class average for the exam

Now we begin the refinement process. We divide the top into a series of smaller tasks and list these in the order in which they need to be performed. This results in the following first refinement.

First Refinement:

Initialize variables

Input, sum and count the exam grades

Calculate and print the class average

Here only the sequence structure has been used.

To proceed to the next level of refinement, we need some variables and a repetition structure. We need a running total of the numbers, a count of how many numbers have been processed, a variable to receive the value of each grade as it is input and a variable to hold the calculated average. We need a loop to calculate the total of the grades before deriving the average. Because we do not know in advance how many grades are to be processed, we will use sentinel-controlled repetition. The program will test for the sentinel value after each grade is input and will terminate the loop when the sentinel value is entered by the user. Now we come to the pseudocode of the second refinement.

Second Refinement:

Input the first grade(possibly the sentinel)

While the user has not as yet entered the sentinel

Add this grade into the running total

Input the next grade(possibly the sentinel)

Calculate and print the class average

The pseudocode statement

Calculate and print the class average

can be refined as follows:

If the counter is not equal to zero

set the average to the total divided by the counter

print the average

else

Print “No grades were entered”.

Notice that we are being careful here to test for the possibility of division by zero – a fatal error, if undetected, would cause the program to fail. Now we come to the pseudocode of the third refinement.

Third Refinement:

Initialize total to zero

Initialize counter to zero

Input the first grade

While the user has not as yet entered the sentinel

Add this grade into the running total

Add one to the grade counter

Input the next grade

If the counter is not equal to zero

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask