473,405 Members | 2,210 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,405 software developers and data experts.

Periodic Arrays

Hi there, I'm relatively new to c++ and I'm trying to write a fairly simple simulation.

The simulation involves updating values in a grid based on the values of neighbouring nodes, and I'm working with polar coordinates.

Say I have an array A[200] and I iterate using an update equation of say;

A[i] = (A[i+1] + A[i-1])/2;

This is fine except at the boundaries, where asking for A[i-1] at i=0 will give a segmentation fault. Due to the periodicity of angles A[i-1] should in fact be A[199] at this boundary.

Obviously I can overcome this with some simple conditionals, but I can't help but feel this must be a fairly common problem and since this is a simulation I want things as concise and quick as possible.

Is there any object already out there which works like an array but loops around at the ends? Or if not does anyone have any ideas of how to elegantly tackle the problem?

Thanks
Feb 12 '12 #1
2 3059
weaknessforcats
9,208 Expert Mod 8TB
It looks like you want a circular linked list. There is a linked list in the C++ Standard library but it is not circular. However, you could write your own functions to manage the standard list as though it was circular. For example, your advance function when it encounters the end f the list could continue on by positioning to te beginning of the list before returning.

A C++ developer would define a class with a standard library linked list as a member so that creating an object of this class would also create the list. Then all of the member functions would operate on this list to make it appear circular.

At least, the ls code is done and debugged leaving you to write only your handling functions.
Feb 12 '12 #2
Hi

Is it clear to you that each time you compute an element, the array is automatically updated, if you do the assigment at the same time ? So, if it is not what you wish, you may use a temporary array.


it is easy to compute A[i] = (A[i+1] + A[i-1])/2;

so have an array of size n, either defined statically or dynamicall.

to compute it do it like following

A[0] = A[ 1 ] + A[ n-1 ] ) / 2;
A[ n -1 ] = A[ 0 ] + A[ n - 2 ] ) / 2;

for(int i = 1 ; i < n - 1 ; i++){

A[i] = A[ i + 1 ] + A[ i - 1 ] ) / 2;
}

hope it helps
Feb 13 '12 #3

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

Similar topics

2
by: Hans Forbrich | last post by:
The following is a Request for Discussion, following up on some recent posts about distributing a periodic news group 'Charter and FAQ' post. While not formally following RFC/RFD rules, the intent...
0
by: Hans Forbrich | last post by:
Section 1. Ballot: ------------------- 1.YES NO: I agree that there should be a periodic post describing the newsgroup charter and providing a FAQ on newsgroup usage. 2.MONTHLY BI-WEEKLY...
0
by: Hans Forbrich | last post by:
Section 1. Ballot: ------------------- 1.YES NO: I agree that there should be a periodic post describing the newsgroup charter and providing a FAQ on newsgroup usage. 2.MONTHLY BI-WEEKLY...
20
by: Karl Smith | last post by:
I heard a rumour that Opera succeeded where none have before, and implemented the tables described in HTML4 and CSS2. So I thought I'd try it out with the well known Periodic Table. ...
8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
5
by: nishantxl | last post by:
Hi there, I am looking to design a project using C++ The main objective of the project is to display details of periodic table elements such as periodic element name, properties(such as atomic...
7
by: KailashDas | last post by:
Please can some 1 help me out in writing a program for outputting the periodic table of elements Kailash Das
2
archonmagnus
by: archonmagnus | last post by:
Hello all, I've been experimenting with developing an orbital analysis program. Being a visually oriented person, I'd like to translate my (x, y) coordinate pairs to an pixel image array so I can...
5
oranoos3000
by: oranoos3000 | last post by:
hi I want to repeat executing a seri of the code in page in interval periodic time without use from infinite loop and i prefer that page is not refreshed only a seri of the code is execute....
3
oranoos3000
by: oranoos3000 | last post by:
hi i want to get time of server with ajax,php,javascript i use under scritp and with method onload i get time on server and show on the page so i want in a period time the same function is run...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.