Saturday, January 30, 2010

CHAP 2[C]j Checking if three coordinates form a straight line

Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.

void main()
{
float x1,x2,x3,y1,y2,y3;
printf("\nEnter the three coordinates (x1,y1),(x2,y2),(x3,y3)");
scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
if ((y2-y1)/(x2-x1)==(y3-y2)/(x3-x2))
printf("\nThe three points fall on the same line");
else
printf("\nThe three points are not collinear");
}

7 comments:

  1. 1 1 1 2 1 3
    for this input runtime error is displaying

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. void main()
    {
    float x1,x2,x3,y1,y2,y3,temp;
    printf("\nEnter the three coordinates (x1,y1),(x2,y2),(x3,y3)");
    scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
    if(x1==x3)
    {
    temp=x1;
    x1=y1;
    y1=temp;

    temp=x2;
    x2=y2;
    y2=temp;

    temp=x3;
    x3=y3;
    y3=temp;

    }

    if ((y2-y1)/(x2-x1)==(y3-y2)/(x3-x2))
    printf("\nThe three points fall on the same line");
    else
    printf("\nThe three points are not collinear");
    }

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. why u swap when x1 and x3 are equal?

    ReplyDelete
  6. else it will show exception error divide by zero, which is runtime error

    ReplyDelete
  7. Slots Casinos Near DC, Philadelphia, MD - Mapyro
    Find and compare slot machines, video poker, and 양주 출장마사지 blackjack at 영주 출장안마 nearly 속초 출장안마 30000 Casino Wyandotte casinos in 군산 출장마사지 Maryland Heights, 부산광역 출장마사지 MD.

    ReplyDelete