data structure question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drjay1627
    New Member
    • Nov 2008
    • 19

    data structure question

    Hello,

    Can someone help me with the answer for this question. This is an exam I did a couple of week back and I don't understand why I got it wrong. AND I don't understand the answer the instructor gave me either.

    QUESTION

    Suppose the following partial definition for a vector class. Fill the method indicated below so that it functions as described in the comments. You should write your answer in C++ or as close to C++ as you can (minor syntactic errors will be ignored!).


    Code:
    #ifndef VECTOR_H_
    #define  VECTOR_H_
    
    #include <cstring>
    
    namespace somenamespace{
    
    template <typename ELEMENT_TYPE> class vector
    {
    private:
    	ELEMENT_TYPE* data;
    	size_t num_items;
    	size_t current_capacity;
    	const static size_t INITIAL_CAPACITY = 10;
    	
    public: 
    	vector<ELEMENT_TYPE>():
    		current_capacity(INITIAL_CAPACITY), num_items(0), data(new ELEMENT_TYPE[INITIAL_CAPACITY]){}
    		
    		//*************************************************************************
    		// Fill this function so that it returns the element 
    		// currently stored at index passed in as parameter.
    		//*************************************************************************
    		
    		ELEMENT_TYPE& get(size_t index)
    		{
    		
    		
    		
    		
    		
    		
    		
    		
    		}
    };
    };
    endif /*VECTOR_H_*/
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    And what did you write for your answer?

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      And why did they say it was wrong?

      Comment

      • drjay1627
        New Member
        • Nov 2008
        • 19

        #4
        Well I didn't write code. I hate to write code on paper.

        I wrote what need to be done.

        //Iterate through the vector
        //if( iter == index)
        // return element_type


        something similar to this.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          If you have been given the index why would you need to iterate through the vector? You have made an operation which should execute in O(0) (constant) time execute in O(n) (linear) time.

          Iterators are not the only way to access a vector I suggest you look up vector in you text book (or online, we have a sticky thread with useful links at the top of this forum) and see if you can find a better solution.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            In addition, the shell of code you were given does not define an iterator. You would have to write your own iterator class to use an iterator here, which I assume is not what was expected of you.

            As Banfa suggested, there is a very simple way to access the elements here.

            Comment

            Working...