# Problem with taking inputs/calculating slope

This is a discussion on Problem with taking inputs/calculating slope within the C Programming forums, part of the General Programming Boards category; Hey all. Got a problem. I'm writing a program that takes the coordinates for 3 points and I'm supposed to ...

1. ## Problem with taking inputs/calculating slope

Hey all. Got a problem. I'm writing a program that takes the coordinates for 3 points and I'm supposed to identify whether or not the points in relation to eachother are collinear or not. For example, I have points

(x1, y1) (x2, y2)(x3, y3)

If the points were collinear, the slope between pt1 and pt2 would be the same as the slope between pt2 and pt3.

My code so far gives me this error
Code:
```collinear.c: In function `main':
collinear.c:17: error: invalid lvalue in assignment```
And here's my code so far
Code:
```#include <stdio.h>
#include <math.h>

main()
{

double x1;
double y1;
double x2;
double y2;
double x3;
double y3;

printf("Enter coordinates for 3 points--x1 y1 x2 y2 x3 y3\n");
scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);

if(((y2 - y1)/(x2 - x1)) = ((y3 - y2)/(x3 - x2)))
{
printf("The points entered are collinear\n");
}

else
{
printf("The points entered are not collinear\n");
}

}```

2. Comparison is done using ==, not =.

Also, this will not work with vertical lines (if x1==x2 or x2==x3).

3. Ahh, much thanks!

4. also note that main should be declared as
int main(void)

5. My indentation?

Any suggestions?

6. Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{

double x1;
double y1;
double x2;
double y2;
double x3;
double y3;

printf("Enter coordinates for 3 points--x1 y1 x2 y2 x3 y3\n");
scanf("&#37;lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);

if (((y2 - y1)/(x2 - x1) ) == ( (y3 - y2)/(x3 - x2)))
{
printf("The points entered are collinear\n");
}
else
{
printf("The points entered are not collinear\n");
}
}```

7. just copied into VS and pressed Alt+F8
Code:
```#include <stdio.h>
#include <math.h>

main()
{

double x1;
double y1;
double x2;
double y2;
double x3;
double y3;

printf("Enter coordinates for 3 points--x1 y1 x2 y2 x3 y3\n");
scanf("&#37;lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);

if(((y2 - y1)/(x2 - x1)) = ((y3 - y2)/(x3 - x2)))
{
printf("The points entered are collinear\n");
}

else
{
printf("The points entered are not collinear\n");
}

}```

8. Ups... you have mix of spaces and tabs
Edited... was already beated... so i remove this intermediate state

9. Nice and organized. Thanks!

10. Code:
```{
//code
}```
not
Code:
```{
//code
}```
edit: beat twice (maybe thrice)! ARG!!

11. You might want to check for division by 0!

12. Originally Posted by Bajanine
You might want to check for division by 0!
Indeed, but don't write special case code for that. Instead make use of the following algebraic identity:
Code:
`a / b == c / d   <==>   a * d == c * b`
This converts the divisions by zero into multiplications by zero, which as you know is not a problem.

However in practical terms you still have another bug either way. The problem is to do with floating point inaccuracy and that fact that you can't compare values using ==.
You should instead be usinfg fabs to test the values within an epsilon value of each other.