Question

In: Computer Science

C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the...

C Language

Let us define a Point type to store two-dimensional coordinates (x, y)! Write the following functions operating on this data type:

  • dist(): calculates the distance between the two points received (using the Pythagorean theorem)
  • equal(): checks if to points are equal or not
  • read(): reads a point from the keyboard and returns it

In the main, define two points, and test all these functions. When all tests are passed, solve the following task by utilizing the structure and functions defined above:

A farmer wants to surround his land with a wire fence. Write a C program to compute how much fence is required! The program asks the farmer to enter the coordinates of the corner points of the fence, calculates the distances, and sums them up. Repeat these steps as long as the new coordinates are not equal to the first one (hence, till we return to the starting point).

It is recommended to store the starting point in a separate variable. After that, two more point variables are needed to calculate the length of a fence segment: one of them stores the current point, and the other the previous one.

Solutions

Expert Solution

Complete code in C:-

#include <stdio.h>
#include <math.h>

// Data structure that stores points' 'x' coordinate, 'y' coordinate.
typedef struct node {
   float x, y;
}Point;

// This function is used to square a 'double' type number
double sqr(double x) {
   return x*x;
}

// Function used to take coordinates of points as input from user
Point read() {
   double x, y;
   printf("Enter 'X' coordinate : ");
   scanf("%lf", &x);
   printf("Enter 'Y' coordinate : ");
   scanf("%lf", &y);
   Point point = {x, y};
   return point;
}

// Checks if two points are equal or not?
int equal(Point first, Point second) {
   return (first.x == second.x && first.y == second.y);
}

// Returns distance between two points, using pythagorean formula.
double dist(Point first, Point second) {
   double temp = (sqr(first.x - second.x) + sqr(first.y - second.y));
   double len = sqrt(temp);
   return len;
}

// Main function
int main(void) {

   // This variable will store the length of fence.
   double requiredFence = 0;

   // Taking first point input
   Point first = read();

   // Taking secdon point input.
   Point second, current = read();

   // Calculating distance between first and current point.
   requiredFence += dist(first, current);

   // Run this while loop until reads first point again from user
   while(equal(first, current) == 0) {
       second = current;
       current = read();
       requiredFence += dist(second, current);
   }
   printf("\nTotal fence required : %lf\n", requiredFence);
   return 0;
}

Screenshot of output:-


Related Solutions

in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y)...
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y) in 2-D space. The private data members are the coordinates X and Y of type ‘double’. The public member functions are: A default constructor (no parameters) to initialize X and Y to zero. An explicit value constructor with two parameters to initialize the values of X and Y. Two functions, one to set the X value and the other to set the Y value...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In this problem, you will write a Point class which stores this information and provides useful methods to work with points. We have provided an __init__ definition for the Point class. You will note that this accepts two arguments, being the x- and y-coordinates of the point. We have also included a __repr__ method, which will provide a canonical string representation of the point. Make...
Separate the wave equation in two-dimensional rectangular coordinates x, y. Consider a rectangular membrane, rigidly attache...
Separate the wave equation in two-dimensional rectangular coordinates x, y. Consider a rectangular membrane, rigidly attache to supports along its sides, such that a ≤ x ≤ 0 and b ≤ y ≤ 0. Find the solution, including the specification of the characteristic frequencies of the membrane oscillations. In the case of a = b, show that two or more modes of vibration correspond to a single frequency
Let S be the two dimensional subspace of R^4 spanned by x = (1,0,2,1) and y...
Let S be the two dimensional subspace of R^4 spanned by x = (1,0,2,1) and y = (0,1,- 2,0) Find a basis for S^⊥
Find the coordinates of the point (x, y, z) on the plane z = 4 x...
Find the coordinates of the point (x, y, z) on the plane z = 4 x + 1 y + 4 which is closest to the origin.
Let f(x, y) = − cos(x + y2 ) and let a be the point a...
Let f(x, y) = − cos(x + y2 ) and let a be the point a = ( π/2, 0). (a) Find the direction in which f increases most quickly at the point a. (b) Find the directional derivative Duf(a) of f at a in the direction u = (−5/13 , 12/13) . (c) Use Taylor’s formula to calculate a quadratic approximation to f at a.
. Let two circles C1 and C2 intersect at X and Y. Prove that a point...
. Let two circles C1 and C2 intersect at X and Y. Prove that a point P is on the line XY if and only if the power of P with respect to C1 is equal to the power of P with respect to C
Let f (x, y) = c, 0 ≤ y ≤ 4, y ≤ x ≤ y...
Let f (x, y) = c, 0 ≤ y ≤ 4, y ≤ x ≤ y + 1,  be the joint pdf of X and Y. (a) (3 pts) Find c and sketch the region for which f (x, y) > 0. (b) (3 pts) Find fX(x), the marginal pdf of X. (c) (3 pts) Find fY(y), the marginal pdf of Y. (d) (3 pts) Find P(X ≤ 3 − Y). (e) (4 pts) E(X) and Var(X). (f) (4 pts) E(Y)...
The coordinates of a point P in the x-y plane are (2;3) meters. q1 = 10...
The coordinates of a point P in the x-y plane are (2;3) meters. q1 = 10 C load (1;0) , q2 = 20 C load (-1;0) is placed in the metre position. The electric flied formed by these charges at the P point, find your size and direction.
(a). The Cartesian coordinates (x, y) = (−4, 4) of a point are given. Find polar...
(a). The Cartesian coordinates (x, y) = (−4, 4) of a point are given. Find polar coordinates (r, θ) of the point so that r > 0 and 0 ≤ θ ≤ 2π. (b). The polar coordinates (r, θ) = (−3, 5π/6) of a point are given. Find the Cartesian coordinates (x, y) of this point.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT