简体   繁体   English

为什么我的程序在第一个for循环之后结束?

[英]Why is my program ending after the first for loop?

I'm writing a program that will calculate a magic square but I cant get the input to work right. 我正在编写一个可以计算出幻方的程序,但是我无法使输入正常工作。 I'm pretty sure I am filling the array with values but it ends right after the first two for loops. 我很确定我正在用值填充数组,但是它在前两个for循环之后立即结束。 The second two are supposed to print the array but the program ends right after entering the data. 前两个应该打印数组,但是程序在输入数据后立即结束。 Am I putting the loop in the wrong spot? 我会将循环放在错误的位置吗?

#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;
}

my input is: 我的输入是:

3
123
456
789

my expected output is 我的预期输出是

123
456
789

what I am getting is: 我得到的是:

123

0

0

456

0

0

789

0

0

After first loop, you need to reset row and col to 0: 第一次循环后,您需要将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]);
                        }
               }

Keep the habit to initialize your counter variable in loop, else it will be dangerous. 保持循环初始化计数器变量的习惯,否则会很危险。

You need to write the initial value in the first part of the for loop. 您需要在for循环的第一部分中写入初始值。 So instead of 所以代替

for(row; row<dimension; ++row) 

and

for(col; col<dimension; ++col)

use 采用

for(row = 0; row < dimension; ++row)

and

for(col = 0; col < dimension; ++col)

Also very important, you need to re-initialize variables row and col before you use them in the second loop. 同样非常重要,在第二个循环中使用变量之前,需要重新初始化rowcol Right now, when you reach the second loop (the printing one), row and col will already be equal to dimension from the first loop (the reading one), so you will never enter the second loop. 现在,当您到达第二个循环(打印一个)时, rowcol将已经等于第一个循环(读取一个)的dimension ,因此您将永远不会进入第二个循环。

for(row = 0; row<dimension; ++row)
{
    for(col = 0; col<dimension; ++col)
    {
        printf("%d", arr[row][col]);
    }
}

At the end, your program should be this : 最后,您的程序应为:

#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;
}

NOTE : I think you should change the statement 注意 :我认为您应该更改声明

printf("Please enter the data for row %d: ", ++rowNumber);

to : 至 :

printf("Please enter the data for element %d: ", ++rowNumber);

and your variable rowNumber to elementNumber , as you read elements for each box, not row. 当您读取每个框(而不是行)的元素时,将变量rowNumber更改为elementNumber

您没有正确初始化循环变量,当完成第一对循环时,它们的( colrow )值已经等于dimension ,这会使第二对循环的循环条件无效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM