KCULEARN BackForw Back Forward SYSC2006-Assignment2 (100 points) Purpose. To all
ID: 3869267 • Letter: K
Question
KCULEARN BackForw Back Forward SYSC2006-Assignment2 (100 points) Purpose. To allow you to exercise with basic arrays, structs, and pointers (make and document any reasonable assumptions) Question: Given a struct as folows: typedef struct int y lpoint Write a separate C program to implement the following functions, your programs must have a main function to test your functions: a float euclideanDistance(point pl, point p2): //computes the Euclidean distance between two points b. float averageDistance(point arr1[- point arr211): /takes two arrays of points, computes the Euclidean distance //between each corresponding points in arr1, and arr2 //and returns the average of the distances c. float maximum Distance(point "arr1, point arr2) /takes two pointers to arrays of points, computes the Eudidean distance //between each corresponding points in arr1, andar2 //and returns the MAXIMUM of the distances vertex 1, point vertex2, point P d int //given a rectangle with the upper left vertex as vertex //and the lower right vertex as vertex 2 //the function returns 1 ifpoint p is within the bounds of the rectangle //or returns O if the point is not within the bounds of the rectangle e.point fourthVertex(point vertex1, point vertex2, point vertex3) //vertexi is the upper left vertex of a rectangle //vertex 2 is the lower left vertex of a rectangle /vertex 3 is the upper right vertex of a rectangle //the function returns a point that holds the value of the fourth vertex You will be graded as follows for each problem (10 points eachExplanation / Answer
Hi, have a look at the below content for explanation and other required items. Thanks.
#include <stdio.h>
#include <math.h>
typedef struct {
int x;
int y;
} point;
float euclideanDistance(point p1, point p2) {
// we create two temporary variables to store (x1-x2)^2 and (y1-y2)^2
// to avoid a complex single-line function.
float deltaXsquared = (p1.x - p2.x)*(p1.x - p2.x);
float deltaYsquared = (p1.y - p2.y)*(p1.y - p2.y);
// from the formula of euclidean distance, we have:
// the math.h library we imported provides a function named sqrt to find the square root of a number.
float euclideanDistance = sqrt(deltaXsquared + deltaYsquared);
return euclideanDistance;
}
float averageDistance(point arr1[], point arr2[]) {
// IMPORTANT: since in C, pointers to arrays and arrays themselves do not store the
// length of the array (number of elements), we need to either know the length
// beforehand, or pass in the length as another parameter alongside the arrays themselves
// here, we have assumed the number of points in each array is two, for simplicity and
// ease of providing sample data. this can be changed as per the input we enter,
// or the function can be changed to also accept the length of the arrays.
int numberOfPoints = 2;
printf("numberOfPoints is %d ", numberOfPoints);
// we use a float variable to keep track of the total distance between pairs of points.
float sumOfDistances = 0.0;
for(int i = 0; i < numberOfPoints; i++) {
// for clarity during coding:
//printf("points are (%d, %d), (%d, %d) ", arr1[i].x, arr1[i].y, arr2[i].x, arr2[i].y);
sumOfDistances = sumOfDistances + euclideanDistance(arr1[i], arr2[i]);
}
// we can find the average distance by dividing the sum of distances of each pair of points
// with the number of pairs of points.
float averageDistance = sumOfDistances/numberOfPoints;
return averageDistance;
}
float maximumDistance(point *arr1, point *arr2) {
// Important: Again as with the previous function, the pointer arr1 and arr2
// do not contain any information about how many elements are in the array they
// point to the first element of, so we have to either assume beforehand, or
// change the function to also take the length of the arrays pointed to.
int numberOfPoints = 2;
printf("numberOfPoints is %d ", numberOfPoints);
// we use a float variable to keep track of the maximum distance as we check
// each pair of points
float maxofTheDistances = 0.0;
for(int i = 0; i < numberOfPoints; i++) {
// for clarity while coding the function:
// printf("points are (%d, %d), (%d, %d) ", (*(arr1+i)).x, (*(arr1+i)).y, (*(arr2+i)).x, (*(arr2+i)).y);
float thisDistance = euclideanDistance(*(arr1+i),*(arr2+i));
if (thisDistance > maxofTheDistances) {
// this means that the distance in this current pair is more
// than any distance checked so far
maxofTheDistances = thisDistance;
}
}
return maxofTheDistances;
}
int withinRectangle(point vertex1, point vertex2, point p) {
// logically, when we draw a rectangle and provide the top left and bottom right vertices,
// we get a region between them whose boundaries are the co-ordinates of the vertices.
// we can check this by sketching a rectangle.
// point vertex1 (upper left) provides the maximum y, and minimum x of any point in the rectangle
int maxY = vertex1.y;
int minX = vertex1.x;
// point vertex2 (lower right) provides the maximum x, and minimum y of any point in the rectangle
int maxX = vertex2.x;
int minY = vertex2.y;
// for debugging while coding the function:
// printf("minX, minY, maxX, maxY: %d, %d, %d, %d ", minX, minY, maxX, maxY);
// printf("p.x, p.y = %d, %d ", p.x, p.y);
//if the x and y co-ordinates of the point p are within these limits, it is in the rectangle, otherwise it is not.
if (p.x <= maxX && p.x >= minX && p.y >= minY && p.y <= maxY) {
return 1;
}
else {
return 0;
}
}
point fourthVertex(point vertex1, point vertex2, point vertex3) {
// we are given that, of the rectangle:
// vertex1 is the upper left corner
// vertex2 is the lower left corner
// vertex3 is the upper right corner
// we need to find the lower right corner
// clearly, the lower right corner will have the same x co-ordinate as the upper right corner
// and the same y co-ordinate as the lower left corner (draw this out to be sure of it)
// so, the point fourthvertex can be constructed simply as:
point fourthvertex = {vertex3.x,vertex2.y};
return fourthvertex;
}
int main(void) {
// to test the functions we have written, we need to create a few sample points:
point p1 = {3, 3};
point p2 = {0, 0};
printf("Euclidean distance between p1 and p2 : %f ", euclideanDistance(p1, p2));
point p3 = {5, 6};
point p4 = {2, 9};
// we use two pairs of points to create the two arrays for the next two functions
// this can be changed to add more points or take input dynamically and the assumption of
// length of each array being two points can be updated as mentioned in the function.
point arr1[] = {p1, p2};
point arr2[] = {p3, p4};
printf("Average Distance: %f ", averageDistance(arr1, arr2));
printf("Maximum Distance: %f ", maximumDistance(arr1, arr2));
// we set up the vertices of a sample rectangle to test the
// withinRectangle and fourthVertex functions:
point upperLeft = {0, 5};
point lowerLeft = {0, 2};
point upperRight = {3, 5};
point lowerRight = {3, 2};
// the point to check for "within-ness" is below:
point isinside = {2,4};
int iswithinrect = withinRectangle(upperLeft, lowerRight, isinside);
// we show the result of our function - 1 means the point is within bounds of the
// rectangle, 0 means it is not.
printf("Result of withinRectangle(): %d ", iswithinrect);
// We reuse the same points as the above rectangle - notice that we should
// correctly get {3, 3} as the result of this fourthVertex function
// when we pass the other three vertices:
point foundFourthVertex = fourthVertex(upperLeft, lowerLeft, upperRight);
printf("Fourth vertex is (%d, %d) ", foundFourthVertex.x, foundFourthVertex.y);
printf("Done testing each function.");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.