Lab 1: Templated Array

Objectives

To learn templated class structure, access to (usage of) member functions

Assignment

  1. Using the array_t class presented in class, make the class templated to allow instantiaion of array_t<int> and array_t<float> objects
  2. Provide the member functions
    int min() const;         // return min value in list
    int max() const;         // return max value in list
    int find(int val) const; // return index of argument val
    void randseed();         // seed random number generator
    void randfill();         // fill up member arr with random ints
    	
    Use the C stdlib rand() routine to generate (pseudo-) random numbers when filling the array in member function randfill(), e.g., sample usage:
    #include <cstdlib>
    
    srand((unsigned int)1337);  // seed generator, do this only once!
    	
    // for each new value, get pseudo-random 0 < number < 1
    for(int i=0; i<arr.size(); i++)
      arr[i] = (float)rand()/(float)RAND_MAX;
    
    	

  3. Your code should be such that the following main() routine works unaltered:
    int main()
    {
            int             seeknum;
            array_t<int>    iarr(10);
    
      iarr.randseed();
      iarr.randfill();
      std::cout << "iarr:\n" << iarr << std::endl;
    
      std::cout << "min: " << iarr.min() << std::endl;
      std::cout << "max: " << iarr.max() << std::endl;
      std::cout << std::endl;
      std::cout << "Enter number to find: ";
      std::cin >> seeknum;
      std::cout << seeknum << " is at position " << iarr.find(seeknum) << std::endl;
    }
    	
  4. Sample output:
    seed = 1337
    iarr:
    [0] = 1
    [1] = 86
    [2] = 24
    [3] = 21
    [4] = 30
    [5] = 74
    [6] = 89
    [7] = 64
    [8] = 21
    [9] = 68
    
    min: 1
    max: 89
    
    Enter number to find: 64
    64 is at position 7
    	

Turn in

Turn in all of your code, in one tar.gz archive of your lab02/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Lab description
    4. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    5. Lessons learned, identified interesting features of your program
    6. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .cpp source)
  4. object code (do a make clean before tar)

How to hand in

See sendlab notes