1

I am trying to solve a problem that says:

Write a program that computes the difference between non-negative integers.

Input:

Each line of the input consists of a pair of integers. Each integer is between 0 and 10 raised to 15 (inclusive). The input is terminated by end of file.

Output:

For each pair of integers in the input, output one line, containing the absolute value of their difference.

Here is my solution:

#include <iostream>
#include <cmath> 
using namespace std;

int main(){
    int x;
    int y;
    int z;
    
    cin >> x >> y;
    
    if(x && y <= 1000000000000000){
        z=x-y;
        cout << std::abs (z);
    }
}

But the problem is my answer is wrong, please help me correct my answer and explain why it was wrong.

Community
  • 1
  • 1
Marr
  • 555
  • 1
  • 6
  • 11
  • Change `if(x && y <= 1000000000000000)` to `if(x <= 1000000000000000 && y <= 1000000000000000)`. BTW, this condition is always true **by definition** (of `int`). – barak manos Feb 14 '16 at 08:02
  • If `x` is zero, the `if` statement won't execute because 0 represents `false`. – Thomas Matthews Feb 14 '16 at 08:04
  • Check the capacity of `int` on your system. You may need to use `long` or `long long`. – Thomas Matthews Feb 14 '16 at 08:06
  • @barakmanos can you please elaborate a little what you meant by *always true by definition of int*? Notice that in the preceding line there is `cin` taking inputs for `x` and `y` – Sнаđошƒаӽ Feb 14 '16 at 08:16
  • @Shadowfax: Sorry, **by definition** is most definitely the wrong terminology here. But most likely, the size of `int` on OP's platform is 32 bits, which is not sufficiently large in order to "accommodate" a value of 1000000000000000. – barak manos Feb 14 '16 at 08:21

3 Answers3

1

For starters usually the range of valid values for objects of type int is less than 1000000000000000. You should use an integer type wide enough to store such big values. An appropriate type is unsigned long long int because according to the assignment entered values are non-negative.

Otherwise you will need also to check that the values are not negative that is greater than or equal to zero.

Also the condition in the if statement

if(x && y <= 1000000000000000){

is wrong. It is equivalent to

if(x && ( y <= 1000000000000000 )){

that in turn is equivalent to

if( ( x != 0 ) && ( y <= 1000000000000000 )){

it is not the same as

if ( ( x <= 1000000000000000 ) && ( y <= 1000000000000000 ) ){

The program can look the following way

#include <iostream>

int main()
{
    const unsigned long long int UPPER_VALUE = 1000000000000000;
    unsigned long long int x, y;

    while ( std::cin >> x >> y )
    {
        if ( x <= UPPER_VALUE && y <= UPPER_VALUE )
        {
            std::cout << "The difference between the numbers is "
                      << ( x < y ? y - x : x - y )
                      << std::endl;
        }
        else
        {
            std::cout << "The numbers shall be less than or equal to " 
                      << UPPER_VALUE 
                      << std::endl;
        }
    }        
}

If for example to enter these values

1000000000000000 1000000000000000 
1000000000000001 1000000000000000 
1 2

then the program output will look like

The difference between the numbers is 0
The numbers shall be less than or equal to 1000000000000000
The difference between the numbers is 1
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • To accommodate values less than `10^15`, `unsigned long long` is not needed. `long long` can well handle such numbers. Am I wrong? – Sнаđошƒаӽ Feb 14 '16 at 09:13
  • @Shadowfax In the assignment there is written that the values are non-negative. If you are going to use a signed integer type then you also need to check the entered values with zero. What is the reason for makeing your life harder?:) – Vlad from Moscow Feb 14 '16 at 09:16
  • Why would I need to compare entered values with zero? I thought I am making my life easier by *not* writing `unsigned` word! – Sнаđошƒаӽ Feb 14 '16 at 09:22
  • @Shadowfax You should follow the requirement of the assignment: "Write a program that computes the difference between non-negative integers." Otherwise your solution does not satisfy the assignment. – Vlad from Moscow Feb 14 '16 at 09:24
  • I don't want to argue, and I don't go along with your argument either. The assignment says the input numbers are going to be non-negative, it is just a piece of info given to me. Now it is up to me how I handle that non-negative number. I could if I wish handle that as a, say string, as long as it computes the difference. Actually, the OP is probably not an assignment, probably from some programming contests problem, like in UVa. I think I will just quit. It doesn't look good to argue with someone approx. 90 times you are, you know what I mean `:-)` – Sнаđошƒаӽ Feb 14 '16 at 09:36
  • @Shadowfax In any case you have to check that the given numbers are non-negative. If you do not do this then your solution is wrong. – Vlad from Moscow Feb 14 '16 at 09:42
  • The way the problems in those contests work is when they say the numbers are non-negative, they will definitely be non-negative, no exception. So we can safely ignore the numbers being otherwise. Reading the OP I had this thought intuitively lurking in my head, hence my response. – Sнаđошƒаӽ Feb 14 '16 at 09:44
  • @Shadowfax In this case there is no reason to declare the variables as signed. – Vlad from Moscow Feb 14 '16 at 09:45
  • told you, making life easier by not writing the extra word `unsigned`. And in such contests time is very important. Contestants define macros like `LL` to mean `long long`, `pb` to mean vector's `push_back` `:-)` – Sнаđошƒаӽ Feb 14 '16 at 09:47
  • @Shadowfax I do not think that writing unsigned takes much time.:) – Vlad from Moscow Feb 14 '16 at 09:48
  • You are right, but in contests, they want to shed every ms off of their code writing time. – Sнаđошƒаӽ Feb 15 '16 at 15:10
0

Your condition is written wrongly. It should have been

if(x <= 1000000000000000 && y <= 1000000000000000) {
    z=x-y;
    cout << std::abs (z);
}

However, your condition if(x && y <= 1000000000000000) evaluates in plain English to if x is true AND y is less than or equal to 1000000000000000, which is definitely not what you want. The way you have done, the condition will evaluate to true when y is less than 1000000000000000 AND x is any value other than zero, positive or negative. The reason behind this is in C any non-zero value evaluates to true.

Note: your code will fail when x and y are assigned any values not within the range of int which is -2^31 to 2^31-1 (considering int to be 32 bits, which is most common). You must use long long data type for x, y and z here.

long long x, y, z;

long long has range of -2^63 to 2^63-1, which is large enough to accommodate any number within the specified range, namely 0 to 1000000000000000.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
  • The range of `int` is not necessarily what you say. It is at least 16 bits, and I don't think there's an upper limit. – juanchopanza Feb 14 '16 at 08:36
  • @juanchopanza I have considered the `int` to be 32 bits, which I think is most common. Is not the range I said correct for 32 bit `int`? And how come there is no upper limit? BTW, I have run the code from [here](http://stackoverflow.com/a/31768642/3375713) and it agrees with me. – Sнаđошƒаӽ Feb 14 '16 at 09:07
  • The range may be correct for 32 bit int, but `int` isn't restricted to be 32 bits. It is specified to be at least as big as `short`, which is at least 16 bits. No upper limit is specified. And yes, these days 32 bit ints are very common. – juanchopanza Feb 14 '16 at 09:12
  • 1
    Similarly, `long long` has range of **at least** -2^63 **- 1** to 2^63 - 1. Yes, for most implementations it is, in fact, -2^63 to 2^63 - 1, but that extra value at the bottom is **not** required, and that range is the **minimum**. – Pete Becker Feb 14 '16 at 14:36
0

Your if-condition is wrong as others mentioned already. Your variable x results to true for every value except zero. So you have to write (x <= MAX && y <= MAX).

Also you can prevent using fixed max values buy using the function numeric_limits (limits library):

#include <iostream>
#include <limits>
#include <cmath> 

int main() {
int x;
int y;
int z;

std::cin >> x >> y;

if (x <= std::numeric_limits<long>::max() && y <= std::numeric_limits<long>::max()) {
z=x-y;
std::cout << std::abs (z);
}
}
Cloonix
  • 113
  • 1
  • 13