Send Close Add comments: (status displays here)
Got it!  This site "www.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 arrays
by RS  admin@robinsnyder.com : 1024 x 640


1. C: Procedural abstraction using arrays
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.

The following program uses an array to represent all the needed string variables. In C, a string is represented by an array of characters. So the program uses a 2D array. The offset index into the array is the string variable used. In the following program, no constants (or pseudo-constants) are used, just literal values. This approach will be improved in the following programs using straight-forward abstraction techniques - the main objective of this progression.

Compare this program with the programs developed in Lua: Procedural abstraction using variables .

The following correspondence is used.

  • Element data1[0] represents the text for NAME.
  • Element data1[1] represents the text for SIZE.
  • Element data1[2] represents the text for ANIMAL.
  • Element data1[3] represents the text for FUR.
  • Element data1[4] represents the text for COLOR.
  • Element data1[5] represents the text for OBJECT.


  • Note that this is the way that, internally, the run-time system of a compiled program handles local variables allocated on the stack - by the offset of the variable into memory. 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. Note the use of the define for the abstracted literals (as previously covered). This results in the following correspondence.
  • Element data1[RS_NAME] represents the text for NAME.
  • Element data1[RS_SIZE] represents the text for SIZE.
  • Element data1[RS_ANIMAL] represents the text for ANIMAL.
  • Element data1[RS_FUR] represents the text for FUR.
  • Element data1[RS_COLOR] represents the text for COLOR.
  • Element data1[RS_OBJECT] represents the text for OBJECT.
  • 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