<< 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

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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