英文:
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:"
"以下程序可以运行:"
#include <stdio.h>
int main()
{
int i, a[5], b[6];
for (i = 0; i <= 6; i++)
{
a[i] = i;
b[i] = i;
}
for (i = 0; i <= 6; i++)
{
printf(" %d", a[i], b[i]);
}
return 0;
}
"But why the following program doesn't run?"
"但为什么以下程序不运行?"
#include <stdio.h>
int main()
{
int i, j, a[5][5];
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= 5; j++)
{
a[i][j] = j;
}
}
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= 5; j++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
return 0;
}
"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:
#include <stdio.h>
int main()
{
int i, a[5], b[6];
for (i = 0; i <= 6; i++)
{
a[i] = i;
b[i] = i;
}
for (i = 0; i <= 6; i++)
{
printf(" %d", a[i], b[i]);
}
return 0;
}
But why the following program doesn't run?
#include <stdio.h>
int main()
{
int i, j, a[5][5];
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= 5; j++)
{
a[i][j] = j;
}
}
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= 5; j++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
return 0;
}
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 个整数单元,从零到四开始。不包括五。
修正后的第一个代码:
#include <stdio.h>
int main()
{
int i, a[5], b[6];
for (i = 0; i <= 4; i++)
{
a[i] = i;
}
for (i = 0; i <= 5; i++)
{
b[i] = i;
}
for (i = 0; i <= 5; i++)
{
if(i<5)
printf("a= %d\nb= %d", a[i], b[i]);
else
printf("b=%d",b[i]);
}
return 0;
}
对于第二个代码,同样不应该尝试使用边界。
修正后的第二个代码:
#include <stdio.h>
int main()
{
int i, j, a[5][5];
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
a[i][j] = j;
}
}
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
return 0;
}
英文:
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:
#include <stdio.h>
int main()
{
int i, a[5], b[6];
for (i = 0; i <= 4; i++)
{
a[i] = i;
}
for (i = 0; i <= 5; i++)
{
b[i] = i;
}
for (i = 0; i <= 5; i++)
{
if(i<5)
printf("a= %d\nb= %d", a[i], b[i]);
else
printf("b=%d",b[i]);
}
return 0;
}
for the second again you shouldn't try to use boundaries
Fixed code:
#include <stdio.h>
int main()
{
int i, j, a[5][5];
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
a[i][j] = j;
}
}
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论