Test II, CSCI 230, Spring 2020


  1. ADT:

    An abstract data type (ADT) is defined as a datatype whose properties (domain and operations) are specified independently of any particular implementation. Given that a class is the way an ADT is implemented in c++,

    1. [6 points] List and describe mechanisms provided by the c++ class structure to implement an ADT. How do these mechanisms support development of an ADT?

      The question asked about the c++ class mechanisms and their relationship to an ADT. The answer should address this.

      An abstract data type has both domain(data) and operations (functions). This is supported through encapsulation or the bundling of class member data and member functions within an instance of a class.

      An ADT does not rely on any specific implementation which is supported by private members, both data and functions within a class. This prevents clients who don't know implementation details from incorrectly manipulating class data.

      While not specifically related to classes, the ability to place code in multiple files supports information hiding, or the independence of the implementation details from specification in an ADT.

    2. [6 points] Discuss the implications of this definition for the implementer of a class in c++.

      The clients of the class do not have access to the implementation of the class. This means the the class implementer must provide a complete interface that allows full manipulation of the class.

      In addition, the interface must follow to the published standards for that interface.

      The implementation must maintain error free values of the member data. (Or maintain a correct value for the state of the object).

      The creator must properly document and test all methods to assure that they perform as promised. The client is placing complete trust in the implementation of the class.

    3. [2 points] Discuss the implications of this definition for the client of a class in c++.

      The client must understand and correctly employ the class interface.

  2. [4 points] Why does class scope extend to all instances of a class? Give an example.

    Again, this question asked specifically about why class scope extends to all instances of the class. This should be addressed.

    Class scope extends to all instances of the class to allow the creation of functions that operate on two or more instances of the same class. Examples include comparison or combination operators.

    In the example below, the overloaded operator < takes a second instance of a class and compares it to the current instance. The current instance is permitted to access the private member data count as class scope extends to all instances of a class.

    class WordCountT {
        ... 
        bool operator <(const WordCountT & other) {
            return count < other.count;
        }
    
        private:
            int count;
            string data;
    };
    
  3. [6 points] Describe the basic differences between procedural programming and object oriented programming.

    This is covered chapter 4, page 163 in your book

    Procedural programming is based on function calls. The programmer tends to focus on the creation of an algorithm, or set of algorithms that each constitute a solution to a problem. Procedural programming is frequently a top down activity where the main algorithm and function is developed, then a set of supporting algorithms and functions and so on. The focus is on functions and how the data is transformed by each function.

    Object oriented programming is focused on creating a series of objects. Each object represents a entity or object used in building the program. These objects are combined to build larger and more complex objects until a solution is finally derived. This leads to the bottom-up development style.

    In general procedural programmers focus on functions and decomposing the program into smaller and smaller functions. Object oriented programmers tend to focus on the objects that make up the solution to a problem and combine those to form a final solution.

  4. In terms of a c++ class,
    1. [2 points] What is a constructor?

      A constructor is a member function of a class which is responsible for establishing the initial state of the class when it is created.

    2. [2 points] What are the responsibilities of a constructor?

      The constructor must initialize the data members (variables) to contain the proper default values.

    3. [2 points] When is a constructor invoked?

      The constructor is invoked when an instance of the class is declared.

  5. [5 points] You are implementing a design that calls for a class with no observers or transformers. The design specifies that all data should be public. Critique this design.

    With all data members in the public area, the class no longer provides any assurance that the data will be properly manipulated and remain error free. This is no longer a true ADT but more of an aggregation of data.

  6. Selection Sort
    1. [4 points] Describe in English the operation of Selection Sort.

      The current position will start at the first position in the array and move "up"one each iteration.

      For each iteration, smallest element in the remaining array will be located and will be moved to the current position.

    2. [2 points] Draw a diagram using ASCII art discussing the state of the array after a number of passes of the main loop of Selection Sort. This diagram should support the answer in part A.
      • you might, for example, use UUUUUUUUUUUU to represent an unsorted array.
      • Make sure you explain what your diagram(s) mean.

      Let U represent an unordered element and S represent an ordered element.
      When the sort begins, the array is
      
      UUUUUU.....UUUU
      
      After the first step the smallest element will be moved to the first position
      SUUUUU.....UUUU
      
      After step n, the n smallest elements will be moved to the front  of the array
      SS...SSU...UUU
      |--n--|
      
    3. [4 points] Discuss the performance of Selection Sort.

      In selection sort, every element is compared to every other element. Means that the performance is O(n2).

      The algorithm is slower than the sequential search algorithm, which is O(n). The algorihtm is not "horrible" or tremendously inefficient. There are many algorithms that are far worse. It is just the most expensive one you know right now.

  7. [5 points] A file contains a series of integers, one per line. Write a function which takes the file name and returns the number of integers contained in the file.
    // don't prompt for the file name or print out the results.
    // don't include the end of file bug.
    size_t CountValues(string fileName){
        ifstream inFile;
        size_t count = 0;
        int tmp;
    
        inFile.open(fileName0;
    
        inFile >> tmp;
        while(inFile) {
           count++;
           inFile >> tmp;
        }
        return count;
    } 

Submission Instructions