C 为什么我的程序在第一个for循环后结束?
我正在写一个程序,将计算一个魔方,但我不能得到正确的输入工作。我很确定我正在用值填充数组,但它在前两个for循环之后结束。后两个应该打印数组,但程序在输入数据后立即结束。我把线圈放错位置了吗C 为什么我的程序在第一个for循环后结束?,c,for-loop,printf,scanf,magic-square,C,For Loop,Printf,Scanf,Magic Square,我正在写一个程序,将计算一个魔方,但我不能得到正确的输入工作。我很确定我正在用值填充数组,但它在前两个for循环之后结束。后两个应该打印数组,但程序在输入数据后立即结束。我把线圈放错位置了吗 #include <stdio.h> #define SIZE 15 int main(){ declareArray(); return 0; } int declareArray(){ int rowNumber = 0; int dimension=
#include <stdio.h>
#define SIZE 15
int main(){
declareArray();
return 0;
}
int declareArray(){
int rowNumber = 0;
int dimension=0;
int arr[SIZE][SIZE] = {0};
int col = 0;
int row = 0;
printf("Please enter the dimension of the square: ");
scanf("%d", &dimension);
arr[dimension][dimension];
for(row; row<dimension; ++row)
for(col; col<dimension; ++col){
printf("Please enter the data for row %d: ", ++rowNumber$
scanf("%d", &arr[row][col]);
}
for(row; row<dimension; ++row){
for(col; col<dimension; ++col){
printf("%d", arr[row][col]);
}
}
return 0;
}
我的预期产出是
123
456
789
我得到的是:
123
0
0
456
0
0
789
0
0
您需要在
for
循环的第一部分中写入初始值。所以不是
for(row; row<dimension; ++row)
最后,您的计划应该是:
#include <stdio.h>
#define SIZE 15
int main(){
declareArray();
return 0;
}
int declareArray(){
int rowNumber = 0;
int dimension=0;
int arr[SIZE][SIZE] = {0};
int col = 0;
int row = 0;
printf("Please enter the dimension of the square: ");
scanf("%d", &dimension);
arr[dimension][dimension];
for(row=0; row < dimension; ++row)
for(col=0; col < dimension; ++col){
printf("Please enter the data for row %d: ", ++rowNumber);
scanf("%d", &arr[row][col]);
}
for(row=0; row < dimension; ++row){
for(col=0; col < dimension; ++col){
printf("%d", arr[row][col]);
}
}
return 0;
}
致:
和变量
rowname
到elementNumber
,读取每个框的元素,而不是行的元素 您没有正确初始化循环变量,当前两个循环完成时,它们的(col
和row
)值已经等于dimension
,这会使第一个循环后的第二对循环的循环条件无效,您需要将row和col重置为0:
for(row = 0; row<dimension; ++row)
for(col = 0; col<dimension; ++col)
{
printf("Please enter the data for row %d: ", ++rowNumber$
scanf("%d", &arr[row][col]);
}
for(row = 0; row<dimension; ++row)
{
for(col = 0; col<dimension; ++col)
{
printf("%d", arr[row][col]);
}
}
for(row=0;rowfor(row;
-->for(row=0;
,for(col;
-->for(col=0;
@BLUEPIXY我很抱歉你这么说是什么意思?因为它在上一个循环中递增,所以必须重置。@BLUEPIXY ok我尝试过了,但是它现在一直要求越来越多的输入,不管在开始时指定了多少printf(“请输入第%d行的数据:,++rowNumber$
(++rowNumber));
):因为您只显示行数?对于(col=0;coli尝试了这一点,当我为每一组循环编写初始值时,不管用户输入什么,它都会要求越来越多的行。这对我来说很好。我用你应该运行的最后一个程序编辑了我的答案。你能试一下吗?当我输入3
时,它会要求我输入9个值,这是预期的。P很可能你输入了一个很大的数字,你认为它一直在问?当我输入3时,它会问我9次。不管我为每一行输入了什么。这就是它应该做的。你期望什么?@bills我认为你最初的问题已经解决了。这是一个不同的问题,所以你应该创建一个新的问题来解释在你的研究中发现了什么。另外,你可以接受一个答案。我试过了,它看起来就像当我为每一组循环写初始值时,不管用户输入了什么,它总是要求越来越多的行。当我点击你已经回答的答案后,如果我早一点看到的话,我不会费心去做pos这不是我的答案
for(row = 0; row < dimension; ++row)
for(col = 0; col < dimension; ++col)
for(row = 0; row<dimension; ++row)
{
for(col = 0; col<dimension; ++col)
{
printf("%d", arr[row][col]);
}
}
#include <stdio.h>
#define SIZE 15
int main(){
declareArray();
return 0;
}
int declareArray(){
int rowNumber = 0;
int dimension=0;
int arr[SIZE][SIZE] = {0};
int col = 0;
int row = 0;
printf("Please enter the dimension of the square: ");
scanf("%d", &dimension);
arr[dimension][dimension];
for(row=0; row < dimension; ++row)
for(col=0; col < dimension; ++col){
printf("Please enter the data for row %d: ", ++rowNumber);
scanf("%d", &arr[row][col]);
}
for(row=0; row < dimension; ++row){
for(col=0; col < dimension; ++col){
printf("%d", arr[row][col]);
}
}
return 0;
}
printf("Please enter the data for row %d: ", ++rowNumber);
printf("Please enter the data for element %d: ", ++rowNumber);
for(row = 0; row<dimension; ++row)
for(col = 0; col<dimension; ++col)
{
printf("Please enter the data for row %d: ", ++rowNumber$
scanf("%d", &arr[row][col]);
}
for(row = 0; row<dimension; ++row)
{
for(col = 0; col<dimension; ++col)
{
printf("%d", arr[row][col]);
}
}