Phonetics Sensaphone ISACC 5000 Manuale Utente

Pagina di 143
103
Chapter 8:  C Programming
ARRAYS
Arrays allow you to store a lot of related information in a convenient, organized fashion.  An
array lets you use one line of code to create a series of variables.   These variables share the
same basic name and are distinguished from one another by a numerical tag.
Example:
int count[10];
This means that an array named count has 10 members or “elements,” with each element
having its own value, starting with 0 and ending with 9.  The first element is count[0], the
second element is count[1], and so on up to count[9].  The type int means that the actual
numerical value of each element is an integer.
Example:
count[2] = 7
count[4] = 131
count[9] = 26
SAMPLE PROGRAM:
This program calculates a one-hour average temperature.  The array named "numbers" sets up
a series of variables from 0 to 60 to hold a value for input 1 for each minute in an hour.  The
60 values are totalled, then averaged.  The value of input 2 is then set to this average.
By using an array, the code becomes substantially more concise.  The program is first listed,
then followed by a section by section explanation of how it works.
int numbers[60];  /* array: input 1 value for each minute */
int x;            /* index to the array */
int total;        /* total of the input 1 values */
int average;      /* total/60 */
int oldminute;    /* minute counter */
main()
  {
  if (oldminute != minutes)
    {
    oldminute = minutes;
    numbers[minutes] = input(1);
    total = 0;
    for (x=0;x<60;x=x+1)
      {
      total = total + numbers[x];
      }
    average = total/60;
    if ((total % 60) >= 30)
      {
      average = average + 1;
      }
    set_input(2,average);
    }
  }