Notes Test II, CSCI 230, Fall 2020


  1. Arrays
    1. [3 points] What are arrays? Provide a high level definition.
      Arrays are a homogeneous collection of contiguous data accessed by an index.

      A structured collection of components, all of the same type, that is given a single name. Each component is accessed by an index that indicates the component's position within the collection.

    2. [2 points] State the syntax (not an example) for declaring an array in c++.
      type identifier[const int expression]; 
    3. [4 points] State two conditions which suggest to a programmer that an array is needed.
      The data is homogeneous (all the same type) and is all related.

      The data needs to be stored because it will be used multiple times.

      Stating that you need to search the data is incorrect, if you only need to search the data one time, you don't need an array. On top of that, this is way to specific.
    4. [2 points] What is an array index out of bounds error?
      An array index out of bounds error occurs when an index value of less than zero or greater than or equal the the number of elements in the array is used.
    5. [6 points] What happens when an array index out of bounds error occurs? List all possible results.
      Memory outside of the array is accessed or changed.

      The programmer may observe no ill effects. Ie the values retrieved or the memory overwritten does not cause an error in the computation.

      The programmer may observe "strange" results in computation based on the data incorrectly retrieved or written to.

      The program may crash due to an attempt to access memory outside of that available to the program.

    6. [5 points each] Describe any errors in the following code segments
      1. size_t size;
        cout << "Enter the array size ";
        cin >> size;
        cout << endl;
        
        int ary[size]; 
        This code declares an array based on a non-constant size. This is not permitted by the c++ standard.
      2. const size_t MAX_SIZE =10;
        
        int main() {
            int ary2[MAX_SIZE]{0};
            size_t i;
        
            for(i =0; i <= MAX_SIZE; i++) {
               cout << ary2[i] << " ";
            }
            cout << endl; 
        This code has an array out of bounds error accessing one past the declared size of the array.
  2. Sorting and Searching
    1. [3 points] Give a high level algorithm for insertion sort (explain what the algorithm does, do NOT supply code or near code pseud-code.)
      For each position in the array starting at the first
         Move the smallest number in the remaining array to this position. 
      I intended to ask for selection sort here. I accepted an algorithm for either insertion sort or selection sort.
    2. [5 points] Write a function called CheckArray that takes an array of integers and the size of the array. This function should return true if the array is in increasing order and false otherwise.
      bool CheckArray(const int array[], size_t size) { 
      bool CheckArray(const int array[], size_t size) { 
          size_t i=0;
          bool good = true;
      
          while (good and i < size-1) {
             if (array[i] > array[i+1]) {
                good = false;
             }
             i++;
          }
      
          return good;
      }
      
  3. Algorithm Performance and Complexity
    1. [2 points] Name an algorithm that is $O(n)$ and an algorithm that is $O(n^2)$.
      sequential search is $O(n)$

      Selection sort is $O(n^2)$

    2. [4 points] In terms of data, what does it mean for an algorithm to be $O(n)$ and $O(n^2)$
      An $O(n)$ algorithm deals with each piece of data once.

      An $O(n^2)$ algorithm has each piece of data interact with every other piece of data.

    3. [4 points] Which algorithm listed in part A does this code behave most like computationally?
      • int Fibonacci(int limit){
            int old = 1;
            int current = 1;
            int sum = 1;
            int i;
        
            for(i = 2; i <= limit; i++) {
                sum = old+current;
                old = current;
                current = sum;
            }
        
            return sum; 
        }
        This algorithm has a single loop, or it has complexity of $O(n)$, therefore it behaves most like sequential search.
  4. Compiling
    1. [2 points] Name two benefits of breaking a program into multiple files.
      Multiple programmers can work on a project much more easily.

      This can be used to support information hiding.

      It makes it possible to reuse code more easily.

    2. [1 point each] Describe the purpose and function of each of the following in the compilation process.
      • C++ preprocessor
      • C++ compiler proper
      • C++ linker.
      The preprocessor integrates code from header files into the source code preparing it for compilation.

      The compiler proper translates high level c++ code into machine language. This produces object files.

      The linker combines object files and system library files into an executable.


Submission Instructions