CSCI 201 Study Aid: Arrays

The Array concept.

Array usage

Simple array processing.

Array Initialization

Arrays as parameters

Exercises

  1. When declaring an array, for example
     
      float the_array[12]; 
    
    the number specified in the square brackets is:
    1. The size of the array.
    2. One more than the size of the array.
    3. One less than the size of the array.
    4. The index of the last element of the array.

    solution

  2. The first element in an array of size n always has an index of:
    1. 1
    2. 0
    3. n
    4. n-1

    solution

  3. This fragment of code attempts to print the elements of an array. What is logically wrong with the code?
     
      char message[35]; 
      int i = 0; 
      while( i <= 35 )  { 
        cout << message[i]; 
        i++; 
      } 
    
    1. The first character printed is element 1.
    2. The loop does not traverse the whole array.
    3. There is no newline printed after each element.
    4. The loop processes past the end of the array.

    solution

  4. Why are arrays in C++ (and C) indexed from 0?
    1. Because it's a natural place to start.
    2. Because Kernighan and Ritchie had no fingers.
    3. Because that's the way it's gotta be.
    4. Because the indexes represent offsets from the first element.

    solution

  5. Write a program, to declare an integer array of size 12 and initialize the the values of that array. The program should then sum the values stored in that array and print the sum.

    solution

  6. Write a program that declares an integer array of size 50 and set the array values equal to the even numbers between 2 and 100 in assending order. The program should also print the array values.

    solution

  7. What is printed by the following program?
    // Passing arrays and individual array elements to functions
    #include 
    #include 
    
    void modifyArray(int [], int); 
    void modifyElement(int);
    
    main()
    {
       const int arraySize = 5;
       int a[arraySize] = {0, 1, 2, 3, 4};
    
       cout << "Effects of passing entire array call-by-reference:" 
            << endl << endl 
            << "The values of the original array are:" << endl;
    
       for (int i = 0; i < arraySize; i++)
          cout << a[i];
       cout << endl;
    
       modifyArray(a, arraySize);  // array a passed call-by-reference
       cout << "The values of the modified array are:" << endl;
    
       for (i = 0; i < arraySize; i++)
          cout << a[i];
    
       cout << endl << endl << endl
            << "Effects of passing array element call-by-value:" 
            << endl << endl << "The value of a[3] is " 
            << a[3] << endl;
    
       modifyElement(a[3]);
       cout << "The value of a[3] is " << a[3] << endl;
       return 0;
    }
    
    void modifyArray(int b[], int sizeOfArray)
    {
       for (int j = 0; j < sizeOfArray; j++)
          b[j] *= 2;
    }
    
    void modifyElement(int e)
    {
       cout << "Value in modifyElement is " 
            << (e *= 2) << endl;
    }
    

    solution