Assuming that the inputs are:
6 4
0 1 2 2 1 0
1 0 0 0 0 1
1 0 0 0 0 1
0 1 1 1 1 0
6 and 4 are width and height respectively. What I used i
You can use gets
, strtok
and atoi
to get it done.
Following is complete working code. See here working:
#include <stdio.h>
#include <string.h>
int main(void) {
int w, h, i, j;
char buffer[100];
scanf("%d %d", &w, &h);
int num[w][h];
gets(buffer);
for(i=0; i<h; i++)
{
gets(buffer);
char *token = strtok(buffer, " ");
for(j =w; j && token; j--)
{
num[i][w-j] = atoi(token);
token = strtok(NULL, " ");
}
if(j)
{
//Do what you wish to do on error
printf("\nError!!! %d inputs are missing\n", j);
}
}
for(int i=0; i<h; i++)
{
for(int j =0; j<w; j++)
printf("%d ", num[i][j]);
printf("\n");
}
return 0;
}