The Line Program Create a Line program that will: .Contain two point structs tha
ID: 3920010 • Letter: T
Question
The Line Program Create a Line program that will: .Contain two point structs that represents the endpoints of a line Allow the user to enter values for x and y coordinates for each endpoint Use the distance formula to display the length of the line described by the endpoints . This Line program should have: .A function that will read keyboard input to set x and y values for either point .A function that will determine and display the length of the line A menu function Additionally, your program must validate endpoint input. Only endpoints with positive integer x and y values may be accepted. See example runs.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Point {
int x, y;
};
//This is a function to set coordinates of points
struct Point setPoints(struct Point p)
{
int temp, status;
printf("Please enter x coordinate: ");
status = scanf("%d", &p.x);
/*below while loop ensures entered input is only integer
if string or special character or negative numbers are entered it will not accept till positive integer is entered*/
while(status!=1 || p.x<0){
while((temp=getchar()) != EOF && temp != ' ');
printf("Invalid input... please enter a number: ");
status = scanf("%d", &p.x);
}
printf("Please enter y coordinate: ");
status = scanf("%d", &p.y);
while(status!=1 || p.y<0){
while((temp=getchar()) != EOF && temp != ' ');
printf("Invalid input... please enter a number: ");
status = scanf("%d", &p.y);
}
return p;
}
//This function returns the distance between two points
double getDistance(struct Point a, struct Point b)
{
double distance;
distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
return distance;
}
int main()
{
struct Point a, b;
int choice=0;
while(1)
{
printf(" Press 1 to enter points ");
printf("Press 2 to exit ");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
printf("Enter coordinates of first point ");
a = setPoints(a);
printf("Enter coordinates of second point ");
b = setPoints(b);
printf("Distance between a and b: %lf ", getDistance(a, b));
break;
}
case 2: {
exit(0);
}
default : exit(0);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.