home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / imc9102.zip / DYNARRAY.C next >
Text File  |  1991-01-08  |  3KB  |  86 lines

  1. /****************************************************************
  2. *    DYNARRAY.C - a demonstration of a dynamic array                *
  3. *                                                                *
  4. *    To compile with dynamic array:  cl /DDYNAMIC dynarray.c        *
  5. *    To compile with static array:   cl dynarray.c                *
  6. ****************************************************************/
  7.  
  8. #include <stdio.h>                /* printf()                        */
  9. #include <stdlib.h>                /* qsort()                        */
  10. #include <string.h>                /* strcpy()                        */
  11.  
  12. struct Record
  13.    {
  14.    char Name[30];                /* Person's name                */
  15.    int  Ext;                    /* Telephone extension            */
  16.    };
  17.  
  18. #ifdef DYNAMIC    /* If dynamic array, declare pointer to array    */
  19.     struct Record *Data=(struct Record *)NULL;
  20. #else            /* If not dynamic array, declare entire array    */
  21.     struct Record Data[100];
  22. #endif
  23.  
  24. /****************************************************************
  25. *    RecordCompare - compare two Record structures, and return    *
  26. *    -1 if the first record should be first, +1 if the second    *
  27. *    record should be first, and 0 if it doesn't matter            *
  28. ****************************************************************/
  29. int RecordCompare(void *a, void *b)
  30.     {
  31.     if ( ((struct Record *)a)->Ext < ((struct Record *)b)->Ext )
  32.         return -1;
  33.     if ( ((struct Record *)a)->Ext > ((struct Record *)b)->Ext )
  34.         return 1;
  35.     return 0;
  36.     }
  37.  
  38. /****************************************************************
  39. *    main - initialize the Data array, sort it by the telephone    *
  40. *    extension field, and print the telephone list                *
  41. ****************************************************************/
  42.  
  43. void main(void)
  44.     {
  45.     int i;
  46.  
  47. #ifdef DYNAMIC    /* If we use dynamic array, allocate RAM for it    */
  48.     Data = (struct Record *)calloc(9,sizeof(struct Record));
  49.     if (Data == NULL)
  50.         {
  51.         fputs("ERROR! couldn't allocate enough RAM!\n",stderr);
  52.         exit(1);
  53.         }
  54. #endif
  55.  
  56.     /* Handy shorthand for initializing the array */
  57. #define INITIALIZE(y,z) strcpy(Data[i].Name,y);Data[i].Ext=z; i++
  58.  
  59.     /*** Set up the data in the array ***/
  60.     i=0;
  61.     INITIALIZE("Marco C. Mason",    566);
  62.     INITIALIZE("Mark W. Crane",        531);
  63.     INITIALIZE("Jody Gilbert",        507);
  64.     INITIALIZE("Duane Spurlock",    528);
  65.     INITIALIZE("Tara Billinger",    539);
  66.     INITIALIZE("Tim Landgrave",        556);
  67.     INITIALIZE("Douglas Cobb",        523);
  68.     INITIALIZE("Maureen Pawley",    508);
  69.     INITIALIZE("Karl Feige",        559);
  70.  
  71.     /*** Sort the list by telephone extension ***/
  72.     qsort(Data, 9, sizeof(struct Record),RecordCompare);
  73.  
  74.     /*** Print the resulting phone list ***/
  75. #ifdef DYNAMIC
  76.     puts("Phone list output (using DYNAMIC array)");
  77. #else
  78.     puts("Phone list output (using STATIC array)");
  79. #endif
  80.  
  81.     puts("\nRecord  Telephone\nNumber  Extension  Name");
  82.     for (i=0; i<9; i++)
  83.         printf("  %02d       %03d     %s\n",
  84.                   i+1,    Data[i].Ext,   Data[i].Name);
  85.     }
  86.