Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Let us look at procedure abstraction using a children's nursery rhyme as an example.
Note: Using a simple song has the advantage that the output is known and easily recognizable while one is programming the abstractions.
2. Lyrics
Here are the lyrics to the song, "Mary had a little lamb".
Mary had a little lamb,
little lamb, little lamb
Mary had a little lamb,
it's fleece was white as snow.
And everywhere that Mary went,
Mary went, Mary went.
Everywhere that Mary went,
the lamb was sure to go.
3. Print statements
Obviously, we could output this text using print statements as follows.
4. Program
Here is a program to do just that.
Here is the C code.
The output is as expected.
Here is the output of the C code.
5. New requirement
But suppose we also want the following output.
Johnny had a tiny pig,
tiny pig, tiny pig
Johnny had a tiny pig,
it's hide was black as coal.
And everywhere that Johnny went,
Johnny went, Johnny went.
Everywhere that Johnny went,
the pig was sure to go.
And then the following requirement.
Susan had a hopping rabbit,
hopping rabbit, hopping rabbit
Susan had a hopping rabbit,
it's fur was brown as earth.
And everywhere that Susan went,
Susan went, Susan went.
Everywhere that Susan went,
the rabbit was sure to go.
And there may be more in the future.
6. Abstractions
Do you see that there are similarities and differences. Some abstractions are in order.
A common example of such requirements are a web page system that must generate certain output or reports based on data in a database.
7. Mail merge
A mail-merge process using, say, a template in Microsoft Word and a database of differences in an Excel worksheet is another example.
In the mail merge process, the differences (fields from each database record) are merged into the similarities (document with fill-in fields) to create the collection of documents merged documents
8. Table of differences
From the above text, one should be able to visually see and identify the differences. Here is a table of the above differences.
Names for differences
Name
v. 1
v. 2
v. 3
NAME
Mary
Johnny
Susan
SIZE
little
tiny
hopping
ANIMAL
lamb
pig
rabbit
FUR
fleece
hide
fur
COLOR
white
black
brown
OBJECT
snow
coal
earth
Note that the generic name given the difference need not match all instances. It is sufficient that it has some general meaning for the programming process.
9. Color-coded differences
Suppose one does not see the differences. Here is a color-coded table of differences.
Names for differences
Name
v. 1
v. 2
v. 3
NAME
Mary
Johnny
Susan
SIZE
little
tiny
hopping
ANIMAL
lamb
pig
rabbit
FUR
fleece
hide
fur
COLOR
white
black
brown
OBJECT
snow
coal
earth
And here are the above versions of the text, color-coded.
10. Version 1
Mary had a little lamb ,
little lamb , little lamb Mary had a little lamb ,
it's fleece was white as snow .
And everywhere that Mary went,
Mary went, Mary went.
Everywhere that Mary went,
the lamb was sure to go.
11. Version 2
Johnny had a tiny pig ,
tiny pig , tiny pig Johnny had a tiny pig ,
it's hide was black as coal .
And everywhere that Johnny went,
Johnny went, Johnny went.
Everywhere that Johnny went,
the pig was sure to go.
12. Version 3
Susan had a hopping rabbit ,
hopping rabbit , hopping rabbit Susan had a hopping rabbit ,
it's fur was brown as earth .
And everywhere that Susan went,
Susan went, Susan went.
Everywhere that Susan went,
the rabbit was sure to go.
13. Table of difference's
Here, again, is the color-coded table of differences.
Names for differences
Name
v. 1
v. 2
v. 3
NAME
Mary
Johnny
Susan
SIZE
little
tiny
hopping
ANIMAL
lamb
pig
rabbit
FUR
fleece
hide
fur
COLOR
white
black
brown
OBJECT
snow
coal
earth
14. Input
We can now created input data sets for each of the above (and other, if necessary) versions.
15. Logical input for version 1
Mary little lamb fleece white snow
16. Logical input for version 2
Johnny tiny pig hide black coal
17. Logical input for version 3
Susan hopping rabbit fur brown earth
18. Actual input
Above is the logical input. To become physical input, some decisions need to be made.
Are there other data in the input, such as a debug output setting.
How is the above data recognized: counter loop, header loop, trailer loop (what is the trailer value), endfile loop, or some other way.
19. Solutions
Some ways of creating a program to take the input data and output the text to each of the songs using that data will now be covered. The following approaches will be used.