变量的范围大于for循环,为什么它不改变值
Scope of variable larger than for loop, why does it not change value?
我有以下代码:
void nLargestPalindrome(double number){
int i = 5; //subtract "1" because array starts at zero
int intermediate = i; //intermediate value - equal to the array index;
double *decimalDigit = new double[i + 1];
while (0 <= i){ //acquire digits of number
decimalDigit[i] = (number - fmod(number, pow(10, i))) / pow(10, i);
cout << "Value of digit place " << i << " is " << decimalDigit[i] << endl;
number = fmod(number, pow(10, i));
i--;
}
所以我初始化了一个数组,其值等于它所在的数字位置,即 979979 ---> dD[5] = 9,dD[4] = 7,等等......
现在我在同一个函数中有以下代码:
double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
for (int q = 0; q < 10; q++){ //decreases size of palindrome to the next largest palindrome.
cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << 't';
cout << numberVectorAddition << endl;
decimalDigit[2]--;
decimalDigit[3]--;
if (decimalDigit[2] == 0){
cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << 't';
cout << numberVectorAddition << endl; //runtime error??
...
}
}
我得到的以下输出number = 997799
Value of digit place 5 is 9
Value of digit place 4 is 9
Value of digit place 3 is 7
Value of digit place 2 is 7
Value of digit place 1 is 9
Value of digit place 0 is 9
997799 997799
996699 997799
995599 997799
994499 997799
993399 997799
992299 997799
991199 997799
990099 997799
989989 997799
988889 997799
987789 997799
什么给?我真诚地尝试在谷歌上环顾四周30分钟,因为我以前在这里遇到过一系列低质量的问题。但我没有成功。
我的问题:因为 numberVectorAddition 大于 for 循环范围,nVA 的值不应该改变吗?
行
double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
需要放置在for
环内。
for (int q = 0; q < 10; q++){ //decreases size of palindrome to the next largest palindrome.
double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << 't';
cout << numberVectorAddition << endl;
decimalDigit[2]--;
decimalDigit[3]--;
if (decimalDigit[2] == 0){
cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << 't';
cout << numberVectorAddition << endl; //runtime error??
...
}
否则,它将计算一次并保持在该值。
numberVectorAddition
不会自行神奇地改变,无论你如何初始化它,无论你是否使用其他一些可能改变的变量初始化它。一旦numberVectorAddition
获得它的值,那就是直到你分配给它......在这个代码中,你永远不会这样做!
相关文章:
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- 为什么 c++ 动态数组的大小没有改变?
- 为什么提升图库的 read_graphviz() 函数会改变节点的索引
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 为什么我的函数接受"std::string"进行排序不会改变它?
- 为什么切换 for 循环的顺序会显著改变执行时间?
- 如果没有重新散列,为什么 unordered_set::begin() 会改变?
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 为什么打印"OK"会改变输出?
- 为什么使用 2 个嵌套循环 O(n^2) 复杂度来解决二和问题,当只改变循环计数器逻辑时运行得更快?
- 解释为什么第二次分配会改变性能
- 为什么Qt QString的替换方法会改变QString?
- 为什么当条件为真时 x 的值没有改变?
- 为什么左值不能改变自己。左值用户是什么意思?
- 为什么不同的整数声明会改变结果?
- 为什么当我改变光源的位置时,光源不移动?
- 使用 String.resize() 后,为什么字符串的大小没有改变?
- 为什么我的函数不改变对象的属性
- 为什么改变这些指令的顺序会显著影响性能
- 为什么改变一个非const char*会导致分割错误