home *** CD-ROM | disk | FTP | other *** search
/ Light / Light_Vol.1_August_1992_Datasphere_Publications_Disk_1_of_2_Side_A.d64 / arrays.c < prev    next >
Text File  |  2023-02-26  |  3KB  |  121 lines

  1. /*
  2.  
  3.    Arrays, Pointers & Structures - Chapter 3
  4.  
  5.    Below is an array of structures to hold upto 6 (0-5) names & phone numbers.
  6.    Firstly a structure with the tag "book" is defined, followed by a
  7.    declaration of an array of structure book called names.
  8.  
  9. */
  10.  
  11.  
  12. #include "stdio.h"
  13. #include "ctype.h"
  14. #define clear_screen printf("\223")
  15.  
  16. struct book {SHIFT-+} char surname[15];
  17.               char forename[20];
  18.               long phone;  {SHIFT--};
  19.  
  20. struct book names[6] = {SHIFT-+} "Smith", "Pete",633261,
  21.                          "Askwith", "Bob",471263,
  22.                          "Wilkinson", "Kerry",798624,
  23.                          "Fry", "Andy",623879,
  24.                          "Parker", "Chris",299311  {SHIFT--};
  25.  
  26.  
  27. main()
  28. {SHIFT-+}
  29.    int i,number,range=4;
  30.    static int totals[10];
  31.  
  32.    char *words[2];
  33.    char temp,choice;
  34.  
  35.    words[0] = "Computer";
  36.    words[1] = "Disk Drive";
  37.    words[2] = "Happy-Stick";
  38.  
  39.  
  40.    clear_screen;
  41.    printf("String Arrays");
  42.    printf("\n\nThe following words are held in the");
  43.    printf("\narray *words[3] :\n\n");
  44.  
  45.    for(i=0; i<3; i++)
  46.        {SHIFT-+}
  47.        printf("\nwords[%d] = %s",i,words[i]);
  48.        {SHIFT--}
  49.  
  50.    printf("\n\nPress a key...");
  51.    getchar();
  52.  
  53.    clear_screen;
  54.    printf("Number Arrays\n\n");
  55.  
  56.    for(i=0; i<10; i++)
  57.        {SHIFT-+}
  58.        totals[i] = i*i;
  59.        printf("\ntotals[%u] = %d",i,totals[i]);
  60.        {SHIFT--}
  61.  
  62. /* --- the %u outputs an unsigned integer. On
  63.        other examples displaying integers you
  64.        might have noticed a space in front of
  65.        each  number,  this  is left  for  the
  66.        minus sign when necessary.             ---*/
  67.  
  68.  
  69.  
  70.    printf("\n\nPress a key...");
  71.    getchar();
  72.  
  73.    do
  74.        {SHIFT-+}
  75.        clear_screen;
  76.        printf("Structures");
  77.        printf("\n\nEnter number (0-%u): ",range);
  78.        do
  79.           {SHIFT-+}                                                /* only accepts   */
  80.            scanf("%d",&number);                            /* numbers within */
  81.           {SHIFT--}                                                /* range          */
  82.        while(number <0 {CBM--}{CBM--} number >range);
  83.  
  84.  
  85.        printf("\n\nSurname   : %s",names[number].surname); /* displays array */
  86.        printf("\nForename  : %s",names[number].forename);  /* element to     */
  87.        printf("\nPhone     : %lu",names[number].phone);    /* screen         */
  88.  
  89.        printf("\n\nEnter any surname  : ");                /* accepts 6th    */
  90.        scanf("%s",names[5].surname);                       /* element        */
  91.        printf("\nEnter any forename : ");
  92.        scanf("%s",names[5].forename);
  93.        printf("\nEnter any phone no ");
  94.        printf("\n(99 to end)        : ");
  95.        scanf("%d",&names[5].phone);
  96.        range = 5;                                          /* range is now   */
  97.    {SHIFT--}                                                       /* 0 to 5         */
  98.    while (names[5].phone != 99);
  99.  
  100.  
  101.    printf("\n\nPress... S for binary Search,");
  102.    printf("\n         B for Bubble sort or,");
  103.    printf("\n       any for Menu");
  104.  
  105.    temp = getchar();
  106.    choice = toupper(temp);
  107.  
  108.    switch(choice) {SHIFT-+}
  109.  
  110.        case 'S' : printf("\n\nSearching...Loading binary search...");
  111.                   exec("search"); break;
  112.        case 'B' : printf("\n\nMore bubbles for your money...");
  113.                   exec("bubble"); break;
  114.        default  : printf("\n\nJust waiting for that menu to load....");
  115.                   exec("c-menu"); break;
  116.  
  117.                   {SHIFT--}
  118.  
  119.  
  120. {SHIFT--}
  121.  
  122.