This is another question (I posted one other one, go look!) from my computer science final study guide. I am not sure how to get the value of x[2] or x[5] without a computer (No computers used during the exam). Can someone please explain how to figure out these values just by reading the code? Thanks!
这是我的计算机科学最终学习指南中的另一个问题(我发布了另外一个,去看看!)。我不知道如何在没有计算机的情况下获得x [2]或x [5]的值(考试期间没有使用计算机)。有人可以通过阅读代码解释如何找出这些值吗?谢谢!
int x[SIZE] = {5, 3, 4, 5, 2, 4, 3, 5, 1, 2};
int i;
int j;
for(i = 0; i < SIZE / 2; i++)
{
for(j = x[i]; j < SIZE; j++)
{
x[j]++;
}
}
printf("x[2] = %d\n", x[2]);
printf("x[5] = %d\n", x[5]);
2 个解决方案
#1
Firstly, the array is
首先,阵列是
int x[SIZE] = {5, 3, 4, 5, 2, 4, 3, 5, 1, 2};
and SIZE
is 10 ( or I'll assume it is 10 ).
和SIZE是10(或者我认为它是10)。
The outer for loop is
外部for循环是
for(i = 0; i < SIZE / 2; i++)
so it iterates from i=0
to i=4
.
所以它从i = 0迭代到i = 4。
Now, let's look at the inner for loop
现在,让我们看看内部for循环
for(j = x[i]; j < SIZE; j++)
{
x[j]++;
}
The value assigned for j
is x[i]
.
为j分配的值是x [i]。
Firstly, at i=0
, the value of j
would be x[0]
which is 5
. So, the inner for loop executes from j=5
to j=9
. In the loop, it does x[j]++
, that is, x[5]++
, which is 4++
, which gives 5
. Now on the next iteration of the inner loop, j=6
, and x[6]++
, so 3++
, hence gives 4
. So, all the values of the array from x[5]
to x[9]
are incremented once.
首先,在i = 0时,j的值将是x [0],即5。因此,内部for循环从j = 5到j = 9执行。在循环中,它执行x [j] ++,即x [5] ++,即4 ++,它给出5.现在在内循环的下一次迭代中,j = 6,并且x [ 6] ++,所以3 ++,因此给出4.因此,从x [5]到x [9]的数组的所有值都递增一次。
So, the array now becomes
所以,阵列现在变成了
5 3 4 5 2 5 4 6 2 3
If you look at this pattern, you will notice that to for the value of x[2]
to change, j
must become 2
and for that x[i]
must be 2
, but looking at the the way the loop will progress, you can see that the values of x[0]
, x[1]
and x[2]
will not change. So, x[2]
will remain as 4
.
如果你看一下这个模式,你会注意到要改变x [2]的值,j必须变为2,而x [i]必须是2,但是看看循环的进展方式,你可以看到x [0],x [1]和x [2]的值不会改变。因此,x [2]将保持为4。
Now, moving on to the next iteration of the outer loop, i=1
, so j=x[1]
, so from j=3
to j=9
, increment the values of x[j]
, so the array becomes
现在,继续外循环的下一次迭代,i = 1,所以j = x [1],所以从j = 3到j = 9,增加x [j]的值,所以数组变为
5 3 4 6 3 6 5 7 3 4
For the next iteration at i=2
, the inner loop iterates from j=4
to j=9
, so the array becomes
对于i = 2的下一次迭代,内部循环从j = 4迭代到j = 9,因此数组变为
5 3 4 6 4 7 6 8 4 5
For the next iteration at i=3
, the inner loop iterates from j=6
to j=9
, and hence the array becomes
对于i = 3的下一次迭代,内部循环从j = 6迭代到j = 9,因此数组变为
5 3 4 6 4 7 7 9 5 6
And now, for the last iteration of the outer loop, at i=4
, the inner loop iterates from j=4
to j=9
, and the array becomes
现在,对于外部循环的最后一次迭代,在i = 4时,内部循环从j = 4迭代到j = 9,并且数组变为
5 3 4 6 5 8 8 10 6 7
So, the output of the program would be
所以,该计划的输出将是
x[2] = 4
x[5] = 8
This is how to interpret programs like these without a compiler.
这是如何在没有编译器的情况下解释这些程序。
#2
x
is an array. An array has a group of elements in it. The elements are indexed by a number. In C the index starts from 0
. So the first element has an index 0
, second one's index is 1
and so on.
x是一个数组。数组中包含一组元素。元素由数字索引。在C中,索引从0开始。因此第一个元素的索引为0,第二个元素的索引为1,依此类推。
So if you are looking for x[2]
that means the element at index 2
or position 3 which is 4
in the given code. But wait, there is a nested loop between the array definition and the printing of that value. This means that the original values in the array changes. So, you need to figure out what changes the loop makes to the array by manually iterating through it on a piece of paper. I'll leave that part to you.
因此,如果您正在寻找x [2],这意味着索引2或位置3的元素在给定代码中为4。但是等等,数组定义和该值的打印之间存在嵌套循环。这意味着数组中的原始值会发生变化。因此,您需要通过在一张纸上手动迭代来确定循环对数组所做的更改。我会把那部分留给你。
#1
Firstly, the array is
首先,阵列是
int x[SIZE] = {5, 3, 4, 5, 2, 4, 3, 5, 1, 2};
and SIZE
is 10 ( or I'll assume it is 10 ).
和SIZE是10(或者我认为它是10)。
The outer for loop is
外部for循环是
for(i = 0; i < SIZE / 2; i++)
so it iterates from i=0
to i=4
.
所以它从i = 0迭代到i = 4。
Now, let's look at the inner for loop
现在,让我们看看内部for循环
for(j = x[i]; j < SIZE; j++)
{
x[j]++;
}
The value assigned for j
is x[i]
.
为j分配的值是x [i]。
Firstly, at i=0
, the value of j
would be x[0]
which is 5
. So, the inner for loop executes from j=5
to j=9
. In the loop, it does x[j]++
, that is, x[5]++
, which is 4++
, which gives 5
. Now on the next iteration of the inner loop, j=6
, and x[6]++
, so 3++
, hence gives 4
. So, all the values of the array from x[5]
to x[9]
are incremented once.
首先,在i = 0时,j的值将是x [0],即5。因此,内部for循环从j = 5到j = 9执行。在循环中,它执行x [j] ++,即x [5] ++,即4 ++,它给出5.现在在内循环的下一次迭代中,j = 6,并且x [ 6] ++,所以3 ++,因此给出4.因此,从x [5]到x [9]的数组的所有值都递增一次。
So, the array now becomes
所以,阵列现在变成了
5 3 4 5 2 5 4 6 2 3
If you look at this pattern, you will notice that to for the value of x[2]
to change, j
must become 2
and for that x[i]
must be 2
, but looking at the the way the loop will progress, you can see that the values of x[0]
, x[1]
and x[2]
will not change. So, x[2]
will remain as 4
.
如果你看一下这个模式,你会注意到要改变x [2]的值,j必须变为2,而x [i]必须是2,但是看看循环的进展方式,你可以看到x [0],x [1]和x [2]的值不会改变。因此,x [2]将保持为4。
Now, moving on to the next iteration of the outer loop, i=1
, so j=x[1]
, so from j=3
to j=9
, increment the values of x[j]
, so the array becomes
现在,继续外循环的下一次迭代,i = 1,所以j = x [1],所以从j = 3到j = 9,增加x [j]的值,所以数组变为
5 3 4 6 3 6 5 7 3 4
For the next iteration at i=2
, the inner loop iterates from j=4
to j=9
, so the array becomes
对于i = 2的下一次迭代,内部循环从j = 4迭代到j = 9,因此数组变为
5 3 4 6 4 7 6 8 4 5
For the next iteration at i=3
, the inner loop iterates from j=6
to j=9
, and hence the array becomes
对于i = 3的下一次迭代,内部循环从j = 6迭代到j = 9,因此数组变为
5 3 4 6 4 7 7 9 5 6
And now, for the last iteration of the outer loop, at i=4
, the inner loop iterates from j=4
to j=9
, and the array becomes
现在,对于外部循环的最后一次迭代,在i = 4时,内部循环从j = 4迭代到j = 9,并且数组变为
5 3 4 6 5 8 8 10 6 7
So, the output of the program would be
所以,该计划的输出将是
x[2] = 4
x[5] = 8
This is how to interpret programs like these without a compiler.
这是如何在没有编译器的情况下解释这些程序。
#2
x
is an array. An array has a group of elements in it. The elements are indexed by a number. In C the index starts from 0
. So the first element has an index 0
, second one's index is 1
and so on.
x是一个数组。数组中包含一组元素。元素由数字索引。在C中,索引从0开始。因此第一个元素的索引为0,第二个元素的索引为1,依此类推。
So if you are looking for x[2]
that means the element at index 2
or position 3 which is 4
in the given code. But wait, there is a nested loop between the array definition and the printing of that value. This means that the original values in the array changes. So, you need to figure out what changes the loop makes to the array by manually iterating through it on a piece of paper. I'll leave that part to you.
因此,如果您正在寻找x [2],这意味着索引2或位置3的元素在给定代码中为4。但是等等,数组定义和该值的打印之间存在嵌套循环。这意味着数组中的原始值会发生变化。因此,您需要通过在一张纸上手动迭代来确定循环对数组所做的更改。我会把那部分留给你。