Topical Information

The purpose of this quiz is to give you a chance to review your knowledge of topics involving 1D C-style arrays in C++.

Quiz Information

Questions

  1.  
    TRUE   FALSE   An array declaration tells the (base) type, name, and maximum size of the array.
    TRUE   FALSE   This maximum size is declared using the subscript operator.
    TRUE   FALSE   The array's (base) type can be any data type — even void.
    TRUE   FALSE   The array's maximum size is often a constant so that it may be easily changed if another size is desired at a later phase of the software development cycle.
  2. Write a function which, when called as 'average(arr, 3, 15)' will return the average of the elements of arr between positions 3 and 15 (inclusive).

    
        double average(const double a[], size_t low, size_t high)
        {
            double sum{0.0};
            for (size_t e = low; e <= high; ++e)
            {
                sum += a[e];
            }
            return sum / (high - low + 1);
        }
    
    
  3. If you subscript a 1D array no times (not at all), you are referring to _____ of the elements. If you subscript it once, you are referring to _____ of the elements.

    1. all, one YES
    2. the first, the last NO
    3. one, one NO
    4. all, all NO
    5. the first, the middle NO
  4. We need one loop(s) to process a 1D array. During the entry of array data elements, this is often a indefinite-style loop (since we don't know yet how many items there are). Once data is entered and we are processing the elements, the loop used is typically a definite/finite-style loop.

  5. What would be the result of the following code fragment? (i.e. What is stored in the variable where?)

        size_t locate(const long array[], size_t MAX, long find_me);
    
        ...some_function(...)
        {
            long heights[MAX] = { 15, 14, 17, 18, 17, 13, 15, 16 };
            size_t where;
    
            where = locate(heights, MAX, 17);
    
            return ...;
        }
    
        size_t locate(const long array[], size_t MAX, long find_me)
        {
            size_t i = 0;
            while ((i < MAX) && (array[i] != find_me))
            {
                i++;
            }
            return (i >= MAX) ? (-1) : (i);
        }
    
    
    
        where will contain the value 2 because the locate function looks
        for the desired value from the start of the list forward and stops at the
        first found occurrence.
    
    
  6. What does a const keyword mean when placed on a formal array argument of a function?

    
        It means that the elements of the array will not be changed during the
        execution of that function.  It modifies the base type of the array, in
        other words.
    
    
  7. Do you need to use an ampersand (&) on the formal argument to pass array arguments by reference?

    
        No.  Arrays are automatically allowed reference to the elements of the
        original array with no copying done.
    
    
  8. Rewrite the locate function above to search an array of double values instead of an array of long values. You need show only the lines of code that must change. (Hint: How do you compare double values for [non]equality? Is it just the operator?)

    
        size_t locate(const double array[], size_t MAX, double find_me)
            while ((i < MAX) && (abs(array[i] - find_me) > 1e-6))
    
    
  9. Write a function to read in an array of characters MAX_C long. Make sure to tell the caller how many values were actually read — this may be less than the MAX_C constant. (Hint: What character ends the input?)

    
        size_t read_chars(char a[], size_t MAX_C)
        {
            size_t e{0};
            char ch;
            while (cin.peek() != '\n' && e < MAX_C)
            {
                cin.get(ch);
                a[e] = ch;
                ++e;
            }
            if (e < MAX_C)
            {
                cin.ignore();
            }
            return e;
        }
    
    
  10. Should the MAX_C constant from the previous question be an argument to the reading function or a globally known constant?

    (Hint: Which would make your function more re-usable? More likely to be library-worthy? More likely to be called by another application? All these should lead you to the same answer!)

    
        It should be an argument to the function to make the function as reusable
        as possible!  If we tied it to a global constant, it would only work in that
        one application.
    
    
  11. Given an array declared as 'short arr[12]', answer each of the following:

    1. the array contains 12 positions for elements [number]

    2. the array's valid indices are between 0 and 11

    3. TRUE   FALSE   arr[3] accesses the 4th element
    4. TRUE   FALSE   arr[1] accesses the 1st element
    5. TRUE   FALSE   arr[] accesses the whole array
    6. TRUE   FALSE   arr accesses the whole array
    7. the data type of arr[4] is short

    8. the data type of arr is short[12]

  12. What are we saying when we leave the brackets empty on a formal array argument to a function?

    
        We need a 1D array but we don't care how long it might be.  Just tell us
        how many of the elements to process with a separate argument.
    
    

    If you fill in the [square] brackets (on a function's formal array argument) with a constant or literal, will this do anything? (Is it useful for the compiler? Is it useful for the caller? Will it hinder us in any way?)

    
        It isn't useful for anyone and might actually confuse some callers new to
        C-style arrays.  The compiler will ignore it — throwing it out, even!
        It's just extra typing for nothing but potential trouble.
    
    
  13. A vector in math is a sequence of numbers. This is almost directly analogous to a C-style array in C++ whose data type is numeric. To add two vectors, simply add all of their elements in sequence. If the two vectors are of different lengths, the result is undefined.

    For example:

    
        v1 = { 1 2 3 4 5 }
        v2 = { 5 3 2 4 1 }
        v3 = v1 + v2 = { 6 5 5 8 6 }
    
    

    You should be able to write this in a C++ program (using a function for vector addition) as:

    
        short v1[5] = { 1, 2, 3, 4, 5 },
              v2[5] = { 5, 3, 2, 4, 1 },
              v3[5];
        vector_sum(v3, v1, v2, 5);
        // now v3 is { 6 5 5 8 6 }
    
    

    Write such a function to add two vectors. (You can choose a different type besides short for the array elements, if you like.)

    
        void vector_sum(short v3[], const short v1[], const short v2[], size_t len)
        {
            for (size_t e{0}; e < len; ++e)
            {
                v3[e] = v1[e] + v2[e];
            }
            return;
        }