473,385 Members | 1,645 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,385 software developers and data experts.

Cyclic array problem

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
Nov 13 '05 #1
8 4699
>

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
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


Nov 13 '05 #2
nkw wrote:
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.
--
Tim Prince
Nov 13 '05 #3
nkw <nk***@yahoo.com.au> wrote:
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.

Nov 13 '05 #4
Kevin Easton wrote:
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).

--
Tom Zych
This is a fake email address to thwart spammers.
Real address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #5
Kevin Easton wrote:
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).

--
Tom Zych
This is a fake email address to thwart spammers.
Real address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #6
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
Tom Zych <tz******@pobox.com> wrote in message news:<3F***************@pobox.com>...
Kevin Easton wrote:
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).

Nov 13 '05 #7
Tom Zych <tz******@pobox.com> wrote:
Kevin Easton wrote:
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).


Ahh. Yeah. That. :)

- Kevin.

Nov 13 '05 #8
nkw <nk***@yahoo.com.au> wrote:
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.

Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thomas Mailund | last post by:
Hi group. I have a problem with some C extensions I am working with and hope that some of you can help. Basically, I am wrapping a a tree structure from C where I have python methods for...
7
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in...
3
by: Dennis Lerche | last post by:
Hi I have a problem regarding cyclic dependency, yeahh I know bad design. But right at this moment I can't see how it should be redesigned to avoid this. The problem is that I just can't get it...
5
by: free2cric | last post by:
Hi, how to detect head and tail in cyclic doubly link list ? Thanks, Cric
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
10
by: toton | last post by:
Hi I have a class called Session, which stores a vector of Page, like vector<PageAlso each Page need's to know the Session to which it is attached. Thus I have, (the classes has many other...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
1
by: pallav | last post by:
I have to header files, circuit.h and latch.h that reference each other and are causing a cyclic dependency. latch.h file #include "circuit.h" typedef boost::shared_ptr<struct LatchLatchPtr;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.