<http://lib.cnfolio.com/ENG421CustomDataTypes>
Introduction to Algorithms and Programming

Using custom data types



Symbolic constants


#define is a preprocessor directive that replaces each occurrence of a symbolic constant in the source code with replacement text. In the example below, the symbolic constants are NUMBER_OF_BOXES and MAX_WEIGHT and the replacement text are the numbers. Conventional practice is that symbolic constants are named using all uppercase letters. The replacement of text is performed before the source code is compiled.

#include <stdio.h>

#define NUMBER_OF_BOXES  20
#define MAX_WEIGHT       100

int main( void )
{
  int containerBoxes[ NUMBER_OF_BOXES ] = { 0 };
  int counter = 0;

  do
  {
    if ( scanf( "%d", &( containerBoxes[counter] ) ) == 1 )
    {
      if ( containerBoxes[counter] <= MAX_WEIGHT )
        counter++;
    }
  } while ( counter < NUMBER_OF_BOXES );
}




Symbolic constants defined using preprocessor directives are not variables!




Enumeration constants


Enumeration constants are a set of integers that are represented by identifier names. By default, the first identifier is assigned a zero value.

#include <stdio.h>

#define NUMBER_OF_BOXES  20
#define MAX_WEIGHT       100
#define WEIGHT_XLARGE    90
#define WEIGHT_LARGE     80
#define WEIGHT_STANDARD  40

enum boxSize { small, standard, large, xlarge };

int main( void )
{
  int containerBoxes[ NUMBER_OF_BOXES ] = { 0 };
  enum boxSize containerBoxSizes[ NUMBER_OF_BOXES ] = { small };
  int counter = 0;

  do
  {
    if ( scanf( "%d", &( containerBoxes[counter] ) ) == 1 )
    {
      if ( containerBoxes[counter] <= MAX_WEIGHT )
      {
        if ( containerBoxes[counter] > WEIGHT_XLARGE )
          containerBoxSizes[counter] = xlarge;
        else if ( containerBoxes[counter] > WEIGHT_LARGE )
          containerBoxSizes[counter] = large;
        else if ( containerBoxes[counter] > WEIGHT_STANDARD )
          containerBoxSizes[counter] = standard;
        else
          containerBoxSizes[counter] = small;

        counter++;
      }
    }
  } while ( counter < NUMBER_OF_BOXES );
}




struct data types


Structures are collections of related variables, which may be of many different data types. This is different from an array, which may contain only elements of the same data type.

New custom data types can be used anywhere that a basic data type can be used.

#include <stdio.h>

#define NUMBER_OF_BOXES  20
#define MAX_WEIGHT       100
#define WEIGHT_XLARGE    90
#define WEIGHT_LARGE     80
#define WEIGHT_STANDARD  40
#define MAX_NAME_LENGTH  64

enum boxSize { small, standard, large, xlarge };

struct boxDetails
{
  int weight;
  enum boxSize size;
  char owner[ MAX_NAME_LENGTH + 1 ];
};

int main( void )
{
  struct boxDetails containerBoxes[ NUMBER_OF_BOXES ];
  int counter = 0;

  do
  {
    if ( scanf( "%d", &( (containerBoxes[counter]).weight ) ) == 1 )
    {
      if ( (containerBoxes[counter]).weight <= MAX_WEIGHT )
      {
        if ( (containerBoxes[counter]).weight > WEIGHT_XLARGE )
          (containerBoxes[counter]).size = xlarge;
        else if ( (containerBoxes[counter]).weight > WEIGHT_LARGE )
          (containerBoxes[counter]).size = large;
        else if ( (containerBoxes[counter]).weight > WEIGHT_STANDARD )
          (containerBoxes[counter]).size = standard;
        else
          (containerBoxes[counter]).size = small;

        counter++;
      }
    }
  } while ( counter < NUMBER_OF_BOXES );
}




Custom data type short names


The keyword typedef provides a convenient mechanism for creating synonyms (or aliases) for previously defined data types. In effect, it allows the creation of custom data type short names.

For convenience, the typedef and structure keywords may be combined into one statement.

#include <stdio.h>

#define NUMBER_OF_BOXES  20
#define MAX_WEIGHT       100
#define WEIGHT_XLARGE    90
#define WEIGHT_LARGE     80
#define WEIGHT_STANDARD  40
#define MAX_NAME_LENGTH  64

typedef enum boxSize { small, standard, large, xlarge } bxSize;

typedef struct boxDetails
{
  int weight;
  bxSize size;
  char owner[ MAX_NAME_LENGTH + 1 ];
} bxBox;

int main( void )
{
  bxBox containerBoxes[ NUMBER_OF_BOXES ];
  int counter = 0;

  do
  {
    if ( scanf( "%d", &( (containerBoxes[counter]).weight ) ) == 1 )
    {
      if ( (containerBoxes[counter]).weight <= MAX_WEIGHT )
      {
        if ( (containerBoxes[counter]).weight > WEIGHT_XLARGE )
          (containerBoxes[counter]).size = xlarge;
        else if ( (containerBoxes[counter]).weight > WEIGHT_LARGE )
          (containerBoxes[counter]).size = large;
        else if ( (containerBoxes[counter]).weight > WEIGHT_STANDARD )
          (containerBoxes[counter]).size = standard;
        else
          (containerBoxes[counter]).size = small;

        counter++;
      }
    }
  } while ( counter < NUMBER_OF_BOXES );
}




Random collection of boxes


This sample program uses the random number generator to create a random collection of boxes.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUMBER_OF_BOXES  20
#define MAX_WEIGHT       100
#define WEIGHT_XLARGE    90
#define WEIGHT_LARGE     80
#define WEIGHT_STANDARD  40
#define MAX_NAME_LENGTH  64

typedef enum boxSize { small, standard, large, xlarge } bxSize;

typedef struct boxDetails
{
  int weight;
  bxSize size;
  char owner[ MAX_NAME_LENGTH + 1 ];
} bxBox;

int main( void )
{
  bxBox containerBoxes[ NUMBER_OF_BOXES ];
  int counter = 0;

  srand( time( 0 ) );

  for ( counter = 0; counter < NUMBER_OF_BOXES; counter++ )
  {
    (containerBoxes[counter]).weight = ( rand() % MAX_WEIGHT ) + 1;

    if ( (containerBoxes[counter]).weight > WEIGHT_XLARGE )
      (containerBoxes[counter]).size = xlarge;
    else if ( (containerBoxes[counter]).weight > WEIGHT_LARGE )
      (containerBoxes[counter]).size = large;
    else if ( (containerBoxes[counter]).weight > WEIGHT_STANDARD )
      (containerBoxes[counter]).size = standard;
    else
      (containerBoxes[counter]).size = small;

    printf( "%d - %d\n",
            (containerBoxes[counter]).weight,
            (containerBoxes[counter]).size );
  }
}




Practice examples


  1. Write a program to calculate the total shipping charges for 3 boxes.
  2. Write a program to save information about 6 units you are studying and display the information in a report format.