ANSWERS TO EXERCISES ON ARRAYS


 1. (a)  Equivalent to a[1000]. This is a convenient way to name the size of
         the array at *compile* time, so we can use the name in for loops and
         elsewhere in the program.  If we want to change the size, we only have
         to change it in the define line. The size is constant at *run* time:
         N doesn't change when the program runs.

    (b)  Equivalent to a[1000000]. Simple expressions involving defined variables
         can be evaluated at *compile* time. In ancient times (10 years ago), not 
         many computers could hold such a big array; nowadays most can.

    (c)  Equivalent to a[1000000000]. Might work; but your computer probably
         can't hold such a big array.  Exact behavior depends on the compiler.

          % cat bigarray.c
          #include <stdio.h>
          #define N 1000
          int main(void) {
              int a[N*N*N], i;
              for (i = 0; i < N*N*N; i++)
                a[i] = 0;
              return 0;
          }

          % cc bigarray.c 
          "bigarray.c", line 5: array dimension too big
          cc: acomp failed for bigarray.c
     
          % lcc bigarray.c
          bigarray.c:5: size of `array of int' exceeds 2147483647 bytes

          % gcc bigarray.c
          bigarray.c: In function `main':
          bigarray.c:5: size of array `a' is too large



 2.  1
                          j:  0  1  2  3  4  5  6  7  8  9
        a[j] after 1st loop:  9  8  7  6  5  4  3  2  1  0
        a[j] after 2nd loop:  0  1  2  3  4  4  3  2  1  0

     This is a bit tricky. To see why it's not 8, consider the
     contents of a[] after each step in the second for loop.

                          j:  0  1  2  3  4  5  6  7  8  9
 
                      i = 0   0  8  7  6  5  4  3  2  1  0  
                      i = 1   0  1  7  6  5  4  3  2  1  0  
                      i = 2   0  1  2  6  5  4  3  2  1  0  
                      i = 3   0  1  2  3  5  4  3  2  1  0  
                      i = 4   0  1  2  3  4  4  3  2  1  0  
                      i = 5   0  1  2  3  4  4  3  2  1  0  
                      i = 6   0  1  2  3  4  4  3  2  1  0  
                      i = 7   0  1  2  3  4  4  3  2  1  0  
                      i = 8   0  1  2  3  4  4  3  2  1  0  
                      i = 9   0  1  2  3  4  4  3  2  1  0  

3.  * need to #include 
    * void main() -> int main(void)
    * a[99] -> a[100]  to accomodate numbers between 0 and 99
    * num -> &num in scanf()
    * need to initialize array a[] to 0 before using
    * need to initialize maxi to 0 before using
    * extraneous semicolon at end of for() line