for和数组 [英] for and arrays

查看:75
本文介绍了for和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解这段代码。


int a [5];

int b;

for(b = 0 ; b< 5; b = b + 1)

int a [b];


这应该采用数组的每个元素并将其设置为1,2,3,4,5。

太棒了。现在提出一个大问题。你会怎么做?


int a [5] [3];


并将5的所有零的第一个元素和3的第二个元素相等

到1,2,3?我不知道如何处理二维数组。


Bill

I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

This should take every element of the array a and set it to 1,2,3,4,5.
Great. Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don''t know how to work with a 2 dimensional array.

Bill

推荐答案

" ;比尔坎宁安 < no **** @ nspam.comwrites:
"Bill Cunningham" <no****@nspam.comwrites:

我理解这段代码。


int a [5] ;

int b;

for(b = 0; b< 5; b = b + 1)

int a [b];
I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];



你试过吗?

Did you try?


这应该取数组的每个元素并将其设置为1 ,2,3,4,5。

太棒了。
This should take every element of the array a and set it to 1,2,3,4,5.
Great.



否,一个设置的循环事情通常会在

身上有一个任务。

No, a loop that "sets" things will usually have an assignment in its
body.


现在提出一个大问题。你会怎么做?


int a [5] [3];


并将5的所有零的第一个元素和3的第二个元素相等

到1,2,3?我不知道如何使用二维数组。
Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don''t know how to work with a 2 dimensional array.



我会坚持使用1维的。如果您在学习C的过程中遇到了

的挑战,那么在继续之前掌握简单的

位要好得多。如果你有一些目标(即你想要写一些特定的程序),那么你应该选择一种语言

,比C更少陷阱和特点。特别是

解释语言让你通过尝试解决问题,我怀疑这是一种适合你的风格。


-

Ben。

I''d stick with 1 dimensional ones. If you are in this for the
challenge of learning C, then it is much better to master the easy
bits before moving on. If you have some objective in mind (i.e. you
want to write some specific program) then you should pick a language
with fewer pitfalls and peculiarities than C. In particular,
interpreted languages let you learn by trying things out which, I
suspect, is a style that would suit you.

--
Ben.




" Ben Bacarisse" < be ******** @ bsb.me.ukwrote in message

news:87 ************ @ bsb.me.uk .. 。

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...

" Bill Cunningham" < no **** @ nspam.comwrites:
"Bill Cunningham" <no****@nspam.comwrites:

>我理解这段代码。

int a [5];
int b;
for(b = 0; b <5; b = b + 1)
int a [b];
>I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];



你试过吗?


Did you try?



我试过并编译没有错误。或者至少编译完毕。

是的,这是一个学习的练习。我所做的只是一个学习的练习。什么

如果有500个整数的数组并且设置它们的简单任务

1-500?这对另一个人来说是怎样的。


int a [10];

int b;

for(b = 0; b< 10; b ++){


a [b = b + 1];

}

Bill

Well I tried and it compiled with no errors. Or atleast compiled anyway.
Yes this is an excercise to learn. All I do is an excercise to learn. What
if one had an array of 500 ints and had the simple task of setting them
1-500? How''s this for another take.

int a[10];
int b;
for (b=0;b<10;b++) {

a[ b=b+1];
}
Bill


" Bill Cunningham" < no **** @ nspam.comwrites:
"Bill Cunningham" <no****@nspam.comwrites:

" Ben Bacarisse" < be ******** @ bsb.me.ukwrote in message

news:87 ************ @ bsb.me.uk .. 。
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...

>" Bill Cunningham" < no **** @ nspam.comwrites:
>"Bill Cunningham" <no****@nspam.comwrites:

>>我理解这段代码。

int a [5] ;
int b;
for(b = 0; b <5; b = b + 1)
int a [b];
>>I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];


你试过吗?


Did you try?



我试过,编译时没有错误。或至少编译了

无论如何。


Well I tried and it compiled with no errors. Or atleast compiled
anyway.



你现在必须知道编译的程序就像一个

的拼写检查。这并不意味着你已经回答了这个问题。

You must have found out by now that a program that compiles is like an
essay the spell-checks. It does not mean you''ve answered the question.


是的,这是一个学习的练习。
Yes this is an excercise to learn.



然后编译它是不够的。关于

编程的精彩之处在于,您的程序可以告诉您是否正确。

Compiling it is not enough, then. The wonderful thing about
programming is that your program can tell you if you''ve got it right.


我所做的只是一个练习学习。什么

如果有500个整数的数组并且设置它们的简单任务

1-500?这对另一个人来说是怎样的。


int a [10];

int b;

for(b = 0; b< 10; b ++){


a [b = b + 1];

}
All I do is an excercise to learn. What
if one had an array of 500 ints and had the simple task of setting them
1-500? How''s this for another take.

int a[10];
int b;
for (b=0;b<10;b++) {

a[ b=b+1];
}



让我们假设您打算写一个[500]而不是[10]。你需要移动一个角色来将上面的内容转换为你想要的

a程序,但你能看到哪个角色需要吗?
需要移动到哪里?


[我仍然认为你应该学习一种解释性语言。]


-

Ben。

Let''s assume that you intended to write a[500] rather than a[10]. You
need to move one character characters to turn the above into
a program that does what you want, but can you see which character
needs to move at to where?

[I still think that you should learn an interpreted language.]

--
Ben.


这篇关于for和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆