473,779 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reversing the order of some loops.

I have a program that loops through and changes all the elements on an
array n times, so my code looks like this:

for (n=1; n < n_max; ++n)
for(i=imax; i >= 0; --i) {
for(j=0 ; j < jmax; ++j) {
/* main bulk of code goes here */
}
}

The problem is I want to reverse the order of the **two inner loops**,
so I have all 4 possible combinations of the loops incrementing and
decrement i and j. So on the first iteration of n (n=1), i and j will
both incremnt from 0 to their maximum values, but on another they will
decmement from their maximum values to 0. The second permetation would
be:

for(i=imax; i >= 0; --i) {
for(j=0 ; j < jmax; ++j) {
/* loads of code goes here */
}
}

where I have reversed the i loop, and this one where the j loop is
reversed. The other two combinations would be:

for(i=0; i< imax; ++i) {
for(j=jmax ; j >= 0; --j) {

And finally:

for(i=imax; i >= 0; --i) {
for(j=jmax ; j >= 0; --j) {

Can anyone find a ***neat*** way of doing this in a C function?
The only way I have found to do this is at the minute is rather ugly.
I have a C function (which could be main), with four #includes, and
four #defines, which includes a C source file. The exact ordering of
the loops in the included file depends on what is #defined in the main
file. In other words I have:
int main()
{
for(n=1; n < n_max; ++n) {

#define TO_BOTTOM_RIGHT
#include "update_voltage _array.c"
#undef TO_BOTTOM_RIGHT

#define TO_BOTTOM_LEFT
#include "update_voltage _array.c"
#undef TO_BOTTOM_LEFT

#define TO_TOP_RIGHT
#include "update_voltage _array.c"
#undef TO_TOP_RIGHT

#define TO_TOP_LEFT
#include "update_voltage _array.c"
#undef TO_TOP_LEFT
}
then in the file "update_voltage _array.c", I don't have a function,
but bits like this:

#ifdef TO_BOTTOM_RIGHT
for(i= 0; i <= imax; ++i){
for(j=0; j<=jmax; ++j) {
#endif

#ifdef TO_BOTTOM_LEFT
for(i= imax; i >=0 ; --i){
for(j=0; j<=jmax; ++j) {
#endif

#ifdef TO_TOP_RIGHT
for(i= 0; i <= imax; ++i){
for(j=height-1; j>=0; --j) {
#endif

#ifdef TO_TOP_LEFT
for(i= imax; i >=0; --i){
for(j=jmax; j>=0; --j) {
#endif
Can anyone think of a better way of achieving what I want to achieve?
Ideally a function which took a couple of arguments to determine how
the loops behave would be good, but I can't find a way of doing it.
Whilst its easy to pass the start and end points of the loops, I can
find no way of changing the "i < imax" to be an "i >= 0", so passing
those does not help.

Suggestions welcome.

Dr. David Kirkby.

My real email address can be found at:
http://homepage.ntlworld.com/drkirkby/home-email.jpg
Nov 13 '05 #1
4 2137
In article <c9************ *************@p osting.google.c om>,
Dr. David Kirkby <se************ *************** *******@hotmail .com> wrote:

Can anyone find a ***neat*** way of doing this in a C function?


for (n=1; n < n_max; n++)
for (k=0; k<4; k++)
for (i1=0; i1 < imax; i1++)
for (j1 = 0; j1 < jmax; j1++) {
i = n & 1 ? i1 : imax - i1;
j = n & 2 ? j1 : jmax - j1;
...
};

Or perhaps:

for (n=1; n < n_max; n++)
for (k=0; k<4; k++)
for (i = n&1 ? 0 : imax; n&1 ? i < imax : i >= 0 ; n&1 ? i++ : i--)
for (j = n&2 ? 0 : jmax; n&2 ? j < imax : j >= 0 ; n&2 ? j++ : j--) {
...
};

-- Brett
Nov 13 '05 #2

"Brett Frankenberger" <rb*@panix.co m> wrote in message
news:bn******** **@reader1.pani x.com...
In article <c9************ *************@p osting.google.c om>,
Dr. David Kirkby <se************ *************** *******@hotmail .com> wrote:

Can anyone find a ***neat*** way of doing this in a C function?


for (n=1; n < n_max; n++)
for (k=0; k<4; k++)
for (i1=0; i1 < imax; i1++)
for (j1 = 0; j1 < jmax; j1++) {
i = n & 1 ? i1 : imax - i1;
j = n & 2 ? j1 : jmax - j1;
...
};

Or perhaps:

for (n=1; n < n_max; n++)
for (k=0; k<4; k++)
for (i = n&1 ? 0 : imax; n&1 ? i < imax : i >= 0 ; n&1 ? i++ : i--)
for (j = n&2 ? 0 : jmax; n&2 ? j < imax : j >= 0 ; n&2 ? j++ :

j--) {

I think you have some n's where you should have k's, but otherwise...

I would probably use a switch with four sets of loops inside. That would
give the compiler a chance to properly optimize each loop. Otherwise, the
fisst one might optimize better than the second, though the second probably
satisfies the "neat" requirement better.

Just to complain again about the lack of power of the C preprocessor
relative to PL/I, the PL/I preprocessor could expand four loops at compile
time a little easier than C.

-- glen
Nov 13 '05 #3

"Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> wrote in message
news:6a1mb.6076 $ao4.13422@attb i_s51...

"Brett Frankenberger" <rb*@panix.co m> wrote in message
news:bn******** **@reader1.pani x.com...
In article <c9************ *************@p osting.google.c om>,
Dr. David Kirkby <se************ *************** *******@hotmail .com> wrote:

Can anyone find a ***neat*** way of doing this in a C function?
for (n=1; n < n_max; n++)
for (k=0; k<4; k++)
for (i1=0; i1 < imax; i1++)
for (j1 = 0; j1 < jmax; j1++) {
i = n & 1 ? i1 : imax - i1;
j = n & 2 ? j1 : jmax - j1;
...
};

Or perhaps:

for (n=1; n < n_max; n++)
for (k=0; k<4; k++)
for (i = n&1 ? 0 : imax; n&1 ? i < imax : i >= 0 ; n&1 ? i++ : i--) for (j = n&2 ? 0 : jmax; n&2 ? j < imax : j >= 0 ; n&2 ?

j++ : j--) {

I think you have some n's where you should have k's, but otherwise...
I would probably use a switch with four sets of loops inside. That would give the compiler a chance to properly optimize each loop. Otherwise, the fisst one might optimize better than the second, though the second probably satisfies the "neat" requirement better.

Just to complain again about the lack of power of the C preprocessor
relative to PL/I, the PL/I preprocessor could expand four loops at compile time a little easier than C.

-- glen


And Fortran will unroll and inline the subroutine code even better
with no preprocessor help..

This problem seems to invite "parallelizatio n" and the
Fortran syntax,

forall (i=1:imax,j=1:j max) array_stuff

is a invitation for the compiler to generate multi-processor code.

Lacking multi-processing capability, I would put below in my exec,
and call the array stuff with appropriate indices..

! ----------------------
do n = 1,n_max

icase = mod(n-1,4)+1 ! 4 sequential "n cases"

select case (icase)
case (1)
do i = 1,imax
do j = 1,jmax
call array_stuff(i,j )
end do
end do
case (2)
do i = 1,imax
do j = jmax,1,-1
call array_stuff(i,j )
end do
end do
case (3)
do i = imax,1,-1
do j = 1,jmax
call array_stuff(i,j )
end do
end do
case (4)
do i = imax,1,-1
do j = jmax,1,-1
call array_stuff(i,j )
end do
end do
end select
end do
Nov 13 '05 #4

"David Frank" <da********@hot mail.com> wrote in message
news:hk******** ***********@twi ster.tampabay.r r.com...

"Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> wrote in message
news:6a1mb.6076 $ao4.13422@attb i_s51...

"Brett Frankenberger" <rb*@panix.co m> wrote in message
news:bn******** **@reader1.pani x.com...
In article <c9************ *************@p osting.google.c om>,
Dr. David Kirkby <se************ *************** *******@hotmail .com> wrote: >
>Can anyone find a ***neat*** way of doing this in a C function?

for (n=1; n < n_max; n++)
for (k=0; k<4; k++)
for (i1=0; i1 < imax; i1++)
for (j1 = 0; j1 < jmax; j1++) {
i = n & 1 ? i1 : imax - i1;
j = n & 2 ? j1 : jmax - j1;
...
};

Or perhaps:

for (n=1; n < n_max; n++)
for (k=0; k<4; k++)
for (i = n&1 ? 0 : imax; n&1 ? i < imax : i >= 0 ; n&1 ? i++ : i--) for (j = n&2 ? 0 : jmax; n&2 ? j < imax : j >= 0 ; n&2 ?

j++ :
j--) {

I think you have some n's where you should have k's, but

otherwise...

I would probably use a switch with four sets of loops inside. That

would
give the compiler a chance to properly optimize each loop.

Otherwise, the
fisst one might optimize better than the second, though the second

probably
satisfies the "neat" requirement better.

Just to complain again about the lack of power of the C preprocessor
relative to PL/I, the PL/I preprocessor could expand four loops at

compile
time a little easier than C.

-- glen


And Fortran will unroll and inline the subroutine code even better
with no preprocessor help..

This problem seems to invite "parallelizatio n" and the
Fortran syntax,

forall (i=1:imax,j=1:j max) array_stuff

is a invitation for the compiler to generate multi-processor code.

Lacking multi-processing capability, I would put below in my exec,
and call the array stuff with appropriate indices..

! ----------------------
do n = 1,n_max

icase = mod(n-1,4)+1 ! 4 sequential "n cases"

select case (icase)
case (1)
do i = 1,imax
do j = 1,jmax
call array_stuff(i,j )
end do
end do
case (2)
do i = 1,imax
do j = jmax,1,-1
call array_stuff(i,j )
end do
end do
case (3)
do i = imax,1,-1
do j = 1,jmax
call array_stuff(i,j )
end do
end do
case (4)
do i = imax,1,-1
do j = jmax,1,-1
call array_stuff(i,j )
end do
end do
end select
end do


I don't think you are answering the right question.

What the PL/I preprocessor can do is generate the four different
combinations of increment/decrement loops. Well, that would be more
important if the stuff inside the loop were more complicated.

That doesn't have anything to do with parallelization . The original
question didn't say why those four choices were needed.

-- glen
Nov 13 '05 #5

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

Similar topics

4
5974
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart in how I can accomplish this. Also, I don't know the the size of the array before hand as the image can be any size. I already have the my read and write gif functions working, but I just need to know how to reverse the contents.
1
13588
by: Scott | last post by:
I have a view that contains the following SQL1 and it takes about 500ms to run with Explain Plan EXP1. However, if I add an order by clause(e.g. select * from view1 order by ID desc), it will then take about 3 mins to return. The explain plan for the query with the order by clause is as follows (EXP2). The explain plan 2 shows that there is no changes to the index selection except adding the "Sort Order by" step. The total number of...
1
1822
by: Paolo Tavalazzi | last post by:
I have 2 query that differ only for order by clause. The time of execution of the two query is a lot of different. 1) explain analyze select tkstore.gruppo,tkstore.cassa,enabledcodes.sala,spettacoli.code from tkstore,enabledcodes,spettacoli where
2
2849
by: elein | last post by:
Yes, I vacuumed. Reproduced on both 7.3.2 and 7.5. Brain dead java beans want order by clauses in views that they use. my view is: select .... from bigtable b left join lookuptable l order by bigkey desc;
11
8094
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in a backcolor/forecolor/etc property and calculate the "reverse colour"? In other words, If a user picks 255 (red) for a control backcolor, I'd like to be able to calculate the opposite or negative of that colour and assign the control's...
46
2149
by: Albert | last post by:
Why doesn't: #include <stdio.h> void reverse(char, int); main() { char s;
8
4763
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
16
2087
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it should except that every traversal thru the string I've gotten it to repeat the "list" again. This is what it looks like: for char in x: mylist.append(char)
7
5102
by: benn686 | last post by:
Anyone know of an efficient way of reversing the bits of a word??
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.