Cyclic array problem

N

nkw

Hi,

I'm new to C and I'm stuck in working out the logic code for a cyclic
array.

I have an array of 5 cells, starting at index cell 0 and last cell is
4.
ie
0 1 2 3 4 0 1 ..... and so on

My problem is at the boundaries,
When I'm on Cell 0, I want to check cell 2 and Cell 4 then do
something
When I'm on Cell 4, I want to check cell 3 and Cell 0 then do
something

I have psuedo code something like this
cell[5];
for (col=0, col<5; col++)
if cell[0]=='1' && if cell[2]=='1' && if cell[4]=='1'
then do something
if cell[4]=='1' && if cell[3]=='1' && if cell[0]=='1'
then do something

if cell[0]=='0' && if cell[2]=='0' && if cell[4]=='0'
then do something
if cell[4]=='0' && if cell[3]=='0' && if cell[0]=='0'
then do something
......

My problem:

a) Is it correct to use:
if (cell[0]=='1' && cell[col+1]=='1' && cell[ENDCELL]=='1')
if (cell[ENDCELL]=='1' && cell[ENDCELL-1]=='1' && cell[0]=='1')?

b) Is there am effiecient way to check for conditions at the end
points or do I have to state each end point case ? In this post, I'm
checking for 1 and 0 for simplicity.

Can anyone help out ?
Thanks in advance
Nat
 
B

Bigdakine

I'm new to C and I'm stuck in working out the logic code for a cyclic
array.

I have an array of 5 cells, starting at index cell 0 and last cell is
4.
ie
0 1 2 3 4 0 1 ..... and so on

My problem is at the boundaries,
When I'm on Cell 0, I want to check cell 2 and Cell 4 then do
something
When I'm on Cell 4, I want to check cell 3 and Cell 0 then do
something

I have psuedo code something like this
cell[5];
for (col=0, col<5; col++)
if cell[0]=='1' && if cell[2]=='1' && if cell[4]=='1'
then do something
if cell[4]=='1' && if cell[3]=='1' && if cell[0]=='1'
then do something

if cell[0]=='0' && if cell[2]=='0' && if cell[4]=='0'
then do something
if cell[4]=='0' && if cell[3]=='0' && if cell[0]=='0'
then do something
......

My problem:

a) Is it correct to use:
if (cell[0]=='1' && cell[col+1]=='1' && cell[ENDCELL]=='1')
if (cell[ENDCELL]=='1' && cell[ENDCELL-1]=='1' && cell[0]=='1')?

If that is used in the loop, col+1 would yield 5 and you'd be accessing memory
you didn't intend to access.


One sort of trick often used in mathematical methods when solving for values on
the boundaries is to create superdomain points.

So you can embed your 5 element array in a 7 element array.

Since you're using periodic boundary conditions..

cell[0 =cell[5];
cell[6]=cell[1];

Then you can loop:

for (col=1; col<num_cell+1; col++) {

....................

}

After each loop update your superdomain points..

cell[0]=cell[5];
cell[6]=cell[1]

Your actual cells are cells 1-5 and your superdomain cells are cells, 0, 6.

HTH

Stuart
 
T

Tim Prince

nkw said:
Hi,

I'm new to C and I'm stuck in working out the logic code for a cyclic
array.

I have an array of 5 cells, starting at index cell 0 and last cell is
4.
ie
0 1 2 3 4 0 1 ..... and so on

My problem is at the boundaries,
When I'm on Cell 0, I want to check cell 2 and Cell 4 then do
something
Did you mean the neighboring cells, 1 and 4?
When I'm on Cell 4, I want to check cell 3 and Cell 0 then do
something
You could use remaindering '%' to index to the neighboring cells, although
that could be inefficient when it doesn't translate to masking operations.
 
K

Kevin Easton

nkw said:
Hi,

I'm new to C and I'm stuck in working out the logic code for a cyclic
array.

I have an array of 5 cells, starting at index cell 0 and last cell is
4.
ie
0 1 2 3 4 0 1 ..... and so on

My problem is at the boundaries,
When I'm on Cell 0, I want to check cell 2 and Cell 4 then do
something
When I'm on Cell 4, I want to check cell 3 and Cell 0 then do
something

Perhaps this will help you. For cell i, 0 <= i <= 4, the cell to the
left of i has index (i + 6) % 5 and the cell to the right of i has index
(i + 1) % 5.

- Kevin.
 
T

Tom Zych

Kevin said:
Perhaps this will help you. For cell i, 0 <= i <= 4, the cell to the
left of i has index (i + 6) % 5 and the cell to the right of i has index
(i + 1) % 5.
ITYM:

left of i has index (i + 4) % 5...

Or, more generally, something like (i + SIZE - 1).
 
N

nkw

Thanks everyone for your help.
I'll give this logic approach a try.

But if I use the logic for index (i + 6) % 5, would I address '6' as
(Maxcell + 1) if all I know is Maxcell ?
Wouldn't I get some sort of segmentation error as I addressing
something outside the array range ?

Nat
 
K

Kevin Easton

nkw said:
Thanks everyone for your help.
I'll give this logic approach a try.

But if I use the logic for index (i + 6) % 5, would I address '6' as
(Maxcell + 1) if all I know is Maxcell ?
Wouldn't I get some sort of segmentation error as I addressing
something outside the array range ?

(Maxcell + 1) % 5 won't give you a value outside the range of the array.

- Kevin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top