CISC4080, Computer Algorithms,
Practice Lab on Vector

     

Home

Schedule

Assignments

Grading

 

Goal:

  1. Familiarize with C++ STL class vector

Details:

Here is a step-by-step guide to finish this lab:

  1. Implement the following function following the pseudocode:
          /**************************************************************
           If you use vector
           ***************************************************************/
         /* Generate an vector and fill it with random integers between 1 and 10,000
    	   return the address of the array
    	   @param size: the size of the array to be generated
    	   @return the vector */
    	vector<int> GenerateRandomVector(int size)
    	{
    		vector<int> v; //declare a vector of int, initially empty
    
    		for i=1...size
    		     num = generate a random number
    		     v.push_back (num); //add the number into the end 
    		     	// of vector 
    
    		return v; 
    	}
    
        
  2. Practicing iterating through a vector using index operator
    • Why pass the vector by constant reference?
    	/* display the content of the vector */
    	void PrintVector (const vector<int> & intList)
    	{
    	       int size=intList.size(); //how many items are in the vector
    
    	       for i=0...size-1
    	           display intList[i];
    	}
    
    	
  3. C++ STL vector template class has implemented constructor, desctructor, assignment operator for you, so to assign one vector to another:
    		vector<int> randomvector= GenerateRandomVector(10 );
    		vector<int> copy=randomvector;
    		
  4. Implement and testing bubble sort function with the following prototype.
    • How do you make this a template function (so that the same function can sort a vector of doubles, chars, strings...)?
    • Why passes the vector by reference? (i.e., what happens if you pass the vector by value?)
         ****************************************************/
        	/* sort the vector of int in ascending order using bubble sort
    	@param intList: vector of ints
    	@postcondition: the elements in the vector is arranged in ascending order
    	*/
    	void BubbleSort (vector<int> &  intList)
    	{
    
    	}
    
        

What to submit Nothing!