Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/** --------------------------------------------- * Project 3: Working with Points and Structs. * This program uses struct and given functions to * reads in a point from

/** --------------------------------------------- * Project 3: Working with Points and Structs. * This program uses struct and given functions to * reads in a point from the user, * return the distance between pt1 and pt2, * calculates the slope of the line, * and finally, calculates pt3 such that a line from * pt1 to pt3 is perpendicular to the line from pt1 * to pt2, and the distance between pt1 and pt2 * is the same as the distance between pt1 and pt3 * Class: CS 107, Spr 2018 * System: Online GDB, Macbook Air * * @author Khuma Magar * @version April. 11, 2018 * ---------------------------------------------- */ #include #include #include #include

struct point_struct { double xcoor; double ycoor; char label[25]; }; typedef struct point_struct Point;

// getPoint function receives two points from user input and it returns p(point). Point getPoint() { Point p; printf(" \tX coordinate: "); scanf("%lf", &p.xcoor); printf("\tY coordinate: "); scanf("%lf", &p.ycoor); return p; } // distance function is called to calculate the distance between points and it returns the distance between pt1 and pt 2 double distance(Point pt1, Point pt2) { return sqrt(pow(pt2.xcoor - pt1.xcoor,2) + pow(pt2.ycoor - pt1.ycoor,2)); // formula of the distance } // slope function is called to calculate the slope of the line and it returns the slope of the line between pt1 and pt2 double slope(Point pt1, Point pt2) { return (pt2.ycoor - pt1.ycoor) / (pt2.xcoor - pt1.xcoor); }

/* * Function perpPoint calculate pt3 such that a line from pt1 to pt3 is * perpendicular to the line from pt1 to pt2, and the distance between * pt1 and pt2 is the same as the distance between pt1 and pt3 * This is a void function so it does not returns anything. */ void perpPoint(Point pt1, Point pt2, Point pt3) { pt3.xcoor = pt2.xcoor - pt1.xcoor; pt3.ycoor = pt2.ycoor + 2; printf(" Point 5 is (%lf,%lf)",pt3.xcoor,pt3.ycoor); } // Main function to promt user for input, does not return anything int main() { Point anArray[5]; // Creates four points printf(" Enter Point 1 "); anArray[0] = getPoint(); printf(" Enter Point 2 "); anArray[1] = getPoint(); printf(" Enter Point 3 "); anArray[2] = getPoint(); printf(" Enter Point 4 "); anArray[3] = getPoint(); // Calculates distance between the points and displays it printf(" Distance of Point 1 and Point 2 %lf",distance(anArray[0],anArray[1])); printf(" Distance of Point 3 and Point 4 %lf",distance(anArray[2],anArray[3]));

// Calculates slope of the line and displays it printf(" Slope of Point 1 and Point 2 %lf",slope(anArray[0],anArray[1])); printf(" Slope of Point 3 and Point 4 %lf",slope(anArray[2],anArray[3]));

// Finds perpendicular point perpPoint(anArray[0],anArray[1],anArray[4]);

return 0; }

This is my code but sadly, I am having trouble with perpPoint and also getting -nan for certain test cases. Please check and let me know what am I missing.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Science For Dummies

Authors: Lillian Pierson ,Jake Porway

2nd Edition

1119327636, 978-1119327639

More Books

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

What is meant by decentralisation?

Answered: 1 week ago

Question

Write down the Limitation of Beer - Lamberts law?

Answered: 1 week ago

Question

6. Are my sources reliable?

Answered: 1 week ago

Question

5. Are my sources compelling?

Answered: 1 week ago