CPSC 241-002 Computer Science IV
(Data Structures and Algorithms)

Fall 1998
TTh 12:30--1:45 Daniel 408
Assignment 2
<http://andrewd.ces.clemson.edu/courses/cpsc241/fall98/hw/asg2.html>

Objectives:
To learn class structure, access to (usage of) member functions.

Due date:
09/10/98

What to hand in:
``Professional-quality'' writeup, including:
  1. Cover page containing:
    Course Id--Section No.
    Name:
    SS No:
    Assignment No.
  2. Assignment description
  3. Solution description (e.g., program design, description of algorithm, etc., however appropriate).
  4. Lessons learned, identified interesting features of your program
  5. Appendix A: Source code listing
  6. Appendix B: Sample input (if any)
  7. Appendix C: Sample output
The entire document should be a clearly written account of what you were to accomplish, what you in fact did accomplish, and what you learned (how you accomplished it). The code and sample runs, listed in the appendices, should be clearly documented (e.g., document the source code, generate informative output).

The general presentation of your document counts! (This includes qualities such as neatness, formatting, and legibility.)

Description:
  1. Using the provided "skeleton file" (oarrhw.cpp), provide the member function definitions for members
    	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 randfill();         // fill up member arr with random ints
    		
    Use the Unix-supplied routine rand() to generate (pseudo-) random numbers when filling the array in member function randfill(). (Note: there is no need to re-seed the random number generator, that's already been done for you.)

  2. Your code should be such that the following main() routine works unaltered:
    int main()
    {
            int             seeknum;
            IntArray        Arr(10);
    
      // using "static" object
      Arr.randseed();
      Arr.randfill();
      cout << "printing Arr\n" << Arr << "\n";
    
      cout << "min: " << Arr.min() << endl;
      cout << "max: " << Arr.max() << endl;
      cout << endl;
      cout << "Enter number to find: ";
      cin >> seeknum;
      cout << seeknum << " is at position " << Arr.find(seeknum) << endl;
    }
    		
  3. Sample output:
    seed = 809191
    printing Arr
    [0] = 12802
    [1] = 5781
    [2] = 10707
    [3] = 20155
    [4] = 32739
    [5] = 19420
    [6] = 1169
    [7] = 6289
    [8] = 17350
    [9] = 4702
    
    min: 1169
    max: 32739
    
    Enter number to find: 5781
    5781 is at position 1
    		
  4. (Bonus)
    1. Re-write main() so that a "dynamic" object is used, i.e., where the size of the object's array is determined by the user. The top few lines of main() should now look like this:
      int main()
      {
              int             num = 0;
              int             seeknum;
              IntArray        *Arrp;
      
        // using "dynamic" object
        cout << "Enter list size: ";
        cin >> num;
        .
        .
        .
      		
      Note the difference in the use of object, Arr, above, and the pointer to object, *Arrp, here. Hint: How is Arrp allocated and initialized?

    2. Overload the ostream& operator<< so that it works for the pointer to object, Arrp, as well as for the object itself, Arr, avove. (The code for the latter is already provided.)

  5. (Another Bonus)
    1. Implement the binary + operator.
    2. Your + operator should be implemented so that the following code works:
        Arr3 = Arr1 + Arr2;
        cout << "printing Arr3\n" << Arr3 << endl;
      		
      Hint: What should happen when the objects' array sizes differ?