CSCI 1300
Lecture Notes


11/3/97

Administrative Stuff 



Administrative Stuff

  • Assignment #8 due Nov. 14
  • 2 quizzes left

  • 1 in about 2 weeks
  • 1 on the last day of classes
  • Quiz #3 results

  • Review: functions and parameters

  • Pass by reference

  • Pointers

  • Dynamic memory allocation and deallocation



    Quiz #3

  • Everyone got full credit for #2

  • The average, of people that took the exam, was 75.

  • Almost everyone should review files.


    1. (30 points) TRUE or FALSE

    1. FALSE - 2D arrays take up twice as much memory as 1D arrays.
      A 2D array 2darray[m][n] takes up n times as much memory as a 1D array 1darray[n].

    2. TRUE - C++ arrays start with the 0th element.

    3. FALSE - Class constructors can't take parameters.
      Constructors can take parameters

    4. TRUE - Overloading is defining multiple functions with the same name but different parameters.

    5. FALSE - After you create a file, you can't change the size.
      After you create a file, you can change the size by writing or appending to it.

    6. TRUE - If you open an fstream object for writing, you can't read from it.

    7. FALSE - In graphics mode, the screen is just a big array of pixels with (0,0) in the lower left.
      (0,0) is in the upper left

    8. FALSE - Linear search is faster and more efficient than Binary search.
      In general, Binary search is faster and more efficient.

    9. TRUE - Binary search only works on sorted lists.

    10. FALSE - Functions cannot call themselves.
      Functions can call themselves. This is recursion.

     

    2. (0 points) Given the class String, (partially) defined as follows:

      class String {
        private:
          char letters[80];
    
        public:
          char String::letter(int n);    // Returns the nth letter in the string
          void String::operator=(char S[]);
      };
    
    1. Write the String::operator=() function.
        void String::operator=(char S[])
        {
          int i;
      
          for(i = 0; (i < 79) && (S[i] != 0); i++)
            letter[i] = S[i];
      
          letter[i] = 0;
          
        }
      
    2. Show how you would use the = operator to initialize an object of type String.
        String Foo;
      
        Foo = "This is a test, this is only test";
      

     

    3. (30 points) Show how you would use an fstream object to open a file called "numbers", write the numbers 1 through 100 in it, each on a seperate line, then close the file.

      int i;
      fstream nfile;
    
      nfile.open("numbers", ios::out);
    
      for(i = 1; i <= 100; i++)
        nfile << i << endl;
    
      nfile.close();
    

     

    4. (10 points) Write a recursive function called count_ones() that takes an integer n as a parameter and returns the number of ones in the binary representation of n.
      int count_ones(int n)
      {
        if(n == 0)    // Anchor case: there are no 1's in 0
          return(0);
        else          // Recursive case: lowest bit + count_ones( the rest )
          return( (n & 1) + count_ones(n >> 1) );
      }
    
    OR
      int count_ones(int n)
      {
        if(n == 0)    // Anchor case: there are no 1's in 0
          return(0);
        else          // Recursive case: lowest bit + count_ones( the rest )
          return( (n % 2) + count_ones(n / 2) );
      }
    


    Review: functions and parameters

  • When an object is passed to a function as a parameter
  • A complete copy of the object is made
  • The copy is in the stack space of the function
  • A local name is assigned to the copy of the object
  • All changes affect only the local copy
  • There is no way to affect the value of the original object
  • Each function can return
  • Exactly one value
  • The value has the type of the function
  • Example:
      void main(void)
      {
        int A = 5;
        int B = 7;
        int sum;
    
        sum = add(A, B);
    
        cout << sum;
      }
    
      int add(int C, int D)
      {
        return(A + B);
      }
    
  • Picture: the stack

  • This is know as pass by value because we are passing the value of the object to the function, not the actual object.

  • Questions
  • What happens when we pass a large user-defined object to a function?
  • What if we want to change something in the original object?
  • What if we want to pass a one-of-a-kind object to the parameter? Example: an object that references some particular hardware, like the display card.
  • What if we want to return more than one value?

  • Reference parameters

    Reference parameters allow us to deal with the issues raised above

  • When an object is passed to a function as a reference parameter
  • No copy is made of the original object
  • A local name is assigned to the original object
  • All changes affect the original object
  • Example of pass-by-value:
      void increment(int Z)
      {
        Z++;
      }
    
      void main(void)
      {
        int A = 5;
    
        increment(A);
    
        cout << A << endl;
      }
    
    Example of pass-by-reference:
      void increment(int &Z)
      {
        Z++;
      }
    
      void main(void)
      {
        int A = 5;
    
        increment(A);
    
        cout << A << endl;
      }
    
  • What number gets printed out in the two cases?


    Another example: half-adder()


  • When do you use value parameters?
  • ...
  • When do you use reference parameters?
  • ...
  • Constant reference parameters