Hey all,

I'm trying to use a 3 element array and keep it sorted. So when i get a new value that should be put at the start of the array, I shift values down in the array like this:

array[2] = array[1];
array[3] = array[2];
array[1] = result;

However, I've realized this does not work because of the way arrays are addressed. Is there anyway to do this via unary operators?

Thanks,
barefoot

Recommended Answers

All 5 Replies

>However, I've realized this does not work because of the way arrays are addressed.
Nothing to do with it.
Your error is in the subscripts.

array[2] = array[1];
array[1] = array[0];
array[0] = result;

>However, I've realized this does not work because of the way arrays are addressed.
Nothing to do with it.
Your error is in the subscripts.

array[2] = array[1];
array[1] = array[0];
array[0] = result;

oo wow thanks a bunch. I've been working w/ matlab for to long and it starts with 1 rather than 0 when addressing an array.

Thanks again!

Has nothing to do with whether you start with 0 or 1 in array addressing.
In Pascal you could start from 100 if you wanted to do that.

Look at your code:

array[2] = array[1];
array[3] = array[2];
array[1] = result;

Now look at this code:

array[3] = array[2];
array[2] = array[1];
array[1] = result;

Notice the difference?

>Notice the difference?
Error in coping and pasting.
First line and second line are just swapped.

That is just what Barefootsanders did in the first place ;o)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.