Send Close Add comments: (status displays here)
Got it!  This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
C: Procedural abstraction using records
by RS  admin@robinsnyder.com : 1024 x 640


1. C: Procedural abstraction using records
The example used here is that of abstracting the words of a children's song.

In all cases, input, processing, and output should be separated. Note: Debugging output does not count as output that needs to be separated.

2. Background
For background on the problem, see the following. In addition, check out all related class notes.

3. Initial program
The initial program does not use much abstraction. Here is the C code [#1]


4. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


5. Procedure abstraction
Let us now perform some procedural abstraction to the above program. Note: For this simple example, there is no significant processing part but that part is still included. Here is the C code [#2]


6. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


7. Repetition removal
There is a common repeated pattern in the input statements in inputGet1.

This pattern is abstracted in the following program by introducing the inputGet2 function. Here is the C code [#3]


8. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


9. Code to data driven programming
We can improve our code by going from code-driven programming to data-driven programming. This is done with the use of a constant literal array called NAMES1 - all uppercase since it is a constant (a named literal).
char* NAMES1[RS_DMAX] = {"Name","Size","Animal","Fur","Color","Object"};

This results in the following correspondence.

  • Element NAMES1[RS_NAME] represents the text "Name".
  • Element NAMES1[RS_SIZE] represents the text "Size".
  • Element NAMES1[RS_ANIMAL] represents the text "Animal".
  • Element NAMES1[RS_FUR] represents the text "Fur".
  • Element NAMES1[RS_COLOR] represents the text "Color".
  • Element NAMES1[RS_OBJECT] represents the text "Object".


  • The use of NAMES1 allows the literal text to be abstracted to one place - the constant literal array NAMES1. Here is the C code [#4]


    10. Examples of input and output
    Here are some examples of input and output for the above program code.

    Here is an example input.

    For the above example input, here is the expected output.

    Here is an example input.

    For the above example input, here is the expected output.

    Here is an example input.

    For the above example input, here is the expected output.


    11. End of page

    by RS  admin@robinsnyder.com : 1024 x 640