Array Tips

  1. When declaring an array, the size must be a constant, either a literal or a named constant. For example,
        int vector[100];
    
        const int ROWSIZE = 10;
        const int COLSIZE = 25;
        float matrix[ROWSIZE][COLSIZE];
        float vect[COLSIZE];
    
  2. An array can be initialized by placing the initial values within braces { }. When initializing a 2D array, the values are listed row by row. The values in each row can be enclosed within another set of braces, but they need not be. For example,
        int one[5] = {2,4,8,16,32};
        int two[3][4] = {{1,2,3,4},{2,4,6,8},{3,6,9,12}};
        int three[3][2] = {10,20,30,40,50,60};
    
  3. An individual array element can be used exactly like a scalar variable of the same type: it can be input, assigned, used in an expression, output, or passed as a parameter.

  4. A subscript must be an integer or an integer expression. If an expression is used, the value of the expression determines which array element will be referenced. For example:
        int i = 2;
        int nums[10];
        nums[0] = 1;        // stores the value 1 in element 0
        nums[2*i+1] = 50;   // stores the value 50 in element 5
    
  5. C++ will not prevent a program from referencing array elements beyond the bounds of the array. This results in unpredictable behavior: the program may appear to work correctly, it may give incorrect answers, or it may crash.

One Dimensional Arrays and Functions

  1. The parameter type for an array must include []. It is permissible to put a value in the brackets, but not required. For example, a function whose parm is an array of ints will have a prototype like the following:
        void example(int[]);
    
  2. Arrays are always passed by reference. An & is not required.

  3. A function does not know the size of an array that is passed as a parm, even if you put a size inside the brackets. The size must be passed as a separate parm. For example:
        void printarr(int arr[], int size)
        {
           int i;
           for (i = 0 ; i < size ; i++)
              cout << arr[i] << endl;
        }
    
  4. When passing an array to a function, only the array name is used. The calling statement should not include brackets if the entire array is being passed. For example, the printarr function above would be called as follows:
        int vector[6] = {51,40,75,48,60,46};
        printarr(vector,6);
    
  5. To pass an array element as a parameter, use a subscript to identify the element to be passed. For example:
        int vector[6] = {51,40,75,48,60,46};
        int small = min(vector[2],vector[5]);
    
        int min(int one,int two)
        {
           if (one < two)
              return one;
           return two;
        }
    
    In this example, the parameter one will be a copy of vector[2], which has the value 75, and the parameter two will be a copy of vector[5], which has the value 46. After the call to min, small will contain the value 46.

Two Dimensional Arrays and Functions

  1. The parameter type for an array must include brackets. It is permissible to put a value in the first set of brackets (for the number of rows), but not required. You must include a value in the second set of brackets for the number of columns. For example, a function whose parm is an array of ints with 10 rows and 20 columns will have a prototype like the following:
        void example(int[][20]);
    
  2. You must pass the number of rows and the number of columns as separate parameters. For example:
        void print2D(int arr[][COLS], int ROWS, int COLS)
        {
           int i, j;
           for (i = 0 ; i < ROWS ; i++)
              for (j = 0 ; j < COLS ; j++)
                 cout << arr[i][j] << endl;
        }
    
  3. When passing a 2D array to a function, only the array name is used. The calling statement should not include brackets if the entire array is being passed. For example, the print2D function above would be called as follows:
        int vector[2][4] = {20,51,40,75,48,13,60,46};
        printarr(vector,2,4);
    


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2004