Test II, CSCI 230, Spring 2016


  1. Abstract Data Type
    1. [2 points] Define Abstract Data Type An ADT is a data type whose properties (domain and operations) are specified independantly of any implementation.
    2. [3 points] Is char an abstract data type? Justify your answer. Yes, a character is an ADT. The domain are the values listed in the ASCII chart (but not necessairly the ascii values), and operations include comparison, increment, input and output. You have just defined an ADT, use this to answer the question.

      While we might suspect that a character is just a byte, containing the ascii value, we do not know that this is really the case. Nothing in the definition says that this is how it is done.

  2. Arrays
    1. [2 points] What is an array? A homogenious collection of data accessed by an index.
    2. [2 points] Give the syntax to declare an array. State any restrictions. datatype identifier[constant integer expression];

      Note, the size (constant integer expression) must be known at compile time.

      The size should be non-negative.

    3. [1 point] Give the code to declare an array of 25 integers.
       const int SIZE=25;
      ...
       int myArray[SIZE];
       
    4. [2 points] Give a code fragment which will initialize all elements of this array to be 99.
      int i;
      
      for(i=0;i<SIZE;i++) {
          myArray[i] = 99;
      }
      
    5. [5 points] Give a code segment that finds the maximum and minimum values in an array of ARY_SIZE integers.
      int min,
          max;
      int  ary[ARRAY_SIZE];
      
      ...
      
      min = ary[0];
      max = ary[0];
      
      for(i=1;i<ARRAY_SIZE; i++) {
          if (ary[i] > max) {
              max = ary[i]
          }
          if (min > ary[i]) {
              min = ary[i];
          }
      }
      
      It is not a good thing to set the value of max or min....
             max = INT_MAX ;
             
      would be a problem if you change the type. This solution is much better. In addition, you may look for max and min values where the absolute bounds might not be known. Think of strings, the min is (perhaps) easy, but the max is (for all intense and purposes) undefined.

      You can not make any assumptions about the data in the array. max = 0; assumes that the array only contains positive numbers for example.

  3. Array Index
    1. [2 points] What is an out of bounds array index? When an array index value is less than 0 or greater than or equal to the size of the array.
    2. [2 points] What is the result of an out of f bounds array index error? Incorrect data is accessed or data is stored at an incorrect location, this can cause the program to crash. Your program does not allways crash, it does not even always produce incorrect output.
    3. [2 points] What entity is responsible for assuring that out of bounds array index errors do not occur? It is the responsiblity of the programmer.
  4. Arrays as parameters
    1. [2 points] Describe how arrays are passed as parameters in C/C++. Arrays are always passed by reference, never by value.
    2. [2 points] Why are arrays passed in this manner? To pass by value, the compiler needs to copy all of the data, since arrays can become quite large this could be computationally expensive.
  5. Classes
    1. [4 points] Describe two common programming problems addressed by classes. Name/describe the mechanism that classes in C++ use to overcome these problems. Problem : Uninitalized variables. Classes overcome this problem by providing constructors which automatically initialize an instance of the class when it is created.

      Problem: No control over variables contents. Classes overcome this problem by "hiding" data or limiting the ability to manipulate that data through the class interface. Clients may not change the data directly, but must call a function which can validate any data change.

      Problem: Code and data not linked. Classes overcome this problem by encapsulating the member functions within the class.
    2. [2 points] What data is automatically available to a class member function? All member data of a class is available in the member functions. It does not need to be passed as a parameter. ALL DATA, not just data in the public or data in the private area.
    3. [2 points] How can client code change private data within a class? The only way client code can change private data is through the class interface.
  6. Class Design
    1. [2 points] When would a programmer use a class instead of a struct? When would a struct be appropriate? It is appropriate to use a class when an ADT is being constructed, ie there is a domain, over which data integrity must be maintained and there are a set of operations which manipulate that data.

      A struct is appropriate when there is only a collection of data, no constraints on the data values and no operations.

    2. [3 points] How does a programmer decide which member functions to implement for a class? The data must provide member functions to perform all operations specified in the ADT. In general, the operations must provide all required data manipulation and access.
  7. Class Implementation (Please read the entire problem before beginning)

    For this problem, you will implement a class which contains the geometric object square. A square is a polygon with four sides of equal length and all angles are 90 degrees. The length of a side must be at least 0. The program to use your class will need to compute the perimeter (P) and area (A) of the square. If l is the length of a side, P = 4l and A = l2

    1. [2 points] Define the Square ADT.
      Domain: 
          The length of the sides of the square, which must be non-negative.
      
      Operations:
          Constructor: set length to be 0
          Set the length of the sides to some value
          Return the Perimeter of the square
          Return the Area of the square
          Return the length of a side.
      
    2. [3 points] Give the code to define the SquareT class (as in a header file).
      class SquareT {
         public:
              SquareT();
      	void SetSide(int i);
      	int Perimiter();
      	int Area();
      	int Length();
         private:
              int length;
      }
      
      I would not store area and perimiter. In general storing a computed field, unless the computation of that field is extremenly time consuming is a bad idea. You will have to assure that you update the computation any time a value that computation depends on is updated.

      I would not perform I/O in any of the functions except possibly a print function. You should Prvide functions that return the values so the client can print them out in any manner they desire.

      I would provide a function that allows the client to set the length, but it must validate the length before it does so.

      Only the functions to set length should have a parameter. Everything else should just use the stored length.

    3. [3 points] Give the code to implement the SquareT class (as in a source code file)
      SquareT::SquareT(){
          length = 0;
      }
      
      void SquareT::SetSide(int i){
          if (i > 0) {
             length = i;
          }
          return;
      }
      
      int SquareT::Perimiter(){
           return 4*length;
      }
      
      int SquareT::Area(){
           return length* length;
      }
      
      int SquareT::Lenght(){
          return length;
      }
      
    4. [2 points] Give code to declare an instance of the class SquareT, assign a value to the side, and print out the area and parimiter. This code should not perform computations directly, that should be done by the SquareT class.
      
          SquareT s;
          s.SetSide(4);
      
          cout << "A square with length " << s.Length()
               << "has area " << s.Area() 
      	 << " and perimeter " << s.Perimeter() << endl;