Re: Simple Loop



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:

I have an array of strings. Consider the following example:

cities = {Berlin, Lisbon, London, Madrid, New York, Paris, Roma, Tokio}
[snip]
But I want to fill the columns as follows:

Cities in 3 columns:
Berlin Madrid Roma
Lisbon New York Tokio
London Paris

1 -----> 4 -----> 7
2 -----> 5 -----> 8
3 -----> 6
[snip other example]
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
.



Relevant Pages

  • Re: Need Clarification
    ... has at least two strings in it: the C expressions my_strings and ... my_strings + 4 both point to the start of valid strings ... an array object, not a pointer object. ...
    (comp.lang.c)
  • Re: passing char arrays by reference
    ... I want to pass an array of strings to a function: ... In C, however, all argument expressions are passed by ... Probably because in C it's impossible to pass an array by value, ... by reference. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: String Changes...
    ... But I clean strings BEFORE playing. ... Austrlaia is the most urban country in the world, around 98% live in cities ... Being a Kiwi (NZ has no native Mammals - bar a Bat, Snakes, ...
    (alt.guitar)