CS21 Week5 Tuesday In-Class Exercises

  1. Make a week5 subdirectory in your cs21 directory:
        % cd
        % cd cs21
        % mkdir week5
        % cd week5
        % pwd
        /home/your_user_name/cs21/week5
    

  2. Copy over these files into your week5 subdirectory:
        % cp /home/newhall/public/cs21/week5/* .
    

  3. Open initarray.c and add the variable declaration and the code to initialize an array of int such that the elements in the array are:
    -----------------------------------------------------------------------------  
    |  0  |   1   |   4  |   9  |  16  |  25  |  36  |  49  |  64  |  81 |  100 | 
    -----------------------------------------------------------------------------  
    

    Next, add code to print out the array contents.

    Next, edit the printing loop in this program so that i starts at -5 and continues until 5 past the highest valid index. We would hope that when we try to compile this version of the program that we would get an error for using negative values for array indices. Unfortunately, most C compilers do not check that the array index is in bounds. Compile and run the program. What happens?

  4. We will go over the code in arrayfunccallexample.c together. Open the file in emacs (or vi).