Re: Simple Loop
- From: James Waldby <no@xxxxx>
- Date: Sat, 16 Jun 2007 17:18:38 -0500
shapper wrote:
On Jun 16, 9:32 pm, James Waldby <n...@xxxxx> wrote:....
....a*k mod m (eg, 3k mod 11) will give m distinct values (as k goes from 0
to m-1) if and only if a and m are coprime (ie, have no integer
divisors in common). It isn't obvious to me what sequence you want when
a=3 and m=18.
Let me give you an example what I am trying to do:[snip]
I have an array of strings. Consider the following example:
cities = {Berlin, Lisbon, London, Madrid, New York, Paris, Roma, Tokio}
But I want to fill the columns as follows:[snip other example]
Cities in 3 columns:
Berlin Madrid Roma
Lisbon New York Tokio
London Paris
1 -----> 4 -----> 7
2 -----> 5 -----> 8
3 -----> 6
The loop that displays the cities does it always in the direction
showed.
So I need to rearrange the array of strings so that my Display Loop
shows the cities as I described.
I want to display all cities in the string array.
Well, I hope I explained the situation well.
Yes, quite clear.
Consider pseudocode like the following (for n items in array D,
and first item is D[1], and p columns are desired, and integer
division truncates, and # starts a comment):
rows = (n+p-1)/p; # Now p*rows >= n > p*(rows-1)
for (i=0; i<n; ++i) {
row = i mod p; # Now rows > row >= 0
col = i/rows;
Display D[ 1 + row + col*rows];
if col+1 == p, start new row;
}
Note that the expressions for row and col could be substituted
into the subscript expression 1 + row + col*rows and then
simplified, but for programming clarity you might as well not
do so. Many compilers will optimize away the row and col
variables anyway.
into the expression
--
-jiw
.
- References:
- Simple Loop
- From: shapper
- Re: Simple Loop
- From: [Mr.] Lynn Kurtz
- Re: Simple Loop
- From: shapper
- Re: Simple Loop
- From: James Waldby
- Re: Simple Loop
- From: shapper
- Simple Loop
- Prev by Date: Re: Question about a^2+b^2=4k+1 prime ...
- Next by Date: Re: Simple Loop
- Previous by thread: Re: Simple Loop
- Next by thread: Re: Simple Loop
- Index(es):
Relevant Pages
|