简体   繁体   English

C编程,循环困难

[英]C Programming, Loop difficulties

I'm currently working on a problem and it's very annoying. 我目前正在处理一个问题,这很烦人。 It's a loop that I know how to do but trying to understand -WHY- the second case does not work. 我知道这是一个循环,但试图理解-为什么-第二种情况不起作用。

The first part is fine. 第一部分很好。

int n;
int trianglenumber;
trianglenumber = 0;
for (n = 1; n <= 200; n++)
    trianglenumber = trianglenumber + n;
printf("%I,", trianglenumber");

Output is 20100 输出是20100

int n;
int trianglenumber;
trianglenumber = 0;
int result = 0;
for (n = 1; n <= 200; n++)
    result = trianglenumber + n;
printf("%I,", trianglenumber"); 

The program responds with the solution of 0 which I do not understand. 程序以我不理解的0解决方案作为响应。 Why do you need to use trianglenumber twice and then add it to n ? 为什么需要两次使用trianglenumber ,然后将其添加到n Why can't you just set any variable to take the trianglenumber 's place like I did with result and get the same answer? 您为什么不能像我对result那样仅仅设置任何变量来代替trianglenumber的位置并得到相同的答案? Much appreciated. 非常感激。 Just trying to figure this out. 只是想弄清楚。 It's pretty basic I guess but I don't see it. 我猜这很基本,但我看不到。

In second case, trianglenumber + n; 在第二种情况下, trianglenumber + n; does not increment trianglenumber . 不增加 trianglenumber So, in every iteration of the loop, you are putting just the value of n in result . 因此,在循环的每次迭代中,您只将n的值放入result

Finally, you end up printing the trianglenumber which is still at the first assigned value, 0. That justifies the output you get. 最后,最终打印出仍然处于第一个分配值0的trianglenumber 。这证明您得到的输出是正确的。

That said, %I is not a standard conversion specifier, you should use %i . 也就是说, %I不是标准的转换说明符,您应该使用%i Using invalid conversion specifier invokes undefined behavior . 使用无效的转换说明符会调用未定义的行为

I commented out in the second fragment of code the lines that do not contain trianglenumber . 我在第二段代码中注释掉了不包含trianglenumber This is the result: 结果如下:

// int n;
int trianglenumber;
trianglenumber = 0;
// int result = 0;
// for(n = 1; n<=200; n++)
    result = trianglenumber + n;
printf("%I,", trianglenumber"); 

As you can see, trianglenumber is initialized with 0 and never modified. 如您所见, trianglenumber初始化为0并且从未修改。 The last line of code prints its value that was 0 all the time during the program execution 1 . 的最后一行代码打印其值,该值是0的程序执行期间1所有的时间。


1 This is not entirely true but it's ok for the scope of this discussion. 1这并非完全正确,但在本讨论的范围内是可以的。

I have a solution, there are a lot of errors : 我有一个解决方案,有很多错误:

#include <stdio.h>
int main (){
    int n;
    int trianglenumber=0;
    for(n = 1; n<=200; n++)
        trianglenumber += n;
    printf("%i", trianglenumber); 
return 0;
}

Use just triangualrnumber witout the result, also %I dosen't exist, put the "" correctlly, and triangualnumber never increments, it's just 0 不使用结果而仅使用triangualrnumber,也不会存在%I,正确地放置"" ,并且triangualnumber永远不会增加,它仅为0

  1. The program responds with the solution of 0 which I do not understand. 程序以我不理解的0解决方案作为响应。

You are printing "trianglenumber" which is not incrementing at all. 您正在打印根本不增加的“ trianglenumber”。 As "trianglenumber" is zero as you intilialize so output is zero. 由于“ trianglenumber”为零,因此输出为零。

  1. Why do you need to use trianglenumber twice and then add it to N? 为什么需要两次使用trianglenumber,然后将其添加到N?

First we create "trianglenumber" variable as type of integer. 首先,我们将“ trianglenumber”变量创建为整数类型。 Second "trianglenumber=0" because if you not initialize it, it may take some garbage value and you can't get desired output. 第二个“ trianglenumber = 0”,因为如果不初始化它,可能会占用一些垃圾值,并且无法获得所需的输出。

Writing trianglenumber = trianglenumber + n; 写出三角形数=三角形数+ n; because in each iteration value of trianglenumber gets incremented. 因为在每次迭代中,trianglenumber的值都会增加。

For example- 例如-

In first iteration. 在第一次迭代中。

trianglenumber = trianglenumber + n; 三角数=三角数+ n;

trianglenumber = 0 + 1 = 1 三角数= 0 + 1 = 1

In second iteration n = 2 and trianglenumber becomes 1 from first iteration. 在第二次迭代中,n = 2,并且三角数从第一次迭代开始变为1。

trianglenumber = trianglenumber + n; 三角数=三角数+ n;

trianglenumber = 1 + 2 = 3 三角数= 1 + 2 = 3

Similarly in each iteration. 在每次迭代中类似。

  1. I think from both above answers your 3rd question is answered. 我想从以上两个答案都可以回答您的第三个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM