Write a C program. Where a and b are not both zero. The program should display a
ID: 3593919 • Letter: W
Question
Write a C program.
Where a and b are not both zero.
The program should display a menu with the following options:
a) Enter points
b) Compute the distance
c) Display the line equation
d) Find the closest point to the line in c
e) Exit
The program will finalize ONLY when option e) is chosen.
Given the following list of point coordinates and label: Point Label X-Coordinate Y-Coordinate 50.2 45.7 76.7 12 75.8 48 99.1 14 48 25 23 36 123 34.6 Table 1: Point Information The information is given as three arrays: i) A char array named point_label, for column 1 in Table 1 ii A double array named x_coord, for column 2 in Table 1 iii)A double array named y coord, for column 3 in Table 1 The table information can be hard coded Given two points, entered using the labels (i.e. A and B), the program will compute: a) The distance between the two points b) The equation of the line crossing these points in the form: y mx+k c) Among the other points, the closest perpendicular point to the line described by the two points given(i.e. if A and B are chosen, then look for which of C,D,F,G are the closest) The distance from a point Po(Xo.yo) to a line in the form ax+by+c-0, is defined as: laxo byo +cl distance(ax + by + c = 0, P0)- 2Explanation / Answer
//if you have any issues, doubts or errors do comment it out, I shall help you. Hope this helps!
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
char lab[]={'A','B','C','D','E','F','G','H','I'}; //hardcode the data
double x_coord[]={50.2,45.7,76.7,12.-25,23,-34.5};
double y_coord[]={75.8,48,99.1,14,48,-36,-123};
void enterPoint()
{
//if data is not hard-coded, enter points
}
void distance()
{
int flag1=-1,flag2=-1; //flagsfor variable index
char a,b;
printf(" Input the labels :"); //input labels
scanf("%c %c",&a,&b);
for(int i=0;i<7;i++)
{
if(lab[i]==a)
flag1=i;
if(lab[i]==b)
flag2=i;
}
if(flag1==-1 || flag2==-1 );
else //find the distance
printf("%f",sqrt((x_coord[flag1]-x_coord[flag2])*(x_coord[flag1]-x_coord[flag2])+(y_coord[flag1]-y_coord[flag2])*(y_coord[flag1]-y_coord[flag2])));
}
void equation()
{
int flag1,flag2; //same as before
char a,b;
printf(" Input the labels :");
scanf("%c %c",&a,&b);
for(int i=0;i<7;i++)
{
if(lab[i]==a)
flag1=i;
if(lab[i]==b)
flag2=i;
}
double m=(y_coord[flag1]-y_coord[flag2])/(x_coord[flag1]-x_coord[flag2]); //find the slope
double c=y_coord[flag1]-m*x_coord[flag1]; //find the constant
printf("y = %fx + %f",m,c); //print equation
}
void closest()
{
int flag1,flag2; //same as before
char a1,b1;
printf(" Input the labels :");
scanf("%c %c",&a1,&b1);
for(int i=0;i<7;i++)
{
if(lab[i]==a1)
flag1=i;
if(lab[i]==b1)
flag2=i;
}
double m=(y_coord[flag1]-y_coord[flag2])/(x_coord[flag1]-x_coord[flag2]);
double c=y_coord[flag1]-m*x_coord[flag1];
double a=m,b=-1,dist,min=0; //assign as line equation
for(int i=0;i<7;i++)
{
if(i!=flag1 && i!=flag2)
{
dist=abs(a*x_coord[i]+b*y_coord[i]+c)/sqrt(a*a+b*b); //find distance from line
if(dist<min)
min=dist; //check if minimum
}
}
printf("%f",min); //display minimum
}
int main(void) {
// your code goes here
int ch;
while(1)
{
//display menu
printf(" 1) Enter points 2) Compute the distance 3) Display the line equation 4) Find the closest point to the line in c 5) Exit");
scanf("%d",&ch);
if(ch==1)
enterPoint(); //enter point
else if(ch==2)
distance(); //compute distance
else if(ch==3)
equation(); //find equation
else if(ch==4)
closest(); //closest distance
else if(ch==5)
exit(1); //exit
else
printf(" Wrong input, Try again."); //try again
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.