************* BACK AND FORTH PAGING SUBROUTINE ************

PAGEFLOP

If they want to quit, do so
 If They want to quit subsession, Do SO!
  Decrement LOCAL and GLOBAL Page Counters

FORWARD

  Increment LOCAL and GLOBAL Page Counters
   *************** END PAGEFLOP *********************
  ****** CHAPTER INTERNAL SELECTOR ALGORITHM ******

SELECTOR

   Reset Local Page Counter

SELAGAIN

 CHECK IF ONLY A <CR>

WRONGSEL

YOUR SELECTION IS NOT IN THE 0 - #SELIMIT  RANGE  Please try again

AANVANG

Copyright CourseWare Technologies Inc., 1985-88


Lesson - 6


 ARRAYS IN C 

P1

Arrays|Topics to Learn|6-0|9,34
  #             Topic
 ---           -------
  1   -   Declaring Arrays
  2   -   Arrays as Arguments
  3   -   Array Indexing
  4   -   Array Initialization
  5   -   Array Operations
  6   -   Lesson Review
  0   -   Return to the Main Menu

P2

Arrays|Declaring Arrays|6-1.1|9,54
     An array is a collection of variables with
identical data types that share a common name.


     An array is declared by specifying the array 
name followed by one or more pairs of square brackets 
([]) with dimension subscripts specifying the number
of items in each dimension (like rows and columns in a 
two-dimensional array).

P3

Arrays|Declaring Arrays|6-1.2|13,54
The following example shows array declarations.  The
first is a one-dimensional array, ar_1d, which can hold
4 elements of type int.  The second is a two-dimensional 
array, ar_2d, which has 3 rows and 4 columns and can,
therefore, hold 3*4 = 12 elements of type float.  The
third is a 4-dimensional array, ar_4d, which can hold 
420 elements of type float.  

/* Array Declarations */

  int ar_1d[4];                /* 1-dim, 4 elements */
  float ar_2d[3][4];          /* 2-dim, 12 elements */
  float ar_4d[4][3][5][7];   /* 4-dim, 420 elements */

P4

Arrays|Arrays as Arguments|6-2.1|10,54
The next example shows the function main() calling
funct(), a function that uses the call by value 
method of argument passing.  The values, not their
addresses, are being passed to the called function.  
In such a case, the function funct() will make its 
own copies of the argument(s) in the area known to
the funct() only.  Any variable modification made by
funct() will be invisible to the main().  Thus, funct()
will print the value of v as 210, while main() will 
print the value of volume as 0.

P5

Arrays|Arrays as Arguments|6-2.2|18,52
/* Call by value information passing */
      main()
      {
         int volume, len, height, width;
              :
         volume = 0; len = 0; height = 3; width = 7;
         funct(volume, len, height, width);
         printf ("the volume = %d\n", volume);
      }
      funct(v, l, h, w)
         int v, l, h, w; /* values, thus funct() will  */
      {                  /* make its own copies        */
         v = 1 * h * w;
         printf ("v=%d\n", v);
      }

NOTE:  In main(), the printed value of volume will be 0,
while in funct(), the printed value of v will be 210!

P6

Arrays|Arrays as Arguments|6-2.3|8,54
     A default method for argument passing is referred 
to as call by value, where the value of a variable 
rather than its address is passed.


     Exceptions to this rule are arrays.  If the array
variable appears in the argument list, the array address,
and not its value, is passed by default.

P7

Arrays|Arrays as Arguments|6-2.4|18,58
The following example depicts a common situation.  A
character string (array) is passed to the function 
str_upper() which converts a string to uppercase. 
In the argument declaration part of the function, the 
dimensions of the array were not specified.  By default, 
when an array is an argument, it is the address of the 
array, not its values, that is passed.  Thus, specifying 
the array dimension in the argument declaration of 
str_upper() would have changed nothing.  str_upper() 
does the conversion to uppercase.  It takes the integer 
value of the character and adds to it the difference 
between the integer value of 'A' and 'a'.

The same effect is achieved by using the difference
between 'D' and 'd', etc.  Using a constant value for 
the difference is a dangerous approach since 'A'-'a' 
will not produce the same result on all machines. However, 
'A'-'a' will always equal 'D'-'d' or 'U'-'u', etc.

P8

Arrays|Arrays as Arguments|6-2.5|17,48
/* using an array as an argument    */
#define NAME_LEN   25   /* max length  */

main()
{
   char   name[NAME_LEN + 1];
      :
      :
   str_upper(name);   /* CONVERT TO UPPER  */
}
str_upper(s)     /* convert string S to       */
   char s[];     /* upper case, array version */
{
   for (i = 0; s[i] != '\0'; i++)
     if (s[i] >='a' && s[i] <='z')
       s[i] = s[i]+'A' -'a';
}

P9

Arrays|Array Indexing|6-3.1|17,54
     To access array elements, one uses indices; 
however, one must not use indices outside the array's 
boundaries.

The following example shows the declaration and use
of three arrays:  cust_name, which can hold up to 26
elements of type char, zip_table, which can hold 
3*50 = 150 elements of type int, and cost_table which
can hold 4*17 = 68 elements of type float.  Since the 
array index in C varies between 0 and n-1, the legal 
indices for cust_name are between 0 and 25, for zip_table 
the row index is between 0 and 2 and the column index 
is between 0 and 49, and finally for cost_table, the
row index may vary between 0 and 3, and the column
index may vary between 0 and 16.  Based on these facts,
dest_code can be between 0 and 3 and stamp_code can be
between 0 and 16.

P10

Arrays|Array Indexing|6-3.2|14,48
/*  Array(s) indexing  */

char cust_name[26];         /* declaration */
int zip_table[3][50];
float cost_table[4][17];

/*  legal indexing  */
c=cust_name[7];
cur_zip = zip_table[2][27];
stamp_cost = cost_table[dest_code][ stamp_code];

/*  illegal indexing  */
c=cust_name[-1] ;
cur_zip = zip_table[5][61];

P11

Arrays|Array Initialization|6-4.1|8,50
     Arrays may be initialized only if they are 
declared as external.


     When initializing a one-dimensional array,
dimension need not be declared; however, for a 
multidimensional array, all dimensions except 
the first must be specified.

FIG3

Arrays|Array Initialization|6-4.2|8,55
A one-dimensional array and a multi-dimensional array
are illustrated next.  

Note that for the one-dimensional array, ar_1d[], the 
amount of memory allocated will be the same whether 
the dimesion subscript is used or not IF the number 
of elements being initialized equals the dimension 
subscript (in this case, 4). 

P13

Arrays|Array Initialization|6-4.3|16,47
/* one-d array initialization */

int ar_1d[]={0, 7, -15, 4};
------ IS THE SAME AS ------
int ar_1d[4]={0, 7, -15, 4};


/* multi-d array initialization */

float ar_2d[][4] = {{2.0, 3.75, 4.9, 5.0}
                      {2.4, 3.6, 5.4, 6.5},
                      {0.2E-7, 3.5, 0.1, -.3}};
(NOTE: braces determine the number of columns)
---------------IS THE SAME AS ------------------
float ar_2d[3][4] = {2.0, 3.75, 4.9, 5.0, 2.4,
         3.6, 5.4, 6.5, 0.2E-7, 3.5, 0.1, -.3};

P14

Arrays|Array Initialization|6-4.4|7,45
     Note that an initialization such as:

     float ar_2d[3][4] = {{2.0}, {2.4},
                             {0.2E-7}};

will initialize the first column of ar_2d, 
and set the rest of entries to zero.

P15


Arrays|Array Operations|6-5.1|8,54
In the following example we have two arrays of type int.  
The assignment b=a; is illegal because an array is not a 
variable, it is a constant.  On the other hand by referring
to the array name, we're referring to the array address 
rather than it's values.

Copying one element of one array into an element of another 
array is a legal process.

P16

Arrays|Array Operations|6-5.2|13,47
/* Copying elements from one array into another
is done one element at a time. */

   int a[15], b[15], i;

   /* wrong element assignment */

    b = a;   /* address to an array variable */

   /* proper array element assignment */

    for (i=0; i < 15; i++)
       b[i] = a[i];