0

I'm writing a c program, and my standard input looks something like this.

56543215
54176312
41235462
41235466
41255633
41256394
45236479
23696654

I'm trying to get this input into an 8x8 array and output the exact array. This is what I have so far but I don't get why it's not working? maybe because there are \n that I need to skip? Should declaration of the array be int arr[7][7]; instead? since it starts at 0?

#include <stdio.h>
#define ROW 8
#define COL 8

int main(int argc, char *argv[]) {
    int arr[ROW][COL];

    for (int i = 0; i < ROW; i++) {
        for (int j = 0; j < COL; j++) {
            scanf("%d", &arr[i][j]);
        }
    }

    for(int i = 0; i < ROW; i++) {
        for(int j = 0; j < COL; j++) {
            printf("%d", arr[i][j] );
        }
        printf("\n");
    }

    return 0;
}

2 Answers2

0

I think you are misunderstanding how scanf and how the format specifiers word. You have the right idea trying to read in a number (specifically a signed decimal integer); however integers as you know range from -infinity to infinity. Meaning that at the first iteration of the nested loop and you enter '56543215' it reads that as the integer value 56543215 rather than the 8 separate numbers 5, 6, 5, 4, 3, 2, 1, and 5. This is because the values you are entering in the terminal are not passed to scanf until you hit the enter key which is the default last character for the input string. If you passed each integer separately on its own line you would see the results you are looking for.

To get the results you want with the specified input you can simply specify that you want a decimal only 1 digit long. To do this specify scanf("%1d") rater than scanf(%d)

joshmeranda
  • 3,001
  • 2
  • 10
  • 24
0

Read a line at a time using gets() [or fgets() to read from file].
Scan the line and pull each digit out of the string.
Convert that digit character into into a number.
Put that number into the array.

#include <stdio.h>
#define ROW 8
#define COL 8

#include <stdlib.h> //for atoi(3)
#include <ctype.h> //for isdigit(3)

// digit to number as function
int ctoi(char ch) {
    if(isdigit(ch)) return ch - '0';
    return 0;
}

// build temporary string, use atoi()
int ctoi_s(char ch) {
    char temp[2];
    temp[1] = '\0'; // null-terminate
    temp[0] = ch;
    return atoi(temp);
}

// digit to number as macro
#define ctoi_macro(ch) (isdigit(ch) ? ((ch) - '0') : 0)

int
main(int argc, char *argv[]) {
    int ray2d[ROW][COL];
    char line[COL+2+100];

    for (int row = 0; row < ROW; row++) {
        gets(line,sizeof(line)-1);
        for (int col = 0; col < COL; col++) {
            ray2d[row][col] = ctoi(line[col]);
            //or: ray2d[row][col] = ctoi_s(line[col]);
            //or: ray2d[row][col] = ctoi_macro(line[col]);
        }
    }

    for(int row = 0; row < ROW; row++) {
        for(int col= 0; col < COL; col++) {
            printf("%d ", ray2d[row][col] );
        }
        printf("\n");
    }

    return 0;
}

See this: Convert a character digit to the corresponding integer in C

ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42