In Class Week7: Tuesday

  1. Copy the files for today's class into your week7 directory, then change into that directory and check the contents:
         % cd ~/cs21/
         % mkdir week7
         % cd  week7
         % cp ~newhall/public/cs21/week7/*  .
         % ls
    

  2. Open the file 2d-arrays.c in emacs or vim and read through the code. Write the function InitSquareArray, which takes a 2-dimensional array, a number of rows (the effective size of rows in the 2-D array), and initializes all of the elements to a particular value. In order to test your function, you'll also have to write the function PrintSquareArray, which prints out an array of effective size n. For example:
         % gccx -o array 2d-arrays.c
         % ./array
         Enter a size from 1-20: 10
         Enter an initial value: 0
    
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
           0  0  0  0  0  0  0  0  0  0
    
           % ./array
           Enter a number of rows in square matrix from 1-20: 5
           Enter an initial value: 0
    
           0  0  0  0  0 
           0  0  0  0  0 
           0  0  0  0  0 
           0  0  0  0  0 
           0  0  0  0  0 
    

  3. Change your program so that InitSquareArray initializes the array as shown below, instead of using a number provided by the user:
         % ./array
         Enter a number from 1-20: 10
           0  1  2  3  4  5  6  7  8  9
           1  2  3  4  5  6  7  8  9 10
           2  3  4  5  6  7  8  9 10 11
           3  4  5  6  7  8  9 10 11 12
           4  5  6  7  8  9 10 11 12 13
           5  6  7  8  9 10 11 12 13 14
           6  7  8  9 10 11 12 13 14 15
           7  8  9 10 11 12 13 14 15 16
           8  9 10 11 12 13 14 15 16 17
           9 10 11 12 13 14 15 16 17 18
    

  4. What happens if you change InitSquareArray's first function parameter to be "int array[][]" instead of "int array[][MAX_SIZE]"? Make this change and find out.