Computer Science Canada

pushing data onto an array

Author:  Homer_simpson [ Sun Jul 03, 2011 6:30 pm ]
Post subject:  pushing data onto an array

It has been a while since I have done any programming in c++ and I've become a bit rusty. =S
here's the explanation of the problem:
I have an array of 800 real numbers. I read a new data point to be added to the array from the serial port and i need to push all the datapoints on the array back by one and consequently get rid of the last datapoint to make room for the new one. now I realize that i can push every single data point down using a loop and add the new datapoint but I don't think that this is the best way of achieving the task.
I hope I explained it properly.
Any suggestions to point me in the right direction are appreciated.thanks

Author:  md [ Sun Jul 03, 2011 6:36 pm ]
Post subject:  Re: pushing data onto an array

Make an array of N elements (size of your buffer), and possibly zero the elements (not strictly speaking required, but helps with debugging).

Create two integers, offset and length, set offset to -1 and length to 1.

For each element that you insert:
offset = (offset + 1) % N
length = length > n ? length + 1 : length
data[offset] = element

Reading back is as easy as starting at offset and reading length values, remembering to index modulo N.

Author:  Homer_simpson [ Sun Jul 03, 2011 7:40 pm ]
Post subject:  Re: pushing data onto an array

thanks for the quick reply.
I tried this method of keeping track of positions and it works fine, but after some google searching i found the vector class which simplifies my task so I will use it instead.

Author:  unoho [ Sun Jul 03, 2011 9:02 pm ]
Post subject:  RE:pushing data onto an array

ya u dont wanna use arrays if u r dealing w/ 800 elements. vectors r def better.

also u cud always make ur own class and use pointers but vectors wud do just fine for a basic task.

Author:  apython1992 [ Sun Jul 03, 2011 9:26 pm ]
Post subject:  Re: pushing data onto an array

Homer_simpson @ Sun Jul 03, 2011 6:30 pm wrote:
It has been a while since I have done any programming in c++ and I've become a bit rusty. =S
here's the explanation of the problem:
I have an array of 800 real numbers. I read a new data point to be added to the array from the serial port and i need to push all the datapoints on the array back by one and consequently get rid of the last datapoint to make room for the new one. now I realize that i can push every single data point down using a loop and add the new datapoint but I don't think that this is the best way of achieving the task.
I hope I explained it properly.
Any suggestions to point me in the right direction are appreciated.thanks

By your description I can't quite tell whether you're looking for a stack-type structure or queue, but to me it sounds like you're looking for a queue (think of a conveyor belt; first in = first out). If so, take a look at the queue STL container. Otherwise, a vector makes for a nice intuitive stack structure with constant-time element access.

Author:  apython1992 [ Sun Jul 03, 2011 9:34 pm ]
Post subject:  Re: RE:pushing data onto an array

unoho @ Sun Jul 03, 2011 9:02 pm wrote:
ya u dont wanna use arrays if u r dealing w/ 800 elements. vectors r def better.

also u cud always make ur own class and use pointers but vectors wud do just fine for a basic task.

Vectors aren't better than arrays because they are more efficient at holding larger numbers of elements (they aren't). They are better because they "know their own size", which makes it trivial to resize your container.


: