home *** CD-ROM | disk | FTP | other *** search
/ Sams Teach Yourself C in 21 Days (6th Edition) / STYC216E.ISO / mac / Examples / Day08 / Ex08_08.c / Ex08_08.c
Encoding:
C/C++ Source or Header  |  2002-04-27  |  893 b   |  39 lines  |  [TEXT/LMAN]

  1. /* random.c: using a single-dimensional array */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. /* Declare a single-dimensional array with 1000 elements */
  6.  
  7. int random[1000];
  8. int a, b, c;
  9. long total = 0;
  10.  
  11. int main( void )
  12. {
  13.    /* Fill the array with random numbers. The C library */
  14.    /* function rand() returns a random number. Use one */
  15.    /* for loop for each array subscript. */
  16.  
  17.    for (a = 0; a < 1000; a++)
  18.    {
  19.       random[a] = rand();
  20.       total += random[a];
  21.    }
  22.    printf("\n\nAverage is: %ld\n",total/1000);
  23.    /* Now display the array elements 10 at a time */
  24.  
  25.    for (a = 0; a < 1000; a++)
  26.    {
  27.       printf("\nrandom[%d] = ", a);
  28.       printf("%d", random[a]);
  29.  
  30.       if ( a % 10 == 0 && a > 0 )
  31.      {
  32.          printf("\nPress Enter to continue, CTRL-C to quit.");
  33.          getchar();
  34.       }
  35.    }
  36.    return 0;
  37. }        /* end of main() */
  38.  
  39.