Advertisement
Guest User

Teste

a guest
Mar 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /*Finds X in map and returns position*/
  4. int findX(char map[1000][1000], int i, int j){
  5.     int x, y;
  6.     for(x = 0; x < i; x++){
  7.         for(y = 0; y < j; y++){
  8.             if(map[x][y] == 'X'){
  9.                 return x*j + y;
  10.             }
  11.         }
  12.     }
  13.     return -1;
  14. }
  15. /*Reads a char map*/
  16. void readMap(char map[1000][1000], int i, int j){
  17.     int x, y;
  18.     for(x = 0; x < i; x++){
  19.         for(y = 0; y < j; y++){
  20.             /* Reads char to correct matrix position*/
  21.             scanf("%c", &map[x][y]);
  22.         }
  23.         getchar();
  24.     }
  25. }
  26.  
  27. int main(){
  28.     /* Map width and height */
  29.     int w, h;
  30.     printf("What are map's demensions?\n");
  31.     scanf("%d %d", &w, &h);
  32.     getchar();
  33.     /* The map*/
  34.     char map[1000][1000];
  35.     readMap(map, w, h);
  36.     int r = findX(map, w, h);
  37.  
  38.     if(r == -1){
  39.         printf("Thre's no X\n");
  40.     }
  41.     else{
  42.         int x = r/w;
  43.         int y = r%w;
  44.        
  45.         printf("X is in position %d, %d\n", x, y);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement