# Incorrect output for larger test cases

I am trying to solve this problem : https://icpc.kattis.com/problems/different

Here’s my code :

``````#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void)
{
unsigned long long int x, y, z;
while (scanf("%llu%llu", &x, &y) != EOF)
{
printf ("%llu\n", abs (x - y) );
if (scanf ("%llu%llu", &x, &y) == EOF)
break;
printf ("%llu\n", abs (x - y) );
}

return 0;
}
``````

It gives correct output for smaller test cases but fails for the larger ones. I am using GCC 4.8.1 on Windows XP 32 BIT Edition.

Input :

``````1 2
232 23
23 3
456 6
4 566
45 5
45 5
666 4
34 4343
34 34
34 5646
5645 32423
23423 32423
0 5
65 0
10 12
1871293781758123 72784
1 9223372036854775807
``````

Output :

``````1
209
20
450
562
40
40
662
4309
0
5612
26778
9000
5
65
2
1994345381
2
``````

AFAIK `unsigned long long int` should be enough for handling 20 digit integers.

Also, is there a way for generating large number of test cases to test my code instead of manually typing them?

1 Like

First of all you don’t have to use unsigned long long. Since, long long is enough to handle 2^63 -1.

Here, is the correct

``````
[1] but little differnt one.

For test cases. Yes, you can write a program for it like this.

#include <stdio.h>
int main(){
long long i,j;
for(i = 1,j = 100000;i<50000000;i+= 50,j+=23)
printf("%lld %lld\n",i,j);
return 0;
}

By changing the values you can generate as many test cases you want.

[1]: http://ideone.com/OLGNHy``````
1 Like

Thanks for the test-case generator code. I understand your solution but I want to know what’s wrong with my code.

I changed the variable type to long long int and also changed the identifier accordingly but it still doesn’t work. http://ideone.com/08k1qo

The problem, I think, is the `abs` function.

I believe `abs` function takes an `int` argument and returns an `int`. Now, `int` is a 32-bit signed data type. Naturally, only the lower-order 32 bits of an argument will get passed to the function. So, you lose some bits here.

Something similar might be happening, when the returned value is converted to `long long` (because of the %lld format specifier).

PS: I am not at all sure about these. But, apart from these, your code looks fine!

1 Like
//