0

I am getting an error as expected declaration or statement at end of input while doing programming practice in http://www.patest.cn/contests/mooc-ds2015spring/06-%E5%9B%BE2

Here is my code:

#include <stdio.h>
#include <malloc.h>
#include <math.h>

typedef struct Node {
    int x;
    int y;
} Position;

int result;
Position beast[1000];
int visited[1000];
int NumOfB, JumpAb;

void dfs(int i) {
    int j;
    if (Available(i)) {
        visited[i] = 1;
        if (Save(i)) {
            result = 1;
            printf("Yes");
    }
    for(j = 0; j < NumOfB; j++)
        if (!visited[j])
            dfs(j);
}

int Available(int i) {
    int j;
    double d_x, d_y;
    for (j = 0; j < NumOfB; j++) {
        d_x = beast[i].x - beast[j].x;
        d_y = beast[i].y - beast[j].y;
        if ((d_x * d_x + d_y * d_y) < JumpAb * JumpAb && visited[j] == 1)
            return 1;
    }
    return 0;
}

int Save(int i) {
    if ((abs(50 - abs(beast[i].x)) < JumpAb) || ((abs(50 - abs(beast[i].y))) < JumpAb))
        return 1;
    else 
        return 0;
}

int FirstJump(int i) {
    if ((beast[i].x * beast[i].x + beast[i].y * beast[i].y) < JumpAb * JumpAb) {
        return 1;
    } else {
        return 0;
    }   
}

int main() {
    result = 0;
    scanf("%d %d", &NumOfB, &JumpAb); 
    int i;
    Position *p = NULL;
    for (i = 0; i < NumOfB; i++) {
        p = (Position*)malloc(sizeof(Position));
        scanf("%d %d", &p->x, &p->y);
    }
    for (i = 0; i < NumOfB; i++) {
        visited[i] = 0;
//      if ((beast[i].x * beast[i].x + beast[i].y * beast[i].y) < JumpAb * JumpQAb)
//          visited[i] = 1; 
    }
    for(i = 0; i < NumOfB; i++) {
        if (!visited[i] && FirstJump(i)) {
            visited[i] = 1;
            dfs(i);
        }
    }
    if (result == 0) {
        printf("No");
    }
    return 0;
}

I could not find any error with the above function. Please help me to understand this error.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
liyi
  • 11
  • 2
  • The first `for` loop in `main` doesn't make any sense. And don't cast the result of `malloc` . You also have a memory leak as you don't `free` whatever you have `malloc`ed – Spikatrix Apr 17 '15 at 10:34
  • 2
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Apr 17 '15 at 10:36
  • 2
    When posting question please post the *complete* and *unedited* error log, also please point out *where* in the source the error is. – Some programmer dude Apr 17 '15 at 10:36

1 Answers1

3

You are missing a brace in this section:

if (Available(i)){
        visited[i]=1;
        if (Save(i)) {
            result = 1;
            printf("Yes");
    }
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Bathsheba
  • 231,907
  • 34
  • 361
  • 483