Compilation Error in online judge, no problems compiling in my computer

Why is it that when I submitted a solution to an online programming judge, I get a COMPILATION ERROR verdict. But in my computer, the code compiles without any problem. My compiler is GCC, I am not aware of the online judge's compiler.

Here's the program specs
Input 

The first line of input is a number n denoting the number of cases. Each case starts with an input line consisted by 
three integers r, x, and y,  where, r is the radius of a fatal circular bomb explosion and (x, y) is 
the center of explosion. This is followed by a line that start with a number p followed by p pairs of numbers (xi, yi) 
where i = 1, 2, . . ., k. Each pair denotes the position of an organism.


Output
For each case, output the number of dead organisms and alive organisms using the format below:

a DEAD b ALIVE

where a is an integer denoting the number of dead organisms and b denotes the number of alive organisms.

Sample Input

2
4 5 10
4 (2,1) (5,8) (11,78) (3,7)
3 -1 5
2 (100,100) (0,0)

Sample Output

2 DEAD 2 ALIVE
0 DEAD 2 ALIVE


Here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;

int getX(char pt[], int len)
{
	char numChar[100];
	int indx = 0;
	for (int i = 1; i < len; i++)
	{
		if (pt[i] == ',')
		{
			numChar[indx] = '\0';
			break;
		}	
		else
		{
			numChar[indx] = pt[i];
		}
		indx++;
	}
	
	return atoi(numChar);
}

int getY(char pt[], int len)
{
	char numChar[100];
	int start, idx = 0;
	for (int i = 0; i < len; i++)
	{
		if (pt[i] == ',')
		{
			start = i + 1;
			break;
		}
	}
	for (int j = start; j < len - 1; j++)
	{
		numChar[idx] = pt[j];
		idx++;
	}
	numChar[len - 1] = '\0';

	return atoi(numChar);
}



int main()
{
	int n, idx = 0, radius, x, y, k, i, xi, yi, dead;
	cin >> n;
	while (idx < n)
	{
		cin >> radius >> x >> y >> k;
		char cartesian[100];
		dead = 0;
		i = 0;
		
		while (i < k)
		{
			cin >> cartesian;
			xi = getX(cartesian, strlen(cartesian));
			yi = getY(cartesian, strlen(cartesian));
			
			if (pow(xi - x, 2) + pow(yi - y, 2) <= pow(radius, 2))
				dead++;
			
			i++;
		}
		cout << dead << " DEAD " << k - dead << " ALIVE\n"; 
		idx++;
	}
}


Which part of this code could cause the compilation error in the judge's compiler? And how will I fix it? TIA!
What is the compilation error and which statement does generate it?
Last edited on
The online judge doesn't display the details of compilation errors, therefore I can't identify what's the statement generating the error. Can you help me spot it? I can't seem to find any 'risky' code or statement that might have caused the error.
It should tell you.
Post a complain to the judge.

¿which one are we talking about?
Sorry I am not going to spend my time when someone is saying about some mythic compilation error and even is unable to say the error message.
Last edited on
Was the assignment supposed to have been done in C?

If so the judge might have compiled with a C compiler, which would produce errors because your code has C++ in it ( cin cout etc).

Can you find out? Post the question exactly as it was written in full.

My compiler is GCC, I am not aware of the online judge's compiler.


if your compiler is gcc, that would have produced errors too, because of the C++. Use g++ to compile C++ programs. Maybe you are using an IDE and weren't aware of the compiler it uses.

If all that turns out be right and it does have to be in C, then change cout to printf functions and cin to scanf functions.

Edit:

Also change the includes, get rid of using namespace.

Btw your main doesn't return anything, that should attract a warning. Turn on the -Wall and -Wextra switches for the compiler.

Hope this helps
Last edited on
Sorry I am not going to spend my time when someone is saying about some mythic compilation error and even is unable to say the error message.


Sorry sir, my raw skills might also be able to identify the problem had the judge displayed error messages. Unfortunately, it didn't. Thanks.

Was the assignment supposed to have been done in C?

The online judge only accepts solutions written in C++. I have submitted a number of times for other problems and it worked fine, until this recent submission. Do you spot any statement or code which might not be acceptable in other compilers other than GCC? Thanks a lot.
Last edited on
Can't see a compile error. A warning maybe, in that main doesn't return a value. Could that be it?

Would be a bit anal if it was.
Can't see a compile error. A warning maybe, in that main doesn't return a value. Could that be it?

Would be a bit anal if it was.


Damn that might be it! The program might not be terminating properly in the judge's compiler. Didn't see that one. Thanks for the heads up iHutch105!
Do you spot any statement or code which might not be acceptable in other compilers other than GCC?


I can't say because I use g++ under KDevelop on linux. gcc stands for gnu c compiler, as I said above, you must be using g++ the C++ compiler.

Now some observations:

int n, idx = 0, radius, x, y, k, i, xi, yi, dead;

I look at this and think what do all these mean? I am a big fan of naming variables with words rather than a single letter, because helps someone to understand straight away, and can help prevent errors in code. Also initialise them at the same , it will save you one day.Try this instead:

1
2
3
4
5
6
7
8
9
10
int n=0;  //don't know what n is  give it a better name
int idx= 0; //don't know what idx is  give it a better name
int radius = 0;    //The radius of a circle used for what?
int XOrdinate = 0;   //The X part of a coordinate
int XOrdinate = 0;   //The Y part of a coordinate
int k = 0;      //don't know what k is give it a better name
int i=0;     //don't know what i is give it a better name
int xi=0;   //some relationship between x and i  give it a better name
int yi=0;   //some relationship between y and i  give it a better name
int dead =0;  //something died or is dead   give it a better name 


This seems really long winded, but it is much better alround. If someone is judging your code, doing this will earn you some brownie points.

I could probably sit here and figure out what they all mean, but I'd rather not - better to make it obvious right at the start.

The other thing to realise is, in the real world there are systems programmers & maintenance programmers. If you were the systems guy, and I was the maintenance guy - I would have something to say to your boss about this code. And your boss might fire you.

I am not trying to be really harsh - I am trying to point you towards some good practice.

1
2
i = 0;
while (i < k)


what if k == 0? you should check that k > I and take action if it isn't.

if (pow(xi - x, 2) + pow(yi - y, 2) <= pow(radius, 2))

You should get a lot of warnings doing this - these variable are all ints - pow takes doubles. It works because the compiler automatically casts the int to a double. It makes me wonder if they should be doubles in the first place .

cout << dead << " DEAD " << k - dead << " ALIVE\n";

if you are using cout, don't use \n, do this instead with endl

cout << dead << " DEAD " << k - dead << " ALIVE" << endl;

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int indx = 0;
	for (int i = 1; i < len; i++)
	{
		if (pt[i] == ',')
		{
			c = '\0';
			break;
		}	
		else
		{
			numChar[indx] = pt[i];
		}
		indx++;



Why put indx++ where it is, why not use numChar[i-1], then you wont need indx at all.

Hope this helps



Finally got it. This was my bad. Thanks for pointing it out TheIdeasMan. And thanks for the great advice.

if (pow(xi - x, 2) + pow(yi - y, 2) <= pow(radius, 2))

You should get a lot of warnings doing this - these variable are all ints - pow takes doubles. It works because the compiler automatically casts the int to a double. It makes me wonder if they should be doubles in the first place .
Also, I just noticed be really careful doing comparisons with floats or doubles, because they are stored in a binary representation and aren't exact. E.g. 0.1 can't be stored exactly so

1
2
double a = 0.1;
if (a* 10.0 == 1.0)   //almost guaranteed to be false 


Google Epsilon
Topic archived. No new replies allowed.