Velocity Reviews > Use of nested loops.

Use of nested loops.

Neptune
Guest
Posts: n/a

 11-03-2003
Hello.

I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"
(Sam's series), and for nested loops, he writes (p116) "It's often
necessary to create a loop even when you are already in a loop." Then he
goes on to portray a contrived example that doesn't tell me under what
conditions a nested loop might be favoured as a solution? i.e. what are
nested loops useful for? What kinds of algorithms are served by nested
loops? etc. Is any of this making sense?

Anyway - thoughts welcomed.

- Andy

Ben Pfaff
Guest
Posts: n/a

 11-03-2003
Neptune <neptune@no_spam_here> writes:

> I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"
> (Sam's series), and for nested loops, he writes (p116) "It's often
> necessary to create a loop even when you are already in a loop." Then
> he goes on to portray a contrived example that doesn't tell me under
> what conditions a nested loop might be favoured as a solution?
> i.e. what are nested loops useful for? What kinds of algorithms are
> served by nested loops? etc. Is any of this making sense?

A typical beginner's example is printing a bar graph,

#include <stdio.h>

int main (void)
{
int bars[] = {1, 6, 2, 4, 9};
int i;

for (i = 0; i < sizeof bars / sizeof *bars; i++) {
int j;

printf ("%3d ", bars[i]);
for (j = 0; j < bars[i]; j++)
putchar ('*');
putchar ('\n');
}
return 0;
}
--
Peter Seebach on C99:
"[F]or the most part, features were added, not removed. This sounds
great until you try to carry a full-sized printout of the standard
around for a day."

Neptune
Guest
Posts: n/a

 11-03-2003
osmium wrote:
> Neptune writes:
>
>
>>I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"
>>(Sam's series), and for nested loops, he writes (p116) "It's often
>>necessary to create a loop even when you are already in a loop." Then he
>>goes on to portray a contrived example that doesn't tell me under what
>>conditions a nested loop might be favoured as a solution? i.e. what are
>>nested loops useful for? What kinds of algorithms are served by nested
>>loops? etc. Is any of this making sense?

>
>
> One case that might have some natural appeal is to compute the sum of all
> the elements of a two-dimensional array. C only simulates (emulates?
> whatever) a two-dimensional array, but the simulation is pretty effective.
>
> Something like:
>
> double sum = 0.0;
> for(i=0; i<10; i++)
> for(j=0; j<25; j++)
> sum = sum + a[i][j];
>
> It is often helpful to hide the nesting. This can be accomplished very
> effectively by calling a function. It lets one focus on the thing of most
> immediate interest. EG: sum_array() calls sum_columns().
>
>

1. I get the idea in terms of the structure of a nested for loop and its
output (e.g. 2 lists of integers from 0 to 10 and 0 to 25) ... but
what does the 'a' stand-for in "sum = sum + a[i][j];"?
2. This is due to my ignorance, but under what conditions would one use
it? In this instance, why would I want a two-dimensional array, anyway?
What would I use it for? That was more to my question.

--
"Today a young man on acid realised that all matter was really energy
condensed to a slow vibration, that we are all one consciousness
experiencing itself subjectively, there's no such thing as death,
life is only a dream, and we're the imaginations of ourselves.
Here's Tom with the weather ..." - Bill Hicks.

Neptune
Guest
Posts: n/a

 11-03-2003
Ben Pfaff wrote:
> Neptune <neptune@no_spam_here> writes:
>
>
>>I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"
>>(Sam's series), and for nested loops, he writes (p116) "It's often
>>necessary to create a loop even when you are already in a loop." Then
>>he goes on to portray a contrived example that doesn't tell me under
>>what conditions a nested loop might be favoured as a solution?
>>i.e. what are nested loops useful for? What kinds of algorithms are
>>served by nested loops? etc. Is any of this making sense?

>
>
> A typical beginner's example is printing a bar graph,
>
> #include <stdio.h>
>
> int main (void)
> {
> int bars[] = {1, 6, 2, 4, 9};
> int i;
>
> for (i = 0; i < sizeof bars / sizeof *bars; i++) {
> int j;
>
> printf ("%3d ", bars[i]);
> for (j = 0; j < bars[i]; j++)
> putchar ('*');
> putchar ('\n');
> }
> return 0;
> }

Cheers Ben
That was helpful. Gives me an idea of its usage outside of text book
example. I haven't come across "sizeof" before. I presume that it is
some standard way of referencing the length of the array?
Thanks

--
"Today a young man on acid realised that all matter was really energy
condensed to a slow vibration, that we are all one consciousness
experiencing itself subjectively, there's no such thing as death,
life is only a dream, and we're the imaginations of ourselves.
Here's Tom with the weather ..." - Bill Hicks.

Mark A. Odell
Guest
Posts: n/a

 11-03-2003
Neptune <neptune@no_spam_here> wrote in news:3fa6ba48@212.67.96.135:

> That was helpful. Gives me an idea of its usage outside of text book
> example. I haven't come across "sizeof" before. I presume that it is
> some standard way of referencing the length of the array?
> Thanks

Sizeof is a nice operator (it's not a function) that tells you the size of
any "object".

E.g.

sizeof (int);
- tells you the size of an int on your platform (in bytes). For types you
must use the parenthesis.

struct foo
{
int a;
double d;
long *p;
} fooVar;

sizeof fooVar
- tells you the size of the variable fooVar (a struct) in bytes. You don't
need parenthesis for a variable when uses with the sizeof operator.

int var[64];

sizeof var;
- tells you the size of the var in bytes.

The sizeof operator is calculated at compile time so it has no run-time

--
- Mark ->
--

Keith Thompson
Guest
Posts: n/a

 11-03-2003
Neptune <neptune@no_spam_here> writes:
[...]
> That was helpful. Gives me an idea of its usage outside of text book
> example. I haven't come across "sizeof" before. I presume that it is
> some standard way of referencing the length of the array?
> Thanks

"sizeof" should be explained in that text book of yours. If it isn't,
you need a better text book.

BTW, another example of a nested loop might be reading lines from a
file, and processing each character in each line. You might use a
triple-nested loop to open each of the files named on the command
line, reading each line from each file, and processing each character
on each line.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"

Ben Pfaff
Guest
Posts: n/a

 11-03-2003
Neptune <neptune@no_spam_here> writes:

> osmium wrote:
> > double sum = 0.0;
> > for(i=0; i<10; i++)
> > for(j=0; j<25; j++)
> > sum = sum + a[i][j];

>
> 1. I get the idea in terms of the structure of a nested for loop and
> its output (e.g. 2 lists of integers from 0 to 10 and 0 to 25) ... but
> what does the 'a' stand-for in "sum = sum + a[i][j];"?

It's just the name of an array, perhaps declared as
double a[10][25];

> 2. This is due to my ignorance, but under what conditions would one
> use it? In this instance, why would I want a two-dimensional array,
> anyway? What would I use it for? That was more to my question.

Suppose you're calculating a statistical crosstabulation;
e.g. you have a bunch of survey responses from several people,
and the survey includes two questions, one of which has 10
Then it may be interesting to figure out how often each possible
combination of responses (25 * 10 = 250 possibilities) was given
by respondents. A two-dimensional array with cell values
corresponding to a count of people is a natural way to do this.
The sum of all of the cell values is then the total number of
survey respondents.

There are of course many other possibilities, too.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

Ben Pfaff
Guest
Posts: n/a

 11-03-2003
Neptune <neptune@no_spam_here> writes:

> Ben Pfaff wrote:
> > int bars[] = {1, 6, 2, 4, 9};
> > int i;
> > for (i = 0; i < sizeof bars / sizeof *bars; i++) {

> That was helpful. Gives me an idea of its usage outside of text book
> example. I haven't come across "sizeof" before. I presume that it is
> some standard way of referencing the length of the array?

sizeof yields the number of bytes in its operand. `sizeof bars'
is the number of bytes in array bars[]; `sizeof *bars' is the
number of bytes in a single element of bars[]. Thus, the
quotient of those two expressions is the number of elements in
bars[]; in this case, 5.
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99

osmium
Guest
Posts: n/a

 11-03-2003
Neptune writes:

> I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"
> (Sam's series), and for nested loops, he writes (p116) "It's often
> necessary to create a loop even when you are already in a loop." Then he
> goes on to portray a contrived example that doesn't tell me under what
> conditions a nested loop might be favoured as a solution? i.e. what are
> nested loops useful for? What kinds of algorithms are served by nested
> loops? etc. Is any of this making sense?

One case that might have some natural appeal is to compute the sum of all
the elements of a two-dimensional array. C only simulates (emulates?
whatever) a two-dimensional array, but the simulation is pretty effective.

Something like:

double sum = 0.0;
for(i=0; i<10; i++)
for(j=0; j<25; j++)
sum = sum + a[i][j];

It is often helpful to hide the nesting. This can be accomplished very
effectively by calling a function. It lets one focus on the thing of most
immediate interest. EG: sum_array() calls sum_columns().

David Rubin
Guest
Posts: n/a

 11-03-2003
Neptune wrote:

> > double sum = 0.0;
> > for(i=0; i<10; i++)
> > for(j=0; j<25; j++)
> > sum = sum + a[i][j];

[snip]
> 1. I get the idea in terms of the structure of a nested for loop and its
> output (e.g. 2 lists of integers from 0 to 10 and 0 to 25) ... but

In this example, a is a (two-dimensional) array, likely defined as

double a[10][25];

> what does the 'a' stand-for in "sum = sum + a[i][j];"?
> 2. This is due to my ignorance, but under what conditions would one use
> it? In this instance, why would I want a two-dimensional array, anyway?
> What would I use it for? That was more to my question.

Two-dimensional arrays are useful in many circumstances; numerical
programming is an obvious one. For example, this is how you would do
matrix addition, transposition, multiplication by a scalar, etc.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown