2016-04-10 13 views
0

Ich habe den folgenden Code:Wrong scanf in 2D-Array

#include <stdio.h> 

int main() { 

int tc,T; 
scanf("%d", &T); 

for(tc=0; tc<T; tc++){ 
    int flag=0; 
    int R,C; 

    scanf("%d %d", &R, &C); 
    { 
     int i,j; 
     int r,c; 
     int Grid[R][C]; 

     //Read the Grid 
     for(i=0; i<R; i++){ 
      for(j=0; j<C; j++){ 
       scanf("%d", &Grid[i,j]); 
      } 
     } 

     scanf("%d %d", &r, &c); 
     { 
      int Pattern[r][c]; 

      //Read the Grid 
      for(i=0; i<r; i++){ 
       for(j=0; j<c; j++){ 
        scanf("%d", &Pattern[i,j]); 
       } 
      } 
      //Here we have both the Grid and the Pattern 
      for(i=0; i<R; i++){ 
       for(j=0; j<C; j++){ 
        if(Grid[i,j]==Pattern[0,0] && ((i+r)<=R) && ((j+c)<=C)){ 
         int x,y; 
         int innerFlag=1; 

         for(x=0; x<r; x++){ 
          for(y=0; y<c; y++){ 
           if(Grid[x+i,y+j]!=Pattern[x,y]) innerFlag=0; 
          } 
         } 

         if(innerFlag == 1){ 
          //Set the flag to 1 and break out of the nested loops 
          flag=1; 
          j=C; 
          i=R; 
         } 
        } 
       } 
      } 

      //Here all the calculation is done and the flag is set 
      if(flag==1) printf("YES\n"); 
      else printf("NO\n"); 
     } 

    } 
} 

return 0; 
} 

die mir die folgenden Fehler gibt:

  1. Warnung: Format '% d' erwartet Argument vom Typ 'int ' , aber Argument 2 hat den Typ 'int () [(Sizetype) (C)]' scanf ("% d", & Gitter [i, j]);
  2. Warnung: Format '% d' erwartet Argument vom Typ 'int', aber Argument 2 hat 'int () [(Sizetype) (c)]' scanf ("% d" geben, & Muster [i, j]);

Wissen Sie, was mit diesem Code nicht stimmt?

P.S Ich habe auch das Raster und das Muster überprüft und es liest Müll!

+0

-Code für Grid/Muster? Ist '[]' für beide Klassen überladen, um zwei Ints als Argumente zu verwenden? – mwm314

+0

@ mwm314 Die Frage ist mit C markiert, und der Code scheint C zu sein, es gibt also keine Überladung. – user3386109

+0

Das Komma in 'Grid [i, j]' wird als [Komma Operator] interpretiert (https://en.wikipedia.org/wiki/Comma_operator), also ist 'Grid [i, j]' das gleiche wie ' Grid [j] '. – user3386109

Antwort

3

In C sollten Sie Arrays wie folgt tiefstellen: array[i][j].
array[i, j] entspricht array[j], da i ignoriert wird.

1

Auf Arrays in c wird nicht zugegriffen wie Grid[i, j] sondern als Grid[i][j]. Ändern Sie es in Ihrem Code. Ähnlich sollte Pattern[i, j] als Pattern[i][j] geschrieben werden.

Dies wird Ihr Problem lösen.

Glücklich zu helfen! :)

0

Probieren Sie etwas wie folgt vor:

#include <stdio.h> 

int main() { 

int tc,T; 
printf("Enter one number\n"); 
scanf("%d", &T); 

for(tc=0; tc<T; tc++){ 
int flag=0; 
int R,C; 
printf("Enter 2 numbers\n"); 
scanf("%d %d", &R, &C); 
{ 
    int i,j; 
    int r,c; 
    int Grid[R][C]; 

    printf("now reading the grid\n"); 
    //Read the Grid 
    for(i=0; i<R; i++){ 
     for(j=0; j<C; j++){ 
      scanf("%d", &Grid[i][j]); 
     } 
    } 

    printf("Enter 2 mre numbers\n"); 
    scanf("%d %d", &r, &c); 
    { 
     int Pattern[r][c]; 
     printf("now reading the pattern\n"); 

     //Read the Grid 
     for(i=0; i<r; i++){ 
      for(j=0; j<c; j++){ 
       scanf("%d", &Pattern[i][j]); 
      } 
     } 
     //Here we have both the Grid and the Pattern 
     for(i=0; i<R; i++){ 
      for(j=0; j<C; j++){ 
       if(Grid[i][j]==Pattern[0][0] && ((i+r)<=R) && ((j+c)<=C)){ 
        int x,y; 
        int innerFlag=1; 

        for(x=0; x<r; x++){ 
         for(y=0; y<c; y++){ 
          if(Grid[x+i][y+j]!=Pattern[x][y]) innerFlag=0; 
         } 
        } 

        if(innerFlag == 1){ 
         //Set the flag to 1 and break out of the nested loops 
         flag=1; 
         j=C; 
         i=R; 
        } 
       } 
      } 
     } 

     //Here all the calculation is done and the flag is set 
     if(flag==1) printf("YES\n"); 
     else printf("NO\n"); 
    } 

} 
} 

return 0; 
}