Last active
July 20, 2018 15:12
-
-
Save Vivek-abstract/cab93a37823147cfa3cf27e7c0c2b32e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() | |
{ | |
int points[10][2], table[20][100]; | |
int nPoints, cal_a, cal_b, max; | |
printf("Enter number of points: "); | |
scanf("%d", &nPoints); | |
for (int i = 0; i < nPoints; i++) | |
{ | |
printf("Enter points leaving space between x and y:\n"); | |
scanf("%d%d", &points[i][0], &points[i][1]); | |
} | |
for (int i = 0; i < 20; i++) | |
{ | |
for (int j = 0; j < 100; j++) | |
{ | |
table[i][j] = 0; | |
} | |
} | |
for (int j = 0; j < nPoints; j++) | |
{ | |
for (int i = -9; i < 10; i++) | |
{ | |
cal_a = i; | |
cal_b = points[j][1] - points[j][0] * i; | |
// Make sure index is positive | |
table[cal_a + 9][cal_b + 50] += 1; | |
} | |
} | |
for (int i = 0; i < 20; i++) | |
{ | |
for (int j = 0; j < 100; j++) | |
{ | |
if (max < table[i][j]) | |
{ | |
max = table[i][j]; | |
cal_a = i - 9; | |
cal_b = j - 50; | |
} | |
} | |
} | |
printf("There are %d collinear points\n", max); | |
printf("They are: "); | |
for (int i = 0; i < nPoints; i++) | |
{ | |
if ((points[i][1] - points[i][0] * cal_a) == cal_b) | |
{ | |
printf("(%d, %d) ", points[i][0], points[i][1]); | |
} | |
} | |
printf("\n"); | |
return 0; | |
} | |
/* OUTPUT: | |
Enter number of points: 4 | |
Enter points leaving space between x and y: | |
1 1 | |
Enter points leaving space between x and y: | |
2 3 | |
Enter points leaving space between x and y: | |
3 3 | |
Enter points leaving space between x and y: | |
4 4 | |
There are 3 collinear points | |
They are: (1, 1) (3, 3) (4, 4) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment