简体   繁体   English

为什么此C ++函数会产生混乱的输出?

[英]Why does this C++ function produce chaotic output?

I have the following code: 我有以下代码:

int main ( int argc, char **argv ){
double lambda = 4;
double x = .2;

for ( int i=1; i<=30; i++ )
  {
    printf( "%.5f \n", x );
    x = lambda * x * (1-x);
  }           
}

That outputs the following: 输出以下内容:

0.20000 0.64000 0.92160 0.28901 0.82194 0.58542 0.97081 0.11334 0.40197 0.96156 0.14784 0.50392 0.99994 0.00025 0.00098 0.00394 0.01568 0.06174 0.23173 0.71212 0.82001 0.59036 0.96734 0.12638 0.44165 0.98638 0.05374 0.20342 0.64815 0.91221 0.20000 0.64000 0.92160 0.28901 0.82194 0.58542 0.97081 0.11334 0.40197 0.96156 0.14784 0.50392 0.99994 0.00025 0.00098 0.00394 0.01568 0.06174 0.23173 0.71212 0.82001 0.59036 0.96734 0.12638 0.44165 0.98638 0.05374 0.20342 0.64815 0.91221

My question is: What is the most apt algorithmic/mathematical description for the manner in which 'x' is being manipulated every iteration? 我的问题是:对于每次迭代操作“ x”的方式,最恰当的算法/数学描述是什么?

For each iteration of the loop, the variable x is assigned a new value based on some calculation involving the old value of x . 对于循环的每次迭代,都会根据涉及x的旧值的一些计算为变量x分配一个新值。 The variable i is used simply to limit the number of times this calculation repeats. 变量i仅用于限制此计算重复的次数。

Similarly, you can change the value of x (without using i ) in a much simpler way: 类似地,您可以以一种更简单的方式更改x的值(不使用i ):

x = x + 1;

Use that instead of the x = lambda * x * (1-x); 用它代替x = lambda * x * (1-x); line and observe how the value increases. 线,观察值如何增加。

You are using x in the following statement x = lambda * x * (1-x); 您在以下语句中使用x x = lambda * x * (1-x); . This way x value changes on every iteration. 这样, x值在每次迭代时都会更改。

The "i" in the for loop is known as the iteration variable, which means it will be used as a counter for keeping track of the no. for循环中的“ i”被称为迭代变量,这意味着它将用作跟踪no的计数器。 of iterations of the loop. 循环的迭代次数。

Whereas, the "x" is the variable which you want to play with in the loop. 而“ x”是您要在循环中使用的变量。

You can try printing both the values of i and x in the loop to understand the concepts of iteration. 您可以尝试在循环中同时打印i和x的值,以了解迭代的概念。

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

相关问题 为什么此C ++程序的输出会在cmd中产生巨大混乱? - Why does the output of this C++ program produce a giant mess in cmd? 为什么这个C ++函数会产生如此多的分支错误预测? - Why does this C++ function produce so many branch mispredictions? 为什么此代码不产生输出? 使用xcode的C ++二进制搜索 - Why this code does not produce output? c++ binary search using xcode 为什么vs c ++ 2010编译器为类似函数生成不同的汇编代码 - why does vs c++ 2010 compiler produce a different assembly code for similar function 为什么我的C ++模板化函数不会产生“未声明的已识别”编译错误? - Why my C++ templated function does not produce `undeclared identified` compile error? 为什么这个 C++ function 不是 output 我想要什么? - Why does this C++ function not output what I want? 为什么 C 或 C++ “-0” 不产生浮点 -0? - Why does C or C++ “-0” not produce a floating-point −0? 为什么这个表达式在 C# 和 C++ 中产生不同的结果? - Why does this expression produce different results in C# and C++? 为什么会产生正确的输出? - Why does this produce correct output? 为什么此c ++静态强制转换代码产生int而不是double - why does this c++ static cast code produce and int not a double
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM