A gentle introduction to using structures

A gentle introduction to using structures

In the previous section, Why learn to use structures?, we stated that structures are containers that can hold different variables. We used the analogy of structures being like a grocery bag which provides a convenient container for holding many similar items that would be unwieldy to handle as individual items.

Defining a structure

Unlike a grocery bag, structures are custom fitted for the variables that you would like to incorporate. The first step in using a structure is creating these custom variable containers for the structure. This is called the structure definition. Here is an example structure definition.

struct descriptiveStats {
   matrix mean;
   matrix max;
   matrix min;
   matrix stdDev;
};

The structure definition has two essential pieces. The first is the structure name, in this case “descriptiveStats”. The name is also referred to as the type of the structure. The second piece of the structure is the list of member variables.

Each member variable is defined with a variable type and a name. In this example all of the member variables are of type matrix, but you may make member variables that are strings, string arrays, multi-dimensional arrays or even other structures. The member variable name is used to access the member variable and should have a name that provides some description of its intended contents.

Declaring a structure

The next step in using a structure is to create a specific instance of a structure. Dog breeding provides a simple and informative analogy. All dog breeding associations provide the definition of German Shepherd or Yorkshire Terrier. This is like the structure definition. If your neighbor has a Yorkshire Terrier named Rover, Rover would be analogous to the structure instance. You create a structure instance with a declaration statement like this:

struct descriptiveStats dsGDP;

After declaring the structure instance, the members of the structure may be assigned and used just like any other variable in GAUSS. To access them you reference the name of the structure instance, followed by the dot operator and the name of the member variable that you would like to use. You can set the “mean” member of the “dsGDP” structure member we created above like this:

dsGDP.mean = 4.3;

The other members may be set and referenced in the same manner. Here is an example procedure that takes in a column vector, computes some descriptive statistics and returns them in an instance of the descriptiveStats structure that we created above.

proc (1) = getDescStats(A);
   struct descriptiveStats retStats;
   retStats.mean  = meanc(A);
   retStats.max  = maxc(A);
   retStats.min = minc(A);
   retStats.stdDev = stdc(A);
   retp(retStats);
endp;

We will end this section by putting everything together and create a small self-contained program that can be run in GAUSS after copying and pasting it into a file.

//Define structure
struct descriptiveStats {
   matrix mean;
   matrix max;
   matrix min;
   matrix stdDev;
};

//Declare structure instance
struct descriptiveStats dsGDP;

//Create a matrix on which to calculate descriptive stats
A = rndn(100, 1);

//Calculate the stats and assign to the structure instance
dsGDP = getDescStats(A);

//Print the output
print “Mean =                “  dsGDP.mean;
print “Maximum =             “ dsGDP.max;
print “Minimum =             “ dsGDP.min;
print “Standard Deviation  = “ dsGDP.stdDev;

//Define the procedure
proc (1) = getDescStats(A);
   struct descriptiveStats retStats;
   retStats.mean  = meanc(A);
   retStats.max  = maxc(A);
   retStats.min = minc(A);
   retStats.stdDev = stdc(A);
   retp(retStats);
endp;

Summary

  1. The steps required to use a structure are:
    1. Define the structure
    2. Declare a structure instance
    3. Assign its member variables.
  2. Structure members are accessed by using the structure instance name followed by the dot operator and the member variable’s name.
  3. Structure members may be used in exactly the same way as any other variable of the same type.