Test I, CSCI 230, Spring 2020


  1. Algorithm
    1. [3 points] Give the definition for an algorithm
    2. [2 points] Your friend proposes the following to sort an array. Is it an algorithm? Why or why not?
      LuckySort   
      Input: an array ary and the size of the array  size
         while ary is not in order    // assume you have a function to test this 
            let i be a random integer between 0 and size-1
            let j be a random integer between 0 and size-1
            swap(ary[i],ary[j])
      

      An algorithm is set of instructions that solves a problem in a finite amount of time.

      Please note, there are three important pieces.

      1. Instructions
      2. Solves the problem
      3. Finite time.
      We tend to not want code in our design algorithms, but code is definitely a set of instructions.

      To tell if the following is an algorithm, we need to test against those three items.

      • Are there instructions: yes.
      • Do they solve the problem in finite time: They are not guaranteed to do so. The random element means that the algorithm might run forever.
      Therefore, the given solution is not an algorithm.

  2. Arrays
    1. [2 points] Give the syntax for declaring an array. (Do not give code)
    2. [2 points] Give example code which declares an array.
    3. [2 points] Name two characteristics of an array.
    4. [2 points] List two different types of operations that can be performed on integers but not arrays.
    5. [2 points] List two different types of operations that do not behave as expected when performed on arrays.

    1. type identifier[const integer expression]

      const integer expression is very important in my opinion

    2.        const size_t ARY_SIZE = 20;
             string names[ARY_SIZE];
      
              or
             string names[20];
      
    3. Contiguous, homogeneous, constant access time.
    4. Mathematics, input, pass by value, return type of a function, assignment.

      Note, these are things you can not do to an array

      Also note, addition, subtraction, mod, division, and multiplication are the same type of operation, mathematics

    5. Comparison, output

      Wile you can do these operations on an array, they do not work as anticipated

      Note the difference in the two questions. One the compiler will catch for you, the other the compiler will not catch. You need to know the things you can do but will behave in an unexpected manner.

  3. Passing arrays to functions
    1. [2 points] Why are arrays passed by reference by default?
    2. [2 points] What alternative method is there to passing an array by reference? (the next two questions refer to this method)
    3. [3 points] Give the function prototype for a Find function which is passed an array with the method described in the previous question.
    4. [3 points] Describe what happens if an array, passed using the previous method, is modified in a function.

    1. Arrays are passed by reference because pass by value would require a copy and that could be very expensive.
    2. Arrays can be passed by constant reference using the keyword const before the parameter declaration.
    3. bool Find(const string names[], size_t size, string key);
    4. If an array passed as a constant reference parameter is changed in the function, the compiler will issue an error and the compilation process will fail.

  4. Modular Compilation Usage
    1. [3 points] Give three benefits that can be derived from placing code in different files.
    2. [3 points] Given an example of a guard which should be placed in a header file?
    3. [2 points] Why should a guard be placed in a header file?
    4. [2 points] What type of code should be placed in a header file?

      • Code Reuse
      • Multiple coders can work on the same program
      • Easier to create a test driver.
    1. #ifndef GUARD_FOR_DOT_H_FILE
      #deinfe GUARD_FOR_DOT_H_FILE
      
      // code here
      
      #endif
      
    2. The guard prevents redeclaration of the items defined if multiple files in the same compilation unit include the header file.
    3. Only definitions such as function prototypes, type and other declarations. Executable code should not normally be placed in a header file.

    Over Please

  5. Modular Compilation Process
      A program is split between three files. A main routine in main.C , a header file utils.h, and an implantation file utils.C.
    1. [7 points] Draw a diagram showing the process where these files will be compiled into a single executable. Place the files listed above, along with the following terms in your diagram where appropriate.
      • System Header Files
      • System Library Files
      • Intermediate Source Code
      • Object file
      • Executable
    2. [3 points] Label your diagram, in all applicable locations, with the following processes:
      • Preprocessor
      • Compiler Proper
      • Linker

  6. [5 points] Provide the full source code for a function which takes a string and returns a copy of that string with all non-alphabetic characters removed. (Do not provide code for the entire program, just a function).

    void StripString(string line) {
       size_t i;
       string rv;
    
       for(i=0;i<line.size();i++) {
          if (isalpha(line[i])) {
             rv += line[i];
          }
       }
       return line;
    }