hi

i have an iterative function, and sadly
i cant figure out how to save memory, algorithmically, swapping pointers instead of storing values in array.
any help greatly appreciated...

foo = bar operation1 bar
bar = bar operation2 bar
boom = foo operation4 boom operation5 boom

must be easy
Posted on 2010-10-01 06:51:42 by Turnip
Turnip,

As I understood it:

  • You have an array of somethings with key.

  • You don't want to exchange entire somethings, just pointers to them.

Looks like some sort-like algorithm. There are many ways to exchange two values, no matter how long are them. How long are yours? ;)
Posted on 2010-10-01 12:42:31 by baldr
hello

right now i am not  really concerned with actual implementation (and size) but algorithmic part.

i have to either allocate a large array, depending on number of iterations my function has, or i can allocate some pointers and swap them.

arrays are foo[], bar[], boom[].

say we input
foo[1] =x1,
foo[2] = x2,

bar[1] = y1,
bar[2] = y2, 

boom[1] = z1
boom[2] = y2

then i have a loop like this

while (func(i))
{
    i++;
    foo = bar + bar
    bar = bar *bar
    boom = foo / boom - boom
}


and if func(i) has 100 states before becoming zero, i have to allocate 300 memory cells.

but i can allocate only say bar[3], foo[3], boom[3], and then sorta changing pointer cyclically to store only most recent calculations.

now i am an algorithmic ignoramus and not very bright , but i know this trick is widely used by progers who want to save space.



Posted on 2010-10-01 13:48:47 by Turnip
Not sure what you want, but perhaps it is just this?
while (func(i))
{
    i++;
    foo = bar[(i-2) % 3] + bar[(i-1) % 3]
    bar = bar[(i-2) % 3] *bar[(i-1) % 3]
    boom = foo / boom[(i-1) % 3] - boom[(i-2) % 3]
}
Posted on 2010-10-01 18:23:11 by LocoDelAssembly
thanks. i knew it was easy!
Posted on 2010-10-02 02:41:01 by Turnip
modulo 3? not much point having an array at all, hehe
Posted on 2010-10-02 11:10:31 by Homer