8 / 26
Jun 2012

Hello, Here is my code........Can any one tell me why am i getting wrong answer?
even though it gives right answers for the sample test case and the one posted here.......? pls...........

#include<stdio.h>
int main()
{
    int nt,i,n,m,j,x1,x2,y1,y2,xt,yt;
    scanf("%d",&nt);
    for(i=0;i<nt;i++)
    {
                     scanf("%d %d",&n,&m);
                     int x[n],y[n];int out[m],k=0,l=0;
                     for(j=0;j<n;j++)
                     {
                                     scanf("%d %d",&x[j],&y[j]);
                                     }
                     for(j=0;j<m;j++)
                     {
                                     scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
                                     int lhs,rhs;
                                     lhs=(xt-x1)*(y2-y1);
                                     rhs=(yt-y1)*(x2-x1);
                                     int cnt=0;
                                     for(l=0;l<n;l++)
                                     {
                                                     if(x[l]>=x1 && y[l]>=y1 && x[l]<=x2 && y[l]<=y2)
                                                     {
                                                     if(((x[l]-x1)*(y2-y1))==((y[l]-y1)*(x2-x1)))
                                                                 cnt++;
                                                                 }
                                                                 }
                                     out[j]=cnt;
                                     }
                     for(j=0;j<m;j++)
                     {
                                     printf("%d\n",out[j]);
                                     }
                                     }
    getch();
}

You are performing calculations with uninitialized variables.

Ok sir......I tried initializing all the variables to zero........Still getting wrong answer sir............ frowning frowning

here is my code sir......

#include<stdio.h>
int main()
{
    int nt=0,i=0,n=0,m=0,j=0,x1=0,x2=0,y1=0,y2=0,xt=0,yt=0;
    scanf("%d",&nt);
    for(i=0;i<nt;i++)
    {
                     scanf("%d %d",&n,&m);
                     int x[n],y[n];int out[m],k=0,l=0;
                     for(j=0;j<n;j++)
                     {
                                     scanf("%d %d",&x[j],&y[j]);
                                     }
                     for(j=0;j<m;j++)
                     {
                                     scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
                                     int lhs=0,rhs=0;
                                     lhs=(xt-x1)*(y2-y1);
                                     rhs=(yt-y1)*(x2-x1);
                                     int cnt=0;
                                     for(l=0;l<n;l++)
                                     {
                                                     if(x[l]>=x1 && y[l]>=y1 && x[l]<=x2 && y[l]<=y2)
                                                     {
                                                     if(((x[l]-x1)*(y2-y1))==((y[l]-y1)*(x2-x1)))
                                                                 cnt++;
                                                                 }
                                                                 }
                                     out[j]=cnt;
                                     }
                     for(j=0;j<m;j++)
                     {
                                     printf("%d\n",out[j]);
                                     }
                                    }
          return 0;
          }

Your issue lies mainly in this line of code:
if(x[l]>=x1 && y[l]>=y1 && x[l]<=x2 && y[l]<=y2)

1 month later

sir i am getting correct aswer for test case and the case given in forum but WA when i submit it at spoj.
dont know why.
[

Your issue is almost 100% identical to the poster above...
if(a[j]>=x1[i]&&a[j]<=x2[i])

if((a[j]>=x1[i]&&a[j]<=x2[i])||(a[j]<=x1[i]&&a[j]>=x2[i]))

this is also giving wrong answer

but why?
coordinates are positive only not negative so i shouold not consider the negative case

ok i read it again .
coordinated of segment can be -ve bt of points are +ve.i am right?
i changed code but getting wrong only.

if 2nd x coordinate is -ve i have interchanged both points of segment

It's not a matter of positive or negative. If all of the variables in this if statement are positive it still has the same issue.

u want to say that 5 5 0 0 can be input so

if(1>=5 && 1<=0)

will be false.
i have considered this case also in below code.

Your algorithm is not accurate. You have not considered all possibilities.

kkkkk......i wil code with a new approach but.....i just want 2 know what is wrong with this approach......inspite it is lengthy i want 2 know where do i fail......???

The biggest issue is that you are assuming that the slope will be an integer value. As mentioned before you should abandon this solution and simplify it greatly. Work with a pen and paper for a while and find a better algorithm.

i dont have patience to read this much length of code like leppy sir wink wink
but what i caught in 3 seconds was

m1=(points[j][1]-start[i][1])/(points[j][0]-start[i][0]);

if points[j][0]=0 and start[i][0]=0;
x/0 underministic
and rest follow what sir say

@Leppy-----i worked on some more furious test cases and debugged my code .............CODE REMOVED