使用嵌套循环。 [英] Use of nested loops.
问题描述
你好。
我正在通过张's'在24小时(2e)教你自己C>
(Sam 's系列),对于嵌套循环,他写道(p116)即使你已经在循环中,创建一个循环也经常需要
。然后他继续描述一个人为的例子,并没有告诉我在什么情况下,嵌套循环可能会成为一个解决方案?即什么是
嵌套循环有用吗?嵌套的
循环提供哪些算法?这有什么意义吗? :)
无论如何 - 欢迎提出意见。
- 安迪
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
推荐答案
海王星写道:
Neptune writes:
我正在通过张's'在24小时(2e)教自己C
(Sam的系列),对于嵌套循环,他写道(p116)即使你已经处于循环中,也经常需要创建一个循环。然后他继续描绘一个人为的例子,这个例子并没有告诉我什么样的条件,嵌套循环可能会被作为一个解决方案?什么是嵌套循环有用的?嵌套的
循环提供哪些算法?这有什么意义吗? :)
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? :)
一个可能有一些自然吸引力的案例是计算二维数组元素的总和。 C只模拟(模仿?
无论如何)二维数组,但模拟非常有效。
类似于:
double sum = 0.0;
for(i = 0; i< 10; i ++)
for(j = 0; j< 25; j ++ )
sum = sum + a [i] [j];
隐藏嵌套通常很有帮助。通过调用函数可以有效地实现这一点。它让人们可以立即关注最重要的事情。 EG:sum_array()调用sum_columns()。
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().
Neptune< neptune @ no_spam_here>写道:
Neptune <neptune@no_spam_here> writes:
我正在通过张's'在24小时(2e)教你自己C&#
(Sam的系列),对于嵌套循环,他写道(p116)即使你已经处于循环中,也经常需要创建一个循环。然后
他接着描绘了一个人为的例子,并没有告诉我
嵌套循环可能作为解决方案有什么条件?
即什么是嵌套循环有用?嵌套循环提供哪些算法?这有什么意义吗? :)
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? :)
一个典型的初学者的例子是打印一个条形图,
,例如: (警告:校对不良):
#include< stdio.h>
int main(无效)
{
int bars [] = {1,6,2,4,9};
int i;
for(i = 0; i< sizeof bars / sizeof * bars; i ++){
int j;
printf("%3d" ;,bar [i]);
for(j = 0; j< bars [i]; j ++)
putchar(''*''); < br $> b $ b putchar(''\ n'');
}
返回0;
}
-
Peter Seebach在C99:
[F]或大部分功能已添加,未删除。这听起来很棒
很棒,直到你试着在一天内完成大小的标准打印输出
。
A typical beginner''s example is printing a bar graph,
e.g. (warning: poorly proofread):
#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."
>
osmium写道:
海王星写道:
Neptune writes:
我正在通过张's'的方式自学C 24小时(2e)
(Sam的系列),对于嵌套循环,他写道(p116)它通常是创建循环所必需的,即使你已经在循环中。然后他继续描绘一个人为的例子,这个例子并没有告诉我什么样的条件,嵌套循环可能会被作为一个解决方案?什么是嵌套循环有用的?嵌套的
循环提供哪些算法?这有什么意义吗? :)
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? :)
一个可能具有一些自然吸引力的案例是计算二维数组的所有元素的总和。 C只模拟(模拟?
无论如何)二维数组,但模拟非常有效。
类似于:
double sum = 0.0;
for(i = 0; i< 10; i ++)
for(j = 0; j< 25; j ++)
sum = sum + a [i] [j];
隐藏嵌套通常很有帮助。这可以通过调用函数非常有效地完成。它让人们专注于最直接的兴趣。 EG:sum_array()调用sum_columns()。
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.我理解了这个想法嵌套for循环的结构及其
输出(例如,从0到10和0到25的2个整数列表)......但是
是什么' 'a''代表'sum = sum + a [i] [j];" ;?
2.这是由于我的无知,但在什么条件下会使用
吗?在这个例子中,为什么我还想要一个二维数组?
我会用它做什么?这更像是我的问题。
-
今天一个年轻人在酸中意识到所有的事情都是真正的能量
压缩到一个缓慢的振动,我们都是一个意识
主观地体验,没有死亡这样的东西,
生命只是一个梦想,我们是自己的想象力。
这里有汤姆和天气...... - 比尔希克斯。
Thanks for replying:
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.
这篇关于使用嵌套循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!