1

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
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
bills
  • 59
  • 9

3 Answers3

2

After first loop, you need to reset row and col to 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.

Alden Ken
  • 142
  • 8
  • i tried that and it looks like when i write the initial value for each set of loops, regardless of what the user inputs it keeps asking for more and more rows – bills Nov 03 '17 at 07:48
0

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.

Marievi
  • 4,951
  • 1
  • 16
  • 33
  • i tried that and it looks like when i write the initial value for each set of loops, regardless of what the user inputs it keeps asking for more and more rows. – bills Nov 03 '17 at 07:45
  • 1
    It works fine for me. I edited my answer with the final program you should run. Could you try it? When I give input `3`, it asks me for 9 values, which is expected. Probably you are giving a large number as input and you think that it never stops asking? – Marievi Nov 03 '17 at 07:50
  • when i put in 3, it asks me 9 times. Regardless of what inputs I enter for each row – bills Nov 03 '17 at 07:55
  • Exactly. This is what it should do. What did you expect? – Marievi Nov 03 '17 at 07:57
  • I was expecting it to only ask 3 times. So data would be entered row by row – bills Nov 03 '17 at 08:01
  • @bills The way you have written your program, it asks element by element, not row by row. This is why I added the last edit in my post about the names. – Marievi Nov 03 '17 at 08:02
  • @ Marievi I see what you are saying now. what would be the best way to do it row by row? – bills Nov 03 '17 at 08:04
  • @bills I would suggest removing the inner `for` in your first, reading, loop. In other words, read row by row. – Marievi Nov 03 '17 at 08:12
  • ok i tried that and it added a 0 on two lines below each input. It did ask for input only 3 times. Any idea what those 0's might be? – bills Nov 03 '17 at 08:14
  • @bills it's the newlines (Enter) you press. You need to search a little online on how to exclude the newlines. – Marievi Nov 03 '17 at 08:15
  • It looks like a null terminated issue. I cant find how to get rid of them though. Any tips? – bills Nov 03 '17 at 08:38
  • 1
    @bills I think that your initial problem is solved. This is a different problem, so you should create a new question explaining what you found in your research. Also, you can accept one answer. – Marievi Nov 03 '17 at 09:24
0

You did not initialize your loop variable properly, when first couple of loops are done their(col and row) values are already equaled to dimension which invalidates loop condition for second couple of loop

Pras
  • 4,047
  • 10
  • 20
  • By the time I clicked on post answer you had already answered, if I would have seen it earlier wont have bothered to post my answer – Pras Nov 03 '17 at 08:08