我该如何编写这个C代码? [英] How do I write this C code?
问题描述
问题是:
用for循环重写以下程序段:
< pre lang =c ++> count = 0 ;
i = 0 ;
while (i< n){
scanf( %d,& X);
if (X == i)
++ count;
++ i;
}
请帮帮我。如果愿意,请写出代码并解释原因。我知道怎么写for循环;但是,由于这个代码的设置方式,我很困惑,需要帮助。请不要使用我不知道的任何C ++语言,因为我只是在编程基础课程中而且我目前只学习C.
它实际上非常很简单,你只需要阅读for循环和while循环的工作原理。最好不要给你一个答案,而是给你一些指示。
for循环C [ ^ ]
在C中循环时 [ ^ ]
第3课:循环 [ ^ ]
你还应该学习并理解++ i和i ++的执行之间的区别。
谢谢大家的帮助。我只是误读了代码。答案是
count = 0 ;
for (i = 0 ; i < n; ++ i);
{
scanf( %d,& X);
if (X == i)
{
++ count;
}
}
出于某种原因,if语句在我脑海中没有产生共鸣所以我复杂化了这个问题。但是,谢谢大家的意见。
The question is:
Rewrite the following program segment with a for loop:
count = 0;
i = 0;
while (i < n) {
scanf("%d", &X);
if (X == i)
++count;
++i;
}
Please help me. If you would, please write out the code and explain why. I know how to write a for loop; however, because of how this code is set up, I am confused and in need of help. Please do not use any C++ language that I would not know since I am only in a Programming Fundamentals class and I am only currently learning C.
It is actually very simple and you only need to read up on how a for loop and a while loop works. It is better not to give you an answer, but to give you some pointers instead.
for loop in C[^]
while loop in C[^]
Lesson 3: Loops[^]
You should also learn and understand the difference between the execution of ++i and i++.
Thank you everyone for the help. I simply misread the code. The answer is
count = 0; for (i = 0; i < n; ++i); { scanf("%d", &X); if (X == i) { ++count; } }
For some reason the if statement didn't resonate in my head that it was there, so I over complicated the problem. But thank you everyone for your input.
这篇关于我该如何编写这个C代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!