473,413 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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!).


Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef VECTOR_H_
  3. #define  VECTOR_H_
  4.  
  5. #include <cstring>
  6.  
  7. namespace somenamespace{
  8.  
  9. template <typename ELEMENT_TYPE> class vector
  10. {
  11. private:
  12.     ELEMENT_TYPE* data;
  13.     size_t num_items;
  14.     size_t current_capacity;
  15.     const static size_t INITIAL_CAPACITY = 10;
  16.  
  17. public: 
  18.     vector<ELEMENT_TYPE>():
  19.         current_capacity(INITIAL_CAPACITY), num_items(0), data(new ELEMENT_TYPE[INITIAL_CAPACITY]){}
  20.  
  21.         //*************************************************************************
  22.         // Fill this function so that it returns the element 
  23.         // currently stored at index passed in as parameter.
  24.         //*************************************************************************
  25.  
  26.         ELEMENT_TYPE& get(size_t index)
  27.         {
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.         }
  37. };
  38. };
  39. endif /*VECTOR_H_*/
Dec 6 '08 #1
5 1154
Ganon11
3,652 Expert 2GB
And what did you write for your answer?
Dec 6 '08 #2
Banfa
9,065 Expert Mod 8TB
And why did they say it was wrong?
Dec 6 '08 #3
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.
Dec 7 '08 #4
Banfa
9,065 Expert Mod 8TB
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.
Dec 7 '08 #5
Ganon11
3,652 Expert 2GB
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.
Dec 8 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Maria | last post by:
Hi Everyone This is a data base question so don't switch off thinking it is a Data Protection Act Question. I'm Looking for some general information regarding the UK Law on rights to access...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
6
by: James | last post by:
I am using vb.net and need to keep in memory a large data structure, so I am looking for the best option. And after several test I am pretty confused. So I will be grateful if anyone can help me. ...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
4
by: Alexander Adam | last post by:
Hello folks, I got a few question on some basic data structure stuff. Sorry if those questions might sound too easy or such but after googling a lot I didn't find a real answer to all those...
8
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from...
20
by: =?Utf-8?B?ZW1pdG9qbGV5ZXM=?= | last post by:
Hi everyone: i read from the documentation of a dll that there is a struct that uses char for storing an IP address. How can it be? and how come i can get to representate the same value in a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.