Home > Net >  Read two coordinate points print the equation of line joining the points
Read two coordinate points print the equation of line joining the points

Time:12-21

#include<stdio.h>
main()
{
    int x1,y1,x2,y2,c,y,x;
    float m;
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    printf("the two cordinate points are <%d,%d> and <%d,%d>",x1,y1,x2,y2);

    if(m=y2-y1/x2-x1){
    y=m*x c;
    c=y-m*x;
    printf("the equation of a straight line is y=mx c");}
    else printf("no equation");
    return 0;
}  

I am not getting the answer. I think I have trouble in expressing slope or constant term. could you please [help?]

CodePudding user response:

There are two problems on the line that set m:

  • You need to use parentheses or else the compiler will evaluate y1/x1 first before doing anything else.
  • It's unusual to put that line inside an if statement because it looks like you might be trying to do a comparison; let's just put it on its own line.
  • Since m is a float you probably want to do floating point division instead of an integer division.

Putting that information together we get this line to compute the slope:

m = (float)(y2 - y1) / (x2 - x1);

For now I think we should just remove the if statement and the else clause corresponding to it. There is no reason why a slope of zero has to be a special case that causes your program to terminate.

Moving on to the next line. x is uninitialized, so please comment out the lines that read from it. The compiler should have warned you about that, so please make sure your compiler warnings are enabled, and that you read them, and fix all of them.

// y = m * x   c
// c = y - m * x

To compute c, I think the line you want to run is:

c = y1 - m * x1;

(You could equally well use y2 and x2.)

Then you just need to add a line that prints m and c. You already know how to print integers with printf because you printed 4 integers earlier in your program, so just add another line like that which prints m and c.

  •  Tags:  
  • c
  • Related