Click here to Skip to main content
15,920,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

Problem statement
Snuke has a calculator. It has a display and two buttons.

Initially, the display shows an integer 
x
. Snuke wants to change this value into another integer 
y
, by pressing the following two buttons some number of times in arbitrary order:

Button A: When pressed, the value on the display is incremented by 
1
.
Button B: When pressed, the sign of the value on the display is reversed.
Find the minimum number of times Snuke needs to press the buttons to achieve his objective. It can be shown that the objective is always achievable regardless of the values of the integers 
x
 and 
y
x and y are integers and  different
|x|,|y|≤10^9
.
Actually in this all the inputs are working but only two are not working, not working in the sense that they are not giving the desired output(according to the question) and BTW I also dont know the inputs


What I have tried:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int x,y,steps;
    cin>>x>>y;
    if(x>=0&&y>=0){                                                    
        if(x<y)steps=y-x;                                            
        if(x>y&&y!=0)steps=x-y+2;
        if(x>y&&y==0) steps= x-y+1;                                                            
    }                                                               
    else if(x<=0&&y<=0){                                                                     
        if(x<y)steps=y-x;                                          
        if(x>y&&x!=0) steps=x-y+2;  
       if(x>y&&x==0) steps= x-y+1;                                                          
    }
    else steps=-1*(x+y)+1;
    cout<<steps;
}
Posted
Updated 23-May-21 5:10am
v3
Comments
Richard MacCutchan 23-May-21 8:33am    
You are using the same calculations for the case when x and y are greater than zero, or less than zero. So you only to one set.

Your calculation for x < 0 and y < 0 is the same as when x > 0 and y > 0. Is that correct, or did you copy-paste the code and forget to change it?

You may also need to consider the cases x == y, x == 0, and y == 0.

I assume that "Some inputs are getting processed and some are not" means that some inputs result in the wrong number of steps being displayed, not that the inputs themselves are not being read by cin. When describing a problem, it is important to be clear.
 
Share this answer
 
Comments
Aquib Javid Mir 23-May-21 11:15am    
Hi Sir, I had mentioned in the question that x and y are different and i did write the code for if x || y =0 and some more inputs got executed but two more are not working. Also sorry for the lack of clarity and i will surely take care of it from next time and i have also modified the question
Greg Utas 23-May-21 11:29am    
When code isn't working and you can't figure it out by looking at it, you need to use a debugger. I pointed out some things that I could see easily, but I'm not willing to debug your code for you. The only way to get a big program working is to debug it, so you should learn how to do it now. A debugger allows you to step through your code one statement at a time so that you can quickly discover where its logic differs from what you intended.
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

And since we have no idea what inputs you give it, or even what that code is supposed to do we can't do any of that for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900