Search code examples
cfor-loopprintfscanfmagic-square

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. 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

Solution

  • You need to write the initial value in the first part of the for loop. 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. 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.

    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.