Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
int main()
{
	int t,i,p1,p2,l=0,r=0;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{	scanf("%d%d",&p1,&p2);
		if(l>(p1-p2)|| l>(p2-p1))
		{	if(p1>p2)
			{	l=p1-p2;
				r=1;}
			if(p2>p1)
			{	l=p2-p1;
				r=2;}}}
	printf("%d %d",r,l);
	return 0;
}


What I have tried:

i tried this code and i think its not entering that 1st if statement.
Posted
Updated 20-Dec-17 18:05pm
v2
Comments
Richard MacCutchan 20-Dec-17 10:11am    
What are the values of p1 and p2?
jeron1 20-Dec-17 10:12am    
Could you explain what the code is trying to do and how it differs from you expectations? Also, that formatting makes it difficult to read your code.

So run it through a debugger and find out exactly what it is doing, and why.
How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
What this code is showing output 0 0 on codechef?

When you ask for help about a codeChef challenge, it is a good idea to give link to that challenge, just in order to give a chance to know what you talk about.

Advice: never pack code on same line, it just makes code more difficult to read without any benefit:
C++
#include<stdio.h>
int main()
{
	int t,i,p1,p2,l=0,r=0;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		scanf("%d%d",&p1,&p2);
		if(l>(p1-p2)|| l>(p2-p1))
		{
			if(p1>p2)
			{
				l=p1-p2;
				r=1;
			}
			if(p2>p1)
			{
				l=p2-p1;
				r=2;
			}
		}
	}
	printf("%d %d",r,l);
	return 0;
}


Quote:
What this code is showing output 0 0 on codechef?

The only answer is 'because it is what you requested in your code'.
To get better answer, you need to tell what is supposed to do this code.
 
Share this answer
 

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



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