In C language - Create main with an array of 5 points, reads in 4 points from th
ID: 3704665 • Letter: I
Question
In C language
- Create main with an array of 5 points, reads in 4 points from the user by calling getLabel() 4 times.
- Display the gap or distance between the 1st and 2nd points, and 3rd and 4th points using function gap.
- Display the slope of the line between the 1st and 2nd points, and the 3rd and 4th points using function slope.
- Create a 5th point by calling perpLabel() using 1st and 2nd points.
Code is provided below
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
struct mark_struct {
double xaxis;
double yaxis;
char tag[25];
};
typedef struct mark_struct Point;
Point getLabel(); // get a point from the user(stdin)
double gap(Point mk1, Point mk2); // returns the distance between mk1 and mk2
double slope(Point mk1, Point mk2);
/* finds slope of the line at mk1 going to mk2 */
void perpLabel (Point mk1, Point mk2, Point *mk3);
/* calculate mk3 such that a line from mk1 to mk3 is perpendicular to the line from pt1
to mk2 */
int main() {
Point aArray[5];
//finish main
return 0;
}
Explanation / Answer
======================================================================================
For calculating perpendicular point, we assume y=0 , so that we can calculate x3 = my2+x2
======================================================================================
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
struct mark_struct {
double xaxis;
double yaxis;
char tag[25];
};
typedef struct mark_struct Point;
Point getLabel() { // get a point from the user(stdin)
Point point;
printf(" Enter Point Label: ");
scanf("%s",point.tag);
printf(" Enter Cordinates: (x, y) : delimated by space:");
scanf("%lf%lf", &point.xaxis, &point.yaxis);
return point;
}
double gap(Point mk1, Point mk2) { // returns the distance between mk1 and mk2
return sqrt(pow(mk2.xaxis - mk1.xaxis, 2) + pow(mk2.yaxis - mk1.yaxis, 2));
}
double slope(Point mk1, Point mk2) {
/* finds slope of the line at mk1 going to mk2 */
// slope = (y2-y1)/(x2-x1)
return (mk2.yaxis - mk1.yaxis) / (mk2.xaxis - mk1.xaxis);
}
void perpLabel (Point mk1, Point mk2, Point *mk3){
/* calculate mk3 such that a line from mk1 to mk3 is perpendicular to the line from pt1
to mk2 */
Point p ;
strcpy(mk3->tag, "mk5"); // Assigning Lable
double slope12;
// Calculate slope of mk1 and mk2
slope12 = slope(mk1, mk2);
//Assuming, mk3.yaxis = 0
mk3->yaxis = 0;
// calculating yaxis .
mk3->xaxis = mk1.xaxis + slope12 * mk1.yaxis;
}
int main() {
Point aArray[5];
double gap12, gap34;
double slope12, slope34;
//Read 4 points from user.
aArray[0] = getLabel();
aArray[1] = getLabel();
aArray[2] = getLabel();
aArray[3] = getLabel();
//Find the gap between Point 1 & Point2
gap12 = gap(aArray[0], aArray[1]);
gap34 = gap(aArray[2], aArray[3]);
printf(" Distance between 1 & 2 : %lf", gap12);
printf(" Distance between 3 & 4 : %lf", gap34);
//Find the gap between Point 1 & Point2
slope12 = slope(aArray[0], aArray[1]);
slope34 = slope(aArray[2], aArray[3]);
printf(" Slope of 1 & 2 : %lf", slope12);
printf(" Slope of 3 & 4 : %lf", slope34);
perpLabel(aArray[0], aArray[1], &aArray[4]);
printf(" 5th Point is: (%lf, %lf)",aArray[4].xaxis,aArray[4].yaxis);
return 0;
}