c++ assignment, answer the following questions 1) Create a struct Point2D that r
ID: 3834319 • Letter: C
Question
c++ assignment, answer the following questions
1) Create a struct Point2D that represents a point in a 2 dimensional plane. It should have two fields x and y. Create a few points at different locations. Then print their locations.
2) Create a struct Student that represents a student. It should have a name, a school, a student id and a grade field. Create a few different students all with grade '-'. Then give them all 'A's.
3) Create a function named dist which takes two Point2D points as parameters and returns the distance between the two points.
4).Write a function named moveTo that takes two Point2D points and moves the first to the position of the second. The function returns nothing. Call your function from the main and verify by calling printLocation on the first point that it has indeed moved.
Explanation / Answer
Please find my implementation.
1) Create a struct Point2D that represents a point in a 2 dimensional plane. It should have two fields x and y. Create a few points at different locations. Then print their locations.
struct Point2D{
int x;
int y;
};
2) Create a struct Student that represents a student. It should have a name, a school, a student id and a grade field. Create a few different students all with grade '-'. Then give them all 'A's.
struct Student{
string name;
string school;
int id;
char grade;
};
Student s1 = {"pk", "school1", 1, '_'};
Student s2 = {"Rahul k", "school2", 2, '_'};
Student s3 = {"Alex", "school3", 3, '_'};
s1.grade = 'A';
s2.grade = 'A';
s3.grade = 'A';
3) Create a function named dist which takes two Point2D points as parameters and returns the distance between the two points.
double dist(Point2D p1, Point2D p2){
int xdiff = p1.x - p2.x;
int ydiff = p1.y - p2.y;
double d = sqrt(xdiff*xdiff + ydiff*ydiff);
return d;
}
4).Write a function named moveTo that takes two Point2D points and moves the first to the position of the second. The function returns nothing. Call your function from the main and verify by calling printLocation on the first point that it has indeed moved.
void moveTo(Point2D p1, Point2D p2){
p1.x = p1.x + p2.x;
p1.y = p1.y + p2.y;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.