1

I have this code and have no idea why my output is incorrect. I'm used to C++ not C.

Run using: ./sudoku.o < inputfile

int row = 8;
int col = 8;
int puzzle[row][col];

for (int r = 0; r < row; r++){
    for (int c = 0; c < col; c++){                                                      
      scanf("%d", &puzzle[r][c]);
    }
}
for (int r = 0; r < row; r++){
    for (int c = 0; c < col; c++){
        printf("%d", puzzle[r][c]);
    }
    printf("\n");
}

this is my input

827154396 
965327148
341689752
593468271
472513689
618972435
786235914
154796823
239841567 

and this is the output I'm getting

-132765-84896035232594-208491627232765-208491623232765
00-84896120832594-84897084832594-85115713632594
10-848970848325941000
10-8489612083259400-208491648032765
1700966438000-84896035232594-208491641632765
-20846176001-208491643232765-163754450041951850
-1000-85506816832594-84897220032594
0000-20849160083276500
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • You've not checked the return value from `scanf()`. You should. (You'd use `if (scanf("%d", &puzzle[r][c]) != 1) { …report error… }`). – Jonathan Leffler Apr 13 '17 at 04:39
  • how would I do this? – Julie Rosen Apr 13 '17 at 04:40
  • 1
    However, your real problem is that you want to read 1-digit numbers, but your code reads 9-digit numbers until you run out of input. You need to use `"%1d"` to limit the input to single digits. If you tested the result from `scanf()`, you'd find out about the trouble because it would say EOF long before your input loops were complete. – Jonathan Leffler Apr 13 '17 at 04:41
  • Yes just Make Sure With Your data type and than write %d or whatever – Ajay Pandya Apr 13 '17 at 04:42
  • 1
    The posted code can't produce the said output. Have you posted it correctly? – Support Ukraine Apr 13 '17 at 05:19

1 Answers1

4

As noted in the comments, you should check that scanf() is successful (in this context, returns 1), and you need to make sure it reads only single digits with "%1d". Like this:

#include <stdio.h>

int main(void)
{
    int row = 8;
    int col = 8;
    int puzzle[row][col];

    for (int r = 0; r < row; r++)
    {
        for (int c = 0; c < col; c++)
        {
            if (scanf("%1d", &puzzle[r][c]) != 1)
            {
                fprintf(stderr, "Failed to read puzzle[%d][%d]\n", r, c);
                return 1;
            }
        }
    }
    for (int r = 0; r < row; r++)
    {
        for (int c = 0; c < col; c++)
        {
            printf("%d", puzzle[r][c]);
        }
        printf("\n");
    }

    return 0;
}

With your data file, the output is (surprise, surprise):

82715439
69653271
48341689
75259346
82714725
13689618
97243578
62359141

"Ah", you said, "I wanted 9 numbers per line". But your code doesn't say that; you set row and col to 8, so you only read 8 numbers per row of the matrix, and only 8 rows. And the ninth number on the first row of date becomes the first number on the second row of your matrix, etc.

Change the limits to 9 to get the result you really wanted:

827154396
965327148
341689752
593468271
472513689
618972435
786235914
154796823
239841567

If you want line-by-line input (not necessarily a bad idea), you'd need to use fgets() (or perhaps POSIX getline()) to read lines, and then sscanf() to read the numbers — see How to use sscanf() in loops?

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278