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 variables
by RS  admin@robinsnyder.com : 1024 x 640


1. C: Procedural abstraction using variables
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. Individual variables
This solution uses individual variables.

4. Initial program
The initial program does not use much abstraction.

The following program uses just individual string variables. In C, a string is represented by an array of characters. Here is the C code [#1]


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


6. 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]


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


8. 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]


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


10. 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]


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


    12. End of page

    by RS  admin@robinsnyder.com : 1024 x 640