为什么for循环在2D数组中出现溢出时不起作用,但在1D数组中仍然起作用

huangapple go评论137阅读模式
英文:

Why for loop doesn't work for 2D array with an overflow but still works for 1D array

问题

以下是翻译好的部分:

"The for loop to store values to two 1D arrays can store and print values even with an overflow but why a program to store values to 2D array using for loop doesn't run when there is an overflow?"

"用for循环将值存储到两个一维数组中,即使溢出,也可以存储和打印值,但为什么使用for循环将值存储到二维数组的程序在溢出时不运行?"

"The following program runs:"

"以下程序可以运行:"

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, a[5], b[6];
  5. for (i = 0; i <= 6; i++)
  6. {
  7. a[i] = i;
  8. b[i] = i;
  9. }
  10. for (i = 0; i <= 6; i++)
  11. {
  12. printf(" %d", a[i], b[i]);
  13. }
  14. return 0;
  15. }

"But why the following program doesn't run?"

"但为什么以下程序不运行?"

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, j, a[5][5];
  5. for (i = 0; i <= 5; i++)
  6. {
  7. for (j = 0; j <= 5; j++)
  8. {
  9. a[i][j] = j;
  10. }
  11. }
  12. for (i = 0; i <= 5; i++)
  13. {
  14. for (j = 0; j <= 5; j++)
  15. {
  16. printf(" %d", a[i][j]);
  17. }
  18. printf("\n");
  19. }
  20. return 0;
  21. }

"I tried to store the values in 2D array using nested for loop but it doesn't run. However, the program runs when 1D array is used."

"我尝试使用嵌套的for循环将值存储在二维数组中,但它不运行。然而,当使用一维数组时,程序可以运行。"

英文:

The for loop to store values to two 1D arrays can store and print values even with an overflow but why a program to store values to 2D array using for loop doesn't run when there is an overflow?

The following program runs:

  1. #include &lt;stdio.h&gt;
  2. int main()
  3. {
  4. int i, a[5], b[6];
  5. for (i = 0; i &lt;= 6; i++)
  6. {
  7. a[i] = i;
  8. b[i] = i;
  9. }
  10. for (i = 0; i &lt;= 6; i++)
  11. {
  12. printf(&quot; %d&quot;, a[i], b[i]);
  13. }
  14. return 0;
  15. }

But why the following program doesn't run?

  1. #include &lt;stdio.h&gt;
  2. int main()
  3. {
  4. int i, j, a[5][5];
  5. for (i = 0; i &lt;= 5; i++)
  6. {
  7. for (j = 0; j &lt;= 5; j++)
  8. {
  9. a[i][j] = j;
  10. }
  11. }
  12. for (i = 0; i &lt;= 5; i++)
  13. {
  14. for (j = 0; j &lt;= 5; j++)
  15. {
  16. printf(&quot; %d&quot;, a[i][j]);
  17. }
  18. printf(&quot;\n&quot;);
  19. }
  20. return 0;
  21. }

I tried to store the values in 2D array using nested for loop but it doesn't run. However, the program runs when 1D array is used.

答案1

得分: 0

第一个你分享的代码有效,因为你很幸运。当你说 int a[5] 时,这意味着你分配了 5 个整数单元,从零到四开始。不包括五。

修正后的第一个代码:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, a[5], b[6];
  5. for (i = 0; i <= 4; i++)
  6. {
  7. a[i] = i;
  8. }
  9. for (i = 0; i <= 5; i++)
  10. {
  11. b[i] = i;
  12. }
  13. for (i = 0; i <= 5; i++)
  14. {
  15. if(i<5)
  16. printf("a= %d\nb= %d", a[i], b[i]);
  17. else
  18. printf("b=%d",b[i]);
  19. }
  20. return 0;
  21. }

对于第二个代码,同样不应该尝试使用边界。

修正后的第二个代码:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, j, a[5][5];
  5. for (i = 0; i < 5; i++)
  6. {
  7. for (j = 0; j < 5; j++)
  8. {
  9. a[i][j] = j;
  10. }
  11. }
  12. for (i = 0; i < 5; i++)
  13. {
  14. for (j = 0; j < 5; j++)
  15. {
  16. printf(" %d", a[i][j]);
  17. }
  18. printf("\n");
  19. }
  20. return 0;
  21. }
英文:

First one that you shared works because you are lucky. When you say
int a[5] this means you allocated 5 unit for integer and it starts from zero to four. It does not include five.

Fixed code first one:

  1. #include &lt;stdio.h&gt;
  2. int main()
  3. {
  4. int i, a[5], b[6];
  5. for (i = 0; i &lt;= 4; i++)
  6. {
  7. a[i] = i;
  8. }
  9. for (i = 0; i &lt;= 5; i++)
  10. {
  11. b[i] = i;
  12. }
  13. for (i = 0; i &lt;= 5; i++)
  14. {
  15. if(i&lt;5)
  16. printf(&quot;a= %d\nb= %d&quot;, a[i], b[i]);
  17. else
  18. printf(&quot;b=%d&quot;,b[i]);
  19. }
  20. return 0;
  21. }

for the second again you shouldn't try to use boundaries
Fixed code:

  1. #include &lt;stdio.h&gt;
  2. int main()
  3. {
  4. int i, j, a[5][5];
  5. for (i = 0; i &lt; 5; i++)
  6. {
  7. for (j = 0; j &lt; 5; j++)
  8. {
  9. a[i][j] = j;
  10. }
  11. }
  12. for (i = 0; i &lt; 5; i++)
  13. {
  14. for (j = 0; j &lt; 5; j++)
  15. {
  16. printf(&quot; %d&quot;, a[i][j]);
  17. }
  18. printf(&quot;\n&quot;);
  19. }
  20. return 0;
  21. }

huangapple
  • 本文由 发表于 2023年7月23日 19:22:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定